home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 432b.lha / EzLib / demo_src / frac / frac.c < prev    next >
C/C++ Source or Header  |  1990-11-10  |  1KB  |  73 lines

  1. /* this program generates psuedo-fractal mountains using a
  2.  * random midpoint displacement algorithim (sounds fancy eh :)
  3.  *
  4.  * It is taken from Pokorny & Gerald, _Computer Graphics_ (the chapter
  5.  * on fractals)
  6.  *
  7.  * D.B.G. August 90.
  8.  */
  9. #include <ezlib.h>
  10. #include <math.h>
  11.  
  12. #define MAX 640
  13.  
  14. /* NOTE:  It is **very** important to declare functions that return doubles
  15.  *      as double.  If not, Manx gleefully fucks up your code by converting
  16.  *      everything to int.
  17.  */
  18. double ran();
  19.  
  20. double line[MAX];
  21. double rug = 0.3;
  22. struct Screen *screen = NULL;
  23.  
  24. main()
  25. {
  26.  struct RastPort *rp;
  27.  int i, j;
  28.  double temp1;
  29.  
  30.  srand(time(0L));    /* I think this may be wrong here.... */
  31.  
  32.  line[0] = line[MAX-1] = (double) ((rand() % 50)+100);  /* seed it */
  33.  
  34.  fracline(0, MAX-1);   /* generate the "mountain" */
  35.  
  36.  screen = makescreen(HIRES, 2);    /* get a custom screen */
  37.  if (screen == NULL)
  38.    { closelibs(); MSG("Opening Screen failed.\n"); exit(10); }
  39.  
  40.  rp = &screen->RastPort;
  41.  
  42.  Move(rp, 0L, (int) line[0]);
  43.  for (i=1; i < MAX; i++)
  44.    Draw(rp, i, (int) line[i]);
  45.  
  46.  Delay(100);
  47.  killscreen(screen);
  48.  closelibs();
  49.  
  50.  exit(0);
  51. }
  52.  
  53. fracline(a, b)
  54.   int a, b;
  55. {
  56.  register int mid;
  57.  double temp1, temp2;
  58.  
  59.  if ( (b - a) > 1 ) {
  60.    mid = (a + b) / 2;
  61.  
  62.    temp1 = (double) ((line[a] + line[b]) / 2);
  63.    temp2 = (ran() * 2.0) - 1.0;
  64.    temp2 = temp2 * ((double)(b - a)) * rug;
  65.    line[mid] = (double) (temp1 + temp2);
  66.  
  67.    fracline(a, mid);
  68.    fracline(mid, b);
  69.  }
  70. }
  71.  
  72.  
  73.