8 enum { ncolors = 600 };
10 double xmin = -2, xmax = 1;
11 double ymin = -1, ymax = 1;
17 extern Cursor reading;
18 uchar colors[3 * (ncolors + 1)];
21 convx(Rectangle *r, int x)
23 return (xmax - xmin) * (x - r->min.x) / (r->max.x - r->min.x) + xmin;
27 convy(Rectangle *r, int y)
29 return (ymax - ymin) * (r->max.y - y) / (r->max.y - r->min.y) + ymin;
35 draw(screen, Rect(x, y, x + 1, y + 1), display->black, nil, ZP);
39 iterate(double x, double y)
42 double zx, zy, zx2, zy2, v;
44 zx = zy = zx2 = zy2 = 0;
45 for(i = 0; i < 100; i++){
51 v = 2 + i - log(log(sqrt(zx2 + zy2)) / log(i + 2)) / 0.69314718;
52 return (int)(v * 1000) % ncolors;
66 setcursor(mctl, &reading);
68 for(y = screen->r.min.y; y < screen->r.max.y; y++)
69 for(x = screen->r.min.x; x < screen->r.max.x; x++){
72 q = colors + 3 * iterate(convx(&screen->r, x), convy(&screen->r, y));
80 loadimage(image, image->r, imagedata, Dx(image->r) * Dy(image->r) * 3);
81 draw(screen, screen->r, image, nil, screen->r.min);
82 flushimage(display, 1);
83 unlockdisplay(display);
96 double xmin_, xmax_, ymin_, ymax_;
99 if(r.min.x == 0 && r.min.y == 0 && r.max.x == 0 && r.max.y == 0)
101 xmin_ = convx(&screen->r, r.min.x);
102 xmax_ = convx(&screen->r, r.max.x);
103 ymin_ = convy(&screen->r, r.max.y);
104 ymax_ = convy(&screen->r, r.min.y);
132 if(recv(mctl->resizec, &l) < 1)
136 if(getwindow(display, Refnone) < 0)
137 sysfatal("getwindow: %r");
140 image = allocimage(display, screen->r, RGB24, 0, DBlack);
141 imagedata = malloc(3 * Dx(screen->r) * Dy(screen->r));
153 for(p = colors + 3, h = 0; p < colors + nelem(colors); h++){
154 x = 0xFF - abs(h % (ncolors / 3) - ncolors / 6) * 0xFF / (ncolors / 6);
159 }else if(h < ncolors/3){
163 }else if(h < ncolors/2){
167 }else if(h < 2*ncolors/3){
171 }else if(h < 5*ncolors/6){
189 if(initdraw(nil, nil, "mandelbrot") < 0)
190 sysfatal("initdraw: %r");
191 display->locking = 1;
192 unlockdisplay(display);
193 if((mctl = initmouse(nil, screen)) == nil)
194 sysfatal("initmouse: %r");
196 image = allocimage(display, screen->r, RGB24, 0, DBlack);
197 imagedata = malloc(3 * Dx(screen->r) * Dy(screen->r));
199 proccreate(redrawproc, nil, mainstacksize);
200 threadcreate(resizethread, nil, mainstacksize);
203 if(mctl->buttons & 4){
204 lockdisplay(display);
205 rc = menuhit(3, mctl, &menu, nil);
206 unlockdisplay(display);
230 {0xff, 0x80, 0xff, 0x80, 0xff, 0x00, 0xfe, 0x00,
231 0xff, 0x00, 0xff, 0x80, 0xff, 0xc0, 0xef, 0xe0,
232 0xc7, 0xf0, 0x03, 0xf0, 0x01, 0xe0, 0x00, 0xc0,
233 0x03, 0xff, 0x03, 0xff, 0x03, 0xff, 0x03, 0xff, },
234 {0x00, 0x00, 0x7f, 0x00, 0x7e, 0x00, 0x7c, 0x00,
235 0x7e, 0x00, 0x7f, 0x00, 0x6f, 0x80, 0x47, 0xc0,
236 0x03, 0xe0, 0x01, 0xf0, 0x00, 0xe0, 0x00, 0x40,
237 0x00, 0x00, 0x01, 0xb6, 0x01, 0xb6, 0x00, 0x00, }