| 1 |
#include "imext.h" |
| 2 |
#include <stdlib.h> |
| 3 |
|
| 4 |
char evalstr[]="Mandlebrot renderer"; |
| 5 |
|
| 6 |
/* Example Mandlebrot generator */ |
| 7 |
|
| 8 |
/* input parameters |
| 9 |
image is the image object. |
| 10 |
*/ |
| 11 |
|
| 12 |
|
| 13 |
static |
| 14 |
int |
| 15 |
mandel(double x, double y, int max_iter) { |
| 16 |
double xn, yn; |
| 17 |
double xo, yo; |
| 18 |
int iter = 1; |
| 19 |
/* Z(n+1) = Z(n) ^2 + c */ |
| 20 |
|
| 21 |
/* printf("(%.2f, %.2f) -> \n", x,y); */ |
| 22 |
|
| 23 |
xo = x; |
| 24 |
yo = y; |
| 25 |
|
| 26 |
while( xo*xo+yo*yo <= 10 && iter < max_iter) { |
| 27 |
xn = xo*xo-yo*yo + x; |
| 28 |
yn = 2*xo*yo + y; |
| 29 |
xo=xn; |
| 30 |
yo=yn; |
| 31 |
iter++; |
| 32 |
} |
| 33 |
return (iter == max_iter)?0:iter; |
| 34 |
} |
| 35 |
|
| 36 |
void |
| 37 |
mandelbrot(i_img *im, double minx, double miny, double maxx, double maxy, int max_iter) { |
| 38 |
|
| 39 |
int i; |
| 40 |
i_img_dim x,y; |
| 41 |
int idx; |
| 42 |
double divx, divy; |
| 43 |
|
| 44 |
i_color icl[256]; |
| 45 |
srand(12235); |
| 46 |
for(i=1;i<256; i++) { |
| 47 |
icl[i].rgb.r = 100+(int) (156.0*rand()/(RAND_MAX+1.0)); |
| 48 |
icl[i].rgb.g = 100+(int) (156.0*rand()/(RAND_MAX+1.0)); |
| 49 |
icl[i].rgb.b = 100+(int) (156.0*rand()/(RAND_MAX+1.0)); |
| 50 |
} |
| 51 |
|
| 52 |
icl[0].rgb.r = 0; |
| 53 |
icl[0].rgb.g = 0; |
| 54 |
icl[0].rgb.b = 0; |
| 55 |
|
| 56 |
if (maxx <= minx) |
| 57 |
maxx = minx + 1.0; |
| 58 |
if (maxy <= miny) |
| 59 |
maxy = miny + 1.0; |
| 60 |
|
| 61 |
divx = (maxx - minx) / im->xsize; |
| 62 |
divy = (maxy - miny) / im->ysize; |
| 63 |
|
| 64 |
for(y = 0; y < im->ysize; y ++) { |
| 65 |
for(x = 0; x < im->xsize; x ++ ) { |
| 66 |
idx = mandel(minx + x*divx , miny + y*divy, max_iter); |
| 67 |
idx = idx % 256; |
| 68 |
i_ppix(im,x,y,&icl[idx]); |
| 69 |
} |
| 70 |
} |
| 71 |
} |