random

:-)
git clone read: git://git.margiolis.net/random.git
Log | Files | Refs | LICENSE

mandelbrot.c (909B)


      1 #include <math.h>
      2 #include <stdio.h>
      3 
      4 #define IMGW 1920
      5 #define IMGH 1080
      6 #define MAXN 1024
      7 #define MINR (-1.5)
      8 #define MAXR 0.7
      9 #define MINI (-1.0)
     10 #define MAXI 1.0
     11 
     12 #define TOREAL(x) ((x) * ((MAXR - MINR) / IMGW) + MINR)
     13 #define TOIMGN(y) ((y) * ((MAXI - MINI) / IMGH) + MINI)
     14 
     15 static int
     16 fmandelbrot(double cr, double ci)
     17 {
     18 	double zr = 0.0, zi = 0.0, tmp;
     19 	int i = 0;
     20 
     21 	for (; i < MAXN && (zr * zr + zi * zi) < 4.0; i++) {
     22 		tmp = zr * zr - zi * zi + cr;
     23 		zi = 2.0 * zr * zi + ci;
     24 		zr = tmp;
     25 	}
     26 
     27 	return i;
     28 }
     29 
     30 int
     31 main(int argc, char *argv[])
     32 {
     33 	double cr, ci;
     34 	int x, y, n, r, g, b;
     35 
     36 	printf("P3\n%d %d\n256\n", IMGW, IMGH);
     37 	for (y = 0; y < IMGH; y++) {
     38 		for (x = 0; x < IMGW; x++) {
     39 			cr = TOREAL(x);
     40 			ci = TOIMGN(y);
     41 			n = fmandelbrot(cr, ci);
     42 			r = (n % 256);
     43 			g = ((int)(n * sinf(M_PI)) % 256);
     44 			b = ((n * 3) % 256);
     45 			printf("%d %d %d ", r, g, b);
     46 		}
     47 		printf("\n");
     48 	}
     49 
     50 	return 0;
     51 }