ignore build and test products
[imager.git] / Mandelbrot / mandel.c
CommitLineData
92bda632
TC
1#include "imext.h"
2#include <stdlib.h>
3
4char evalstr[]="Mandlebrot renderer";
5
6/* Example Mandlebrot generator */
7
8/* input parameters
9 image is the image object.
10*/
11
12
13static
14int
15mandel(double x, double y, int max_iter) {
16 double xn, yn;
17 double xo, yo;
92bda632
TC
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
36void
37mandelbrot(i_img *im, double minx, double miny, double maxx, double maxy, int max_iter) {
38
af070d99 39 int i,x,y;
92bda632
TC
40 int idx;
41 double divx, divy;
42
43 i_color icl[256];
44 srand(12235);
45 for(i=1;i<256; i++) {
46 icl[i].rgb.r = 100+(int) (155.0*rand()/(RAND_MAX+1.0));
47 icl[i].rgb.g = 100+(int) (155.0*rand()/(RAND_MAX+1.0));
48 icl[i].rgb.g = 100+(int) (155.0*rand()/(RAND_MAX+1.0));
49 }
50
51 icl[0].rgb.r = 0;
52 icl[0].rgb.g = 0;
53 icl[0].rgb.g = 0;
54
55 if (maxx <= minx)
56 maxx = minx + 1.0;
57 if (maxy <= miny)
58 maxy = miny + 1.0;
59
60 divx = (maxx - minx) / im->xsize;
61 divy = (maxy - miny) / im->ysize;
62
63 for(y = 0; y < im->ysize; y ++) {
64 for(x = 0; x < im->xsize; x ++ ) {
65 idx = mandel(minx + x*divx , miny + y*divy, max_iter);
66 idx = idx % 256;
67 i_ppix(im,x,y,&icl[idx]);
68 }
69 }
70}