kellex

MandelBrot Set

Jan 7th, 2014
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <fcntl.h>
  4. #include <stdlib.h>
  5. void color();
  6.  
  7. main()
  8. {
  9.     int k;
  10.     int fd;
  11.     char buffer[100];
  12.  
  13.     double x,xx,y,cx,cy;
  14.     int iteration,hx,hy;
  15.     int itermax = 100;      /* how many iterations to do    */
  16.     double magnify=1.0;     /* no magnification     */
  17.     int hxres = 1500;       /* horizonal resolution     */
  18.     int hyres = 1500;       /* vertical resolution      */
  19.  
  20.     char pic[hyres][hxres][3];
  21.  
  22.     /* header for PPM output */
  23.     printf("%d %d\n255\n",hxres,hyres);
  24.  
  25.     for (hy=1;hy<=hyres;hy++)  {
  26.         for (hx=1;hx<=hxres;hx++)  {
  27.             cx = (((float)hx)/((float)hxres)-0.5)/magnify*3.0-0.7;
  28.             cy = (((float)hy)/((float)hyres)-0.5)/magnify*3.0;
  29.             x = 0.000001; y = 0.000001;
  30.             for (iteration=1;iteration<itermax;iteration++)  {
  31.                 xx = x*x-y*y+cx;
  32.                 y = 2.0*x*y+cy;
  33.                 x = xx;
  34.                 if (x*x+y*y>100.0){
  35.                     iteration = 9999999;
  36.                     int inset = 0;
  37.                     k = iteration;
  38.                 }
  39.             }
  40.             if (iteration<9999999){
  41.                 color(35,0,0);
  42.                 pic[hy][hx][0] = 0;
  43.                 pic[hy][hx][1] = 0;
  44.                 pic[hy][hx][2] = 0;
  45.             }
  46.             else {
  47.                 color(42,0,0);
  48.                 pic[hy][hx][0] = k * 255;
  49.                 pic[hy][hx][1] = k * 255 / 2;
  50.                 pic[hy][hx][2] = k * 255 / 2;
  51.             }
  52.         }
  53.     }
  54.  
  55.   if ((fd = open("mand.tga", O_RDWR+O_CREAT, 0)) == -1)
  56.   {
  57.     printf("error opening file\n");
  58.     exit(1);
  59.   }
  60.   buffer[0] = 0;
  61.   buffer[1] = 0;
  62.   buffer[2] = 2;
  63.   buffer[8] = 0; buffer[9] = 0;
  64.   buffer[10] = 0; buffer[11] = 0;
  65.   buffer[12] = (hxres & 0x00FF); buffer[13] = (hxres & 0xFF00) >> 8;
  66.   buffer[14] = (hyres & 0x00FF); buffer[15] = (hyres & 0xFF00) >> 8;
  67.   buffer[16] = 24;
  68.   buffer[17] = 0;
  69.   write(fd, buffer, 18);
  70.   write(fd, pic, hxres*hyres*3);
  71.   close(fd);
  72. }
  73.  
  74. void color(int red, int green, int blue)  {
  75.     fputc((char)red,stdout);
  76.     fputc((char)green,stdout);
  77.     fputc((char)blue,stdout);
  78. }
Advertisement
Add Comment
Please, Sign In to add comment