home *** CD-ROM | disk | FTP | other *** search
/ Pulse 2 / pulse-2.zip / JULIA2.C < prev    next >
C/C++ Source or Header  |  1993-05-04  |  2KB  |  79 lines

  1. /* Optimized version. Draws a Julia picture. Check the code for variables
  2.    to modify if you want other pictures. */
  3.  
  4. /* BUG BUG BUG. After I had compiled and linked the mag I noticed a
  5.    small bug in the fractal text. It says that zrsqr = zr etc., but
  6.    of course this should be zrsqr = zr². I don't want to waste half
  7.    an hour compiling the mag all over when I can write it here
  8.    instead. Hope you didn't get too confused... */
  9.  
  10. #include <graphics.h>
  11.  
  12. main()
  13. {
  14.   int g_driver = 9, g_mode = 2; /* VGA-16 col, 640x480 */
  15.   int count, i;
  16.   float r0, r1, i0, i1, zreal, zimag, zimag2, ztemp, zsize;
  17.   float m, n, cr, ci, zrsqr, zisqr;
  18.  
  19.   /* Set up the graphics and colors. */
  20.  
  21.   initgraph(&g_driver, &g_mode, "");
  22.  
  23.   for(i = 0; i < 16; ++i)
  24.   {
  25.     setrgbpalette(i,0,i*2,i*4);
  26.     setpalette(i,i);
  27.   }
  28.  
  29.   /* Change these values for different parts of the current Julia Set. */
  30.  
  31.   r0 = -2.0; r1 = 2.0;
  32.   i0 = 1.5; i1 = -1.5;
  33.  
  34.   /* Change these values for different Julia Sets. Check out JULIA.EXS for
  35.      some examples. */
  36.  
  37.   cr = 0.32; ci = 0.043;
  38.  
  39.   for(n = 0; n < 480; ++n)
  40.   {
  41.     zimag2 = i0 + ((i1-i0)*n)/480;
  42.     for(m = 0; m < 480; ++m)
  43.     {
  44.       zreal = r0 + ((r1-r0)*m)/480;
  45.       zimag = zimag2;
  46.  
  47.       zrsqr = zreal*zreal;
  48.       zisqr = zimag*zimag;
  49.  
  50.       for(count = zsize = 0; ((count < 256) && (zsize < 4.0)); ++count)
  51.       {
  52.         ztemp = zreal;
  53.         zreal = zrsqr - zisqr + cr;
  54.         zimag = 2*ztemp*zimag + ci;
  55.         zrsqr = zreal*zreal;
  56.         zisqr = zimag*zimag;
  57.         zsize = zrsqr + zisqr;
  58.       }
  59.  
  60.       if (count != 256)
  61.       {
  62.     if (count > 14) count = 15;
  63.       putpixel(m+80, n, count);
  64.       }
  65.       if (kbhit())
  66.       {
  67.         closegraph();
  68.         return(0); /* Yes, close up and leave. */
  69.       }
  70.     }
  71.   }
  72.   /* Picture done. Wait for key, then shut down the graphics and leave
  73.      the program. */
  74.   getch();
  75.   closegraph();
  76.   return(0);
  77. }
  78.  
  79.