home *** CD-ROM | disk | FTP | other *** search
/ Rat's Nest 1 / ratsnest1.iso / prgmming / c / spiral.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-19  |  4.3 KB  |  130 lines

  1. //
  2.  
  3. //+---------------------------------------------------------------------+
  4.  
  5. //+ PROGRAM SPIRAL.CPP                                                  +
  6.  
  7. //+ By Fausto A. A. Barbuto - Rio de Janeiro, BRAZIL, June 25, 1994.    +
  8.  
  9. //+ E-mail: BJ06@C53000.PETROBRAS.ANRJ.BR                               +
  10.  
  11. //+                                                                     +
  12.  
  13. //+ Plots spiral plant forms from a randomic complex number u = v + wi. +
  14.  
  15. //+                                                                     +
  16.  
  17. //+ REFERENCE: Pickover, Clifford A.: "Computers, Pattern, Chaos and    +
  18.  
  19. //+            Beauty", St. Martin's Press, New York, 1990;  pg. 203    +
  20.  
  21. //+            (Pseudocode 12.1)                                        +
  22.  
  23. //+                                                                     +
  24.  
  25. //+ Random number generator by Michael Sargent, msargent@moose.uvm.edu. +
  26.  
  27. //+ SVGA256.BGI graphic driver by Jordan Powell Hargrave.               +
  28.  
  29. //+                                                                     +
  30.  
  31. //+ Press ESC at any time to stop execution, or PAUSE to freeze.        +
  32.  
  33. //+                                                                     +
  34.  
  35. //+---------------------------------------------------------------------+
  36.  
  37. //
  38.  
  39. #include <stdio.h>
  40.  
  41. #include <conio.h>
  42.  
  43. #include <graphics.h>
  44.  
  45. #include <math.h>
  46.  
  47. #include <stdlib.h>
  48.  
  49. #include <complex.h>
  50.  
  51. #include "svga256.h"
  52.  
  53.  
  54.  
  55. double qsrandom(void);
  56.  
  57. int Vid;  //* Global variable *//
  58.  
  59.  
  60.  
  61. int huge DetectVGA256()
  62.  
  63. {
  64.  
  65.   printf("\n Which video mode would you like to use? \n\n");
  66.  
  67.   printf(" 0 - 320x200x256\n");
  68.  
  69.   printf(" 1 - 640x400x256\n");
  70.  
  71.   printf(" 2 - 640x480x256\n");
  72.  
  73.   printf(" 3 - 800x600x256\n");
  74.  
  75.   printf(" 4 - 1024x768x256\n\n> ");
  76.  
  77.   scanf("%d",&Vid);
  78.  
  79.   return Vid;
  80.  
  81. }
  82.  
  83.  
  84.  
  85. void main(void)
  86.  
  87. {
  88.  
  89.      double x, xmin, xmax, y, ymin, ymax, deltax, deltay;
  90.  
  91.      double v, w, Tolerance=10.0;
  92.  
  93.      complex a, b, c, d, e, f, u, z;
  94.  
  95.      int npix, npiy, nx, ny, k, icount;
  96.  
  97.      int graphdriver=DETECT, graphmode;
  98.  
  99.  
  100.  
  101.      clrscr();
  102.  
  103.      printf(" Program PLANTS.CPP \n\n\n");
  104.  
  105.      installuserdriver("Svga256",DetectVGA256);
  106.  
  107.      initgraph(&graphdriver, &graphmode, "C:\\BORLANDC\\BGI");
  108.  
  109.  
  110.  
  111.      if (Vid == 0) { npix = 320; npiy = 200;}
  112.  
  113.      if (Vid == 1) { npix = 640; npiy = 400;}
  114.  
  115.      if (Vid == 2) { npix = 640; npiy = 480;}
  116.  
  117.      if (Vid == 3) { npix = 800; npiy = 600;}
  118.  
  119.      if (Vid == 4) { npix =1024; npiy = 768;}
  120.  
  121.      if ((Vid>4) || (Vid<0)) { npix = 640; npiy = 480;}
  122.  
  123.  
  124.  
  125.      xmin=-2.0; xmax=2.00;
  126.  
  127.      ymin=-2.0; ymax=2.00;
  128.  
  129.      deltax = (xmax-xmin)/(npix-1);
  130.  
  131.      deltay = (ymax-ymin)/(npiy-1);
  132.  
  133.      icount = 0;
  134.  
  135.  
  136.  
  137.      do {
  138.  
  139.        cleardevice();
  140.  
  141.        if (icount == 0) {   //* As suggested on Dr. Pickover's book *//
  142.  
  143.      v = 0.35;
  144.  
  145.      w = 0.35;
  146.  
  147.        }
  148.  
  149.        else {               //* Random v & w *//
  150.  
  151.      w = qsrandom();    //* Not all combinations of v,w will be able to
  152.  
  153.      v = qsrandom();    //* generate beatiful structures. Be patient!
  154.  
  155.        }
  156.  
  157.        icount=1;
  158.  
  159.        u = complex(v,w);
  160.  
  161.  
  162.  
  163.        for (nx=0; nx<=npix-1; nx++) {
  164.  
  165.      x = xmin + (double)nx*deltax;
  166.  
  167.      for (ny=0; ny<=npiy-1; ny++) {
  168.  
  169.        y = ymin + (double)ny*deltay;
  170.  
  171.        a = complex(0.0,0.0);
  172.  
  173.        b = a; c = a; d = a; e = a; f = a;
  174.  
  175.        z = complex(x,y);
  176.  
  177.        for (k=1;k<=31;k++) {  //* Inner Loop *//
  178.  
  179.          a = z;
  180.  
  181.          e = a*a + u;
  182.  
  183.          c = e*e + u;
  184.  
  185.          b = c*c + u;
  186.  
  187.          f = b;
  188.  
  189.          a = f*f + u;
  190.  
  191.          z = a;
  192.  
  193.          if (abs(z) > Tolerance) break;
  194.  
  195.          else  //* A few gray contours are OK. *//
  196.  
  197.            putpixel(nx,ny,18+(int)(abs(z)));
  198.  
  199.        }
  200.  
  201.        if ((fabs(real(z)) < Tolerance) || (fabs(imag(z)) < Tolerance))
  202.  
  203.          putpixel(nx,ny,k+31); //* Hey, aren't these Julia sets??? *//
  204.  
  205.      }
  206.  
  207.      if (kbhit()) break;
  208.  
  209.        }
  210.  
  211.      } while (!kbhit());
  212.  
  213. //
  214.  
  215. //---Clean up and end.
  216.  
  217. //
  218.  
  219.      getch();
  220.  
  221.      closegraph();
  222.  
  223. }
  224.  
  225. //
  226.  
  227. //---qsrandom function by Mike Sargent.
  228.  
  229. //
  230.  
  231. double qsrandom(void)
  232.  
  233. {
  234.  
  235.    int random_integer, temp_integer;
  236.  
  237.    double random_double, temp_double;
  238.  
  239.     
  240.  
  241.    random_integer = random(RAND_MAX);
  242.  
  243.    random_double = (double)random_integer / RAND_MAX;
  244.  
  245.     
  246.  
  247.    temp_integer = random(30519);
  248.  
  249.    temp_double = (double)temp_integer / 1000000000L;
  250.  
  251.    random_double += temp_double;
  252.  
  253.  
  254.  
  255.    return(random_double);
  256.  
  257. }
  258.  
  259.