Commit a86d2f12 authored by Job Cuppen's avatar Job Cuppen

final mandelbrot

parent a7485db0
module Fractal_tier1;
use Structures: all;
use Numerical: all;
export all;
/** <!--*******************************************************************-->
*
* @fn : complex[.,.] genComplexArray( int[2] shp, complex cmin, complex cmax)
*
* @brief generates an array of complex numbers with shape "shp" and
* linearly increasing/decreasing values between "cmin" and "cmax".
* The element at index [0,0] should be "cmin". Increases in the
* 2nd index (!) should reflect increases in the real values!
*
*****************************************************************************/
inline
complex[.,.] genComplexArray( int[2] shp, complex cmin, complex cmax)
{
//g = genarray(shp, cmax);
realCmin = real(cmin);
imagCmin = imag(cmin);
diffReal = real(cmax) - realCmin;
diffImag = imag(cmax) - imagCmin;
// print(cmin);
// print(cmax);
//print(diffReal);
//print(diffImag);
//shp = [3,3];
realShapeSize = tod(shp[[0]] );
imagShapeSize = tod(shp[[1]] );
realStep = diffReal/imagShapeSize;
imagStep = diffImag/realShapeSize;
arr = with {
(. <= [i,j] <= .) : toc(realCmin + tod(j) * realStep, imagCmin + tod(i) * imagStep);
} : genarray(shp, toc(0,0));
return arr;
//return g;
/**
* fill in here...
*/
}
/** <!--*******************************************************************-->
*
* @fn int escapeTime(complex z, int depth)
*
* @brief iteratively computes c = c*c + z starting from c=z.
* terminates either when |c| > 2 or depth iterations
* have been performed.
* NB: Besides "norm" there exists "normSq" which computes |c|^2.
*
* @return number of iterations done
*****************************************************************************/
inline
int escapeTime(complex z, int depth)
{
//return z>toc(0.0,1.0) ? 2 : 0 ;
i = 0;
c = z;
while(normSq(c) <= tod(4) && i<=depth){
c = c*c + z;
i++;
}
return i;
/**
* fill in here...
*/
}
/** <!--*******************************************************************-->
*
* @fn int[*] escapeTime(complex[*] z, int depth)
*
* @brief maps escapeTime to an entire array of complex numbers
*
*****************************************************************************/
inline int[*] escapeTime( complex[*] plane, int depth)
{
// z = {iv -> escapeTime(plane[iv], depth) };
z = with {
(. <= iv <= .): escapeTime(plane[iv], depth) ;
}:genarray(shape(plane), 0);
// z = with{
// z = with{
// (. <= iv <= .): escapeTime(plane[iv], depth);
// }: modarray(plane);
return z;
/**
* fill in here...
*/
}
/** <!--*******************************************************************-->
*
* @fn color[.,.] intArrayToMonochrome( int[.,.] a)
*
* @brief converts non-negative integer values into color values using
* a logarithmic 256 element clut from black to red.
*****************************************************************************/
inline
Color8::color[.,.] intArrayToMonochrome( int[.,.] a)
{
clut = genLogarithmicClut( 0.4d, 0.9d, Color8::black(), Color8::red());
z = 0;
for(i = 0; i<shape(a)[0]; i++){
for(j = 0; j<shape(a)[1]; j++){
if(a[[i,j]] > z){
z = a[[i,j]];
}
}
}
z = (z == 0) ? 1:z; // default to 1 so that we don't divide by zero
a = (a * 256) / z;//z;//1024;//maxval(a);//1024;// maxval(a);
d = with {
( .<= iv <= .) : clut[ a[ iv]];
} : genarray( shape(a), Color8::black());
//print(d);
return( d);
}
/*Color8::color[.,.] stretchRGB(Color8::color[.,.] rgbs, int stretchiness){
//return with {
//(.<= iv <= .): rgbs[iv/stretchiness] ;
//}:genarray([stretchiness,stretchiness], Color8::white());
//:genarray(shape(rgbs)* stretchiness, Color8::blue());
a = tod(stretchiness) / tod(shape(rgbs));
ret = with{
(. <= iv <= .): rgbs[toi(floor(tod(iv)/a))] ;
}:genarray([stretchiness,stretchiness], Color8::white());
return ret;
}*/
module Fractal_tier2;
use Structures: all;
use Numerical: all;
export all;
/** <!--*******************************************************************-->
*
* @fn int, complex escapeTimeAndValue(complex z, int depth)
*
* @brief iteratively computes c = c*c + z starting from c=z.
* terminates either when |c| > 2 or depth iterations
* have been performed.
* NB: Besides "norm" there exists "normSq" which computes |c|^2.
*
* @return number of iterations until termination and final complex value
*****************************************************************************/
int, complex escapeTimeAndValue(complex z, int depth)
{
i = 0;
c = z;
while(normSq(c) <= tod(4) && i<=depth){
c = c*c + z;
i++;
}
return (i,c);
/**
* fill in here...
*/
}
/** <!--*******************************************************************-->
*
* @fn int[*], complex[*] escapeTimeAndValue(complex[*] z, int depth)
*
* @brief maps escapeTimeAndValue to an entire array of complex numbers
*
*****************************************************************************/
inline
int[.,.], complex[.,.] escapeTimeAndValue( complex[.,.] plane, int depth)
{
z,y = with {
(. <= iv <= .){
i,c = escapeTimeAndValue(plane[iv], depth);
}: (i,c) ;
}:(genarray(shape(plane), 0),genarray(shape(plane), toc(0,0)));
return (z,y);
/**
* fill in here...
*/
}
/** <!--*******************************************************************-->
*
* @fn double normalizedIterationCount(int n, complex zn)
*
* @brief normalizes the iteration counts in "n" taking the distance of the
* corresponding final complex numbers from the origin into account.
* For corresponding elements in "n" and "zn", we have:
* 0.0 iff |zn| <= 2
* (n+1) - log2( log2( |zn|)) otherwise
*
* @return the normalized iteration counts
*****************************************************************************/
inline
double[*] normalizedIterationCount(int[*] n, complex[*] zn)
{
//return genarray([2,2], tod(0));
d = with {
(. <= iv <= .){
i = 0d;
if(normSq(zn[iv]) > tod(4)){
i = tod(n[iv] + 1) - log2(log2(norm(zn[iv])));
}
}: i ;
} : genarray(shape(n), 0d);
return d;
/**
* fill in here...
*/
}
/** <!--*******************************************************************-->
*
* @fn color[.,.] doubleArrayToRGB( double[.,.] a)
*
* @brief transforms an array of doubles into RGB values, by
* FIRST scaling them into values between 0.0 and 360.0, and
* THEN using them as HSB angles to choose a color.
* NB: Color8 provides a function Hsb2Rgb for the actual
* conversion.
*
* @return RGB values
*****************************************************************************/
inline
Color8::color[.,.] doubleArrayToRGB( double[.,.] a)
{
z = 0d;
for(i = 0; i<shape(a)[0]; i++){
for(j = 0; j<shape(a)[1]; j++){
if(a[[i,j]] > z){
z = a[[i,j]];
}
}
}
z = (z == 0d) ? 1d:z; // default to 1 so that we don't divide by zero
a = (a * 360d) / z;
ret = with{
(. <= iv <= .): Hsb2Rgb(toi(a[iv]),60,80) ;
}:genarray(shape(a), Color8::white());
//zz = genarray([1,1], Color8::white());
//c = genarray(shape(a), Color8::black());
return ret;
}
#define XRES 6
#define YRES 4
#define EXPAND 64
#define DEPTH 1024
#ifndef TIER
#define TIER 2
#endif
use Structures: all;
use SDLdisplay: all;
use StdIO : all;
use Fractal_tier1 : all;
#if TIER >= 2
use Fractal_tier2 : all;
#if TIER >= 3
use Stencil_tier3 : all;
#endif // 3
#endif // 2
inline
color[.,.] stretchRgb( color[.,.] pic, int stretch)
{
res = with {
(. <= iv <= .): pic[ iv / stretch];
} : genarray( shape(pic) * stretch, black());
return( res);
}
/* Main ******************************************************************** */
int main()
{
disp = initMultiDisplay( [2,2], EXPAND * [YRES,XRES] );
cmin = [toc( -2.2, 1.0)];
cmax = [toc( 0.8, -1.0)];
while( true) {
expand = EXPAND;
cur_shape = [YRES,XRES];
do {
plane = genComplexArray( cur_shape, cmin[[0]], cmax[[0]]);
ts = escapeTime( plane, DEPTH);
rgbs = intArrayToMonochrome( ts);
drawArrayMulti( disp, stretchRgb( rgbs, expand), [0,0]);
#if TIER >= 2
ts, vs = escapeTimeAndValue( plane, DEPTH);
nvs = normalizedIterationCount( ts, vs);
rgbs = doubleArrayToRGB( nvs);
drawArrayMulti( disp, stretchRgb( rgbs, expand), [0,1]);
#if TIER >= 3
rgbs = gaussBlur( gaussBlur25( rgbs));
drawArrayMulti( disp, stretchRgb( rgbs, expand), [1,0]);
rgbs = sobelEdges( rgbs);
drawArrayMulti( disp, stretchRgb( rgbs, expand), [1,1]);
#endif /* TIER >= 3 */
#endif /* TIER >= 2 */
expand = expand / 2;
cur_shape = 2 * cur_shape;
} while ( expand >= 1);
q, zoom_coords = getSelectionMulti( disp, [2,2]);
if (all( zoom_coords >= 0)) {
cmin = [plane[ zoom_coords[[0]]]] ++ cmin;
cmax = [plane[ zoom_coords[[1]]]] ++ cmax;
} else {
cmin = all( shape(cmin) > 1) ? drop( [1], cmin) : cmin;
cmax = all( shape(cmax) > 1) ? drop( [1], cmax) : cmax;
}
}
destroyDisplay( disp);
return(0);
}
......@@ -13,12 +13,20 @@ int[*] overSelWith( int[+] iv, int[*] a) {
} : genarray(reslen, a[iv[0]]);
}
// int[*] overSelCorrect( int[+] iv, int[*] a) {
// shape = drop([-1], shape(iv));
// return with {
// (. <= i <= .) : a[iv[i]];
// } : genarray(shape, ???);
// }
int main()
{
arr = iota(6) + 1;
a = reshape([2,3], arr);
a = reshape([3,2,1], arr);
print(overSelSet([[0,1], [0,2], [1,2]], a));
print(overSelWith([[0,1], [0,2], [1,2]], a));
// print(overSelWith([[0,1,0], [0,0,0], [1,1,1]], a));
// print(overSelCorrect([[0,1,0], [0,0,0], [1,1,1]], a));
print(overSelWith([[0,1,0], [0,0,0], [1,1,0]], a));
return 0;
}
\ No newline at end of file
......@@ -28,10 +28,12 @@ double[*] sumNth(int n, double[+] a) {
int main()
{
arr = tod(iota(12) + 1);
a = reshape([2,3,2], arr);
arr = tod(iota(30));
a = reshape([5,2,3], arr);
print(sumNth(0, a));
print(sumNth(1, a));
print(sumNth(2, a));
return 0;
}
\ No newline at end of file
use StdIO: all;
use Structures: all;
use Numerical: all;
complex[.,.] genComplexArray( int[2] shp, complex cmin, complex cmax)
{
realCmin = real(cmin);
imagCmin = imag(cmin);
diffReal = real(cmax) - realCmin;
diffImag = imag(cmax) - imagCmin;
realShapeSize = tod(shp[[0]] * shp[[1]] - 1);
imagShapeSize = tod(shp[[1]] * shp[[0]] - 1);
realStep = diffReal/realShapeSize;
imagStep = diffImag/imagShapeSize;
// arr = with {
// (. <= [i,j] <= .) : toc(realCmin + tod(i) * realStep, imagCmin + tod(j) * imagStep);
// } : genarray(shp, toc(0,0));
arr = with {
(. <= [i,j] <= .) : toc(realCmin + tod(j) * realStep, imagCmin + tod(i) * imagStep);
} : genarray(shp, toc(0,0));
print(arr);
return arr;
}
int main()
{
cmin = [toc( -2.2, 1.0)];
cmax = [toc( 0.8, -1.0)];
i = genComplexArray( [5, 5], cmin[[0]], cmax[[0]]);
return 0;
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment