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

  1. //
  2.  
  3. //+-------------------------------------------------------------+
  4.  
  5. //+ Mandelbrot Set via Continuous Potential Method with         +
  6.  
  7. //+ detection of Periods 1 & 2, January 30th 1994               +
  8.  
  9. //+ By Fausto Barbuto {BJ06@C53000.PETROBRAS.ANRJ.BR}, Brazil.  +
  10.  
  11. //+ Supports up to five screen types.                           +
  12.  
  13. //+ Needs SVGA256.BGI and SVGA256.H                             +
  14.  
  15. //+ REFERENCE:                                                  +
  16.  
  17. //+ Peitgen, H-O. & Saupe, D. - "The Science of Fractal Images" +
  18.  
  19. //+ Springer-Verlag, New York, 1988 ; Chap. 4, pp. 169-218      +
  20.  
  21. //+-------------------------------------------------------------+
  22.  
  23. //
  24.  
  25. #include <graphics.h>
  26.  
  27. #include <math.h>
  28.  
  29. #include <complex.h>
  30.  
  31. #include <stdio.h>
  32.  
  33. #include <conio.h>
  34.  
  35. #include "svga256.h"
  36.  
  37.  
  38.  
  39. int Vid; // Global variable
  40.  
  41.  
  42.  
  43. double MSetPot(double, double, int);
  44.  
  45.  
  46.  
  47. int huge DetectVGA256()
  48.  
  49. {
  50.  
  51.   printf("\nWhich video mode would you like to use? \n\n");
  52.  
  53.   printf(" 0 - 320x200x256\n");
  54.  
  55.   printf(" 1 - 640x400x256\n");
  56.  
  57.   printf(" 2 - 640x480x256\n");
  58.  
  59.   printf(" 3 - 800x600x256\n");
  60.  
  61.   printf(" 4 - 1024x768x256\n\n>");
  62.  
  63.   scanf("%d",&Vid);
  64.  
  65.   if((Vid<0) || (Vid)>4) Vid = 2;
  66.  
  67.   return Vid;
  68.  
  69. }
  70.  
  71.  
  72.  
  73. void main()
  74.  
  75. {
  76.  
  77.   int nx, ny, iy, ix, ipen, maxiter, iflag=0, iset;
  78.  
  79.   complex c;
  80.  
  81.   double xmin=-2.25, ymin=-1.25, xmax=0.75, ymax=1.25, cx, cy, potent;
  82.  
  83.   double diff=0.6482801, test1, test2;
  84.  
  85.   int graphdriver=DETECT, graphmode;
  86.  
  87.  
  88.  
  89.   clrscr();
  90.  
  91.   printf("\n Enter the number of allowed iterations (default=maximum=16000)\n\n>");
  92.  
  93.   scanf("%d",&maxiter);
  94.  
  95.   printf("\n Do you wish to plot Periods 1 & 2 with different colours?\n");
  96.  
  97.   printf("\n (1 = Yes ; <>1 = No) \n\n>");
  98.  
  99.   scanf("%d",&iset);
  100.  
  101.   if (iset != 1) iset = 0;
  102.  
  103.   if ((maxiter>=16000) || (maxiter<=0)) maxiter = 16000;
  104.  
  105.  
  106.  
  107.   installuserdriver("Svga256",DetectVGA256);
  108.  
  109.   initgraph(&graphdriver,&graphmode,"c:\\borlandc\\bgi");
  110.  
  111.   if (Vid == 0) { nx = 320; ny = 200; ymin = -1.250; ymax = 1.250;};
  112.  
  113.   if (Vid == 1) { nx = 640; ny = 400; ymin = -1.250; ymax = 1.250;};
  114.  
  115.   if (Vid == 2) { nx = 640; ny = 480; ymin = -1.125; ymax = 1.125;};
  116.  
  117.   if (Vid == 3) { nx = 800; ny = 600; ymin = -1.125; ymax = 1.125;};
  118.  
  119.   if (Vid == 4) { nx =1024; ny = 768; ymin = -1.125; ymax = 1.125;};
  120.  
  121.   for (iy=0;iy<=ny-1;iy++) {
  122.  
  123.     cy = ymin + iy*(ymax-ymin)/(ny-1);
  124.  
  125.     for (ix=0;ix<=nx-1;ix++) {
  126.  
  127.       cx = xmin + ix*(xmax-xmin)/(nx-1);
  128.  
  129.       c = complex(cx,cy);
  130.  
  131. //
  132.  
  133. //-------Checks limits for Period 1.
  134.  
  135. //
  136.  
  137.      test1 = 2.0;
  138.  
  139.      if ((cx >= -7.55e-1) && (cx <= 4.0e-1)) {
  140.  
  141.        if ((cy >= -6.6e-1) && (cy <= 6.6e-1))
  142.  
  143.           test1 = abs(1.0 - sqrt(1.0-4.0*c));
  144.  
  145.      }
  146.  
  147. //
  148.  
  149. //-------Checks limits for Period 2.
  150.  
  151. //
  152.  
  153.      test2 = 2.0;
  154.  
  155.      if ((cx >= -1.275e0) && (cx <= -7.45e-1)) {
  156.  
  157.        if ((cy >= -2.55e-1) && (cy <= 2.55e-1))
  158.  
  159.           test2 = abs(4.0*(c+1.0));
  160.  
  161.      }
  162.  
  163. //
  164.  
  165.       if (test1<=1.0) {
  166.  
  167.     potent = 0;
  168.  
  169.     iflag = 1;
  170.  
  171.     if (iset != 0) ipen = 126;
  172.  
  173.     else ipen = 32;
  174.  
  175.       }
  176.  
  177.       else if (test2<=1.0) {
  178.  
  179.     potent = 0;
  180.  
  181.     iflag = 1;
  182.  
  183.     if (iset != 0) ipen = 104;
  184.  
  185.     else ipen = 32;
  186.  
  187.       }
  188.  
  189.       else {
  190.  
  191.     potent = MSetPot(cx,cy,maxiter);
  192.  
  193.     iflag = 0;
  194.  
  195.       }
  196.  
  197.       if ((potent == 0.0) && (iflag==0))
  198.  
  199.     ipen = 32;
  200.  
  201.       else if ((potent !=0) && (iflag==0))
  202.  
  203.     ipen = (int)(33.0 + 15.0*(potent-33.0)/diff);
  204.  
  205.       putpixel(ix,iy,ipen);
  206.  
  207.     }
  208.  
  209.     if (kbhit()) break;
  210.  
  211.   }
  212.  
  213.   getch();
  214.  
  215.   closegraph();
  216.  
  217. }
  218.  
  219. double MSetPot(double cx, double cy, int maxiter)
  220.  
  221. {
  222.  
  223.    double x, y, x2, y2, temp, potential;
  224.  
  225.    int iter;
  226.  
  227.  
  228.  
  229.    x = cx;
  230.  
  231.    x2 = x*x;
  232.  
  233.    y = cy;
  234.  
  235.    y2 = y*y;
  236.  
  237.    iter = 0;
  238.  
  239.  
  240.  
  241.    do {
  242.  
  243.       temp = x2 - y2 + cx;
  244.  
  245.       y = 2.0*x*y + cy;
  246.  
  247.       x = temp;
  248.  
  249.       x2 = x*x;
  250.  
  251.       y2 = y*y;
  252.  
  253.       iter++;
  254.  
  255.    } while ((iter<maxiter) && ((x2+y2)<10000.0));
  256.  
  257.    if (iter<maxiter) potential = 0.5*log(x2+y2)/powl(2.0,iter);
  258.  
  259.    else potential = 0.0;
  260.  
  261.    return (potential);
  262.  
  263. }
  264.  
  265.