mandelbrot

view src/mbrot.c @ 0:4d85805eb875

mandelbrot initial import
author John Tsiombikas <nuclear@mutantstargoat.com>
date Tue, 19 Jun 2012 06:48:38 +0300
parents
children
line source
1 /*
2 A simple interactive mandelbrot fractal explorer
3 Copyright (C) John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "mbrot.h"
19 #include "palette.h"
21 static int mandelbrot(double cx, double cy, int iter);
23 void draw_mandelbrot(unsigned char *pix, int xsz, int ysz, struct area *area, int iter)
24 {
25 int i, j;
26 double x, y, dx, dy;
28 dx = area->width / (double)xsz;
29 dy = area->height / (double)ysz;
31 y = area->y;
32 for(i=0; i<ysz; i++) {
33 x = area->x;
34 for(j=0; j<xsz; j++) {
35 int res = mandelbrot(x, y, iter);
36 /* visualize according to the number of iterations
37 * required to determine membership in the mandelbrot set.
38 * Members of the set will have res = iter and so col = PAL_SIZE-1
39 */
40 int col = (PAL_SIZE - 1) * res / iter;
41 *pix++ = palette[col].r;
42 *pix++ = palette[col].g;
43 *pix++ = palette[col].b;
44 *pix++ = 0;
46 x += dx;
47 }
48 y += dy;
49 }
50 }
52 static int mandelbrot(double cx, double cy, int iter)
53 {
54 int i;
55 double zx, zy; /* complex number Z */
57 /* start with Z0 = C */
58 zx = cx;
59 zy = cy;
61 for(i=0; i<iter; i++) {
62 /* calculate Zn+1 = Zn * Zn + C */
63 double x = (zx * zx - zy * zy) + cx;
64 double y = (zy * zx + zx * zy) + cy;
65 zx = x;
66 zy = y;
68 /* escape if ||Z|| > 2 */
69 if((x * x + y * y) > 4.0) {
70 break;
71 }
72 }
74 return i;
75 }