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

  1. //
  2.  
  3. //+-------------------------------------------------------------------+
  4.  
  5. //+ PROGRAMA PMAPS.CPP                                                +
  6.  
  7. //+ By Fausto A. A. Barbuto - Rio de Janeiro, BRAZIL, March 22, 1994. +
  8.  
  9. //+ E-mail: BJ06@C53000.PETROBRAS.ANRJ.BR                             +
  10.  
  11. //+                                                                   +
  12.  
  13. //+ Plots rational maps from complex functions of the form            +
  14.  
  15. //+ h(z,c) = [f(z,c)/g(z,c)]^2. 12 functions are proposed.            +
  16.  
  17. //+ Uses SVGA256.BGI and supports up to five video screens.           +
  18.  
  19. //+ I henceforward challenge the future users to create their own     +
  20.  
  21. //+ rational funcions.                                                +
  22.  
  23. //+-------------------------------------------------------------------+
  24.  
  25. //
  26.  
  27. #include <stdio.h>
  28.  
  29. #include <conio.h>
  30.  
  31. #include <graphics.h>
  32.  
  33. #include <math.h>
  34.  
  35. #include <complex.h>
  36.  
  37. #include "svga256.h"
  38.  
  39.  
  40.  
  41. //void far initgraph(int far *,int far *,char far *);
  42.  
  43.  
  44.  
  45. int Vid;  //Global variable
  46.  
  47.  
  48.  
  49. int huge DetectVGA256()
  50.  
  51. {
  52.  
  53.   printf("\nWhich video mode would you like to use? \n\n");
  54.  
  55.   printf(" 0 - 320x200x256\n");
  56.  
  57.   printf(" 1 - 640x400x256\n");
  58.  
  59.   printf(" 2 - 640x480x256\n");
  60.  
  61.   printf(" 3 - 800x600x256\n");
  62.  
  63.   printf(" 4 - 1024x768x256\n\n>");
  64.  
  65.   scanf("%d",&Vid);
  66.  
  67.   if((Vid<0) || (Vid)>4) Vid = 2;
  68.  
  69.   return Vid;
  70.  
  71. }
  72.  
  73.  
  74.  
  75. void main()
  76.  
  77. {
  78.  
  79.      double pmin, pmax, qmin, qmax;
  80.  
  81.      double ypy,x,y,xp,yp,p,q,r;
  82.  
  83.      double deltap, deltaq;
  84.  
  85.      register int npix, npiy, k, np, nq, npy, ipen;
  86.  
  87.      int index, istart;
  88.  
  89.      unsigned int maxiter;
  90.  
  91.      complex c, z, za, z2;
  92.  
  93.      int graphdriver=DETECT, graphmode;
  94.  
  95.  
  96.  
  97.      clrscr();
  98.  
  99.      printf("\n                Select a formulation:   \n\n");
  100.  
  101.      printf(" 1: ((z^2+c-1)/(2z+c-2))^2      2: ((z^2+c+1)/(z+c-2))^2\n");
  102.  
  103.      printf(" 3: ((z^2+c)/(z-c))^2           4: ((z+c)/(z-c))^2\n");
  104.  
  105.      printf(" 5: ((z^3-2z-c)/(2z+c))^2       6: ((z^2+c-2)/(2z+c+1))^2\n");
  106.  
  107.      printf(" 7: ((z^2+c-2)/(z+c+1))^2       8: ((z^2+2c+5)/(z^2+c))^2\n");
  108.  
  109.      printf(" 9: ((z^3+2c+9)/(z^2-3z-c))^2  10: ((z^2+2c-3)/(2z-c))^2\n");
  110.  
  111.      printf("11: ((z^2+2(c-1))/(z-c))^2     12: ((z^3+z^2-c)/(z-c))^2\n");
  112.  
  113.      printf("\n\n>");
  114.  
  115.      scanf("%d",&index);
  116.  
  117.      printf("\n  Number of iterations (maximum=default=16384)?");
  118.  
  119.      printf("\n  (suggested : 64 or higher)\n\n>");
  120.  
  121.      scanf("%d",&maxiter);
  122.  
  123.      printf("\n  Set starting colour (>=0 , <=256)?");
  124.  
  125.      printf("\n  (suggested: 33, 16, 40, 145)\n\n>");
  126.  
  127.      scanf("%d",&istart);
  128.  
  129.      if((maxiter>16384) || (maxiter)<=0) maxiter = 16384;
  130.  
  131.      if((istart)<=0 || (istart>256)) istart = 0;
  132.  
  133.      clrscr();
  134.  
  135.  
  136.  
  137.      installuserdriver("Svga256",DetectVGA256);
  138.  
  139.      initgraph(&graphdriver, &graphmode, "c:\\borlandc\\bgi");
  140.  
  141.  
  142.  
  143.      if (Vid == 0) { npix = 320; npiy = 200;}
  144.  
  145.      if (Vid == 1) { npix = 640; npiy = 400;}
  146.  
  147.      if (Vid == 2) { npix = 640; npiy = 480;}
  148.  
  149.      if (Vid == 3) { npix = 800; npiy = 600;}
  150.  
  151.      if (Vid == 4) { npix =1024; npiy = 768;}
  152.  
  153.      if ((Vid>4) || (Vid<0)) { npix = 640; npiy = 480;}
  154.  
  155.  
  156.  
  157. //---Sets plotting window for each rational function.
  158.  
  159.  
  160.  
  161.      if(index == 1) {pmin=-0.50; pmax=4.00;  qmin=-2.25; qmax=2.25;}
  162.  
  163.      if(index == 2) {pmin=-0.50; pmax=5.50;  qmin=-3.00; qmax=3.00;}
  164.  
  165.      if(index == 3) {pmin=-0.50; pmax=5.50;  qmin=-3.00; qmax=3.00;}
  166.  
  167.      if(index == 4) {pmin=-0.50; pmax=5.50;  qmin=-3.00; qmax=3.00;}
  168.  
  169.      if(index == 5) {pmin=-6.00; pmax=0.00;  qmin=-3.00; qmax=3.00;}
  170.  
  171.      if(index == 6) {pmin=-5.60; pmax=-5.5;  qmin=-0.04; qmax=0.04;}
  172.  
  173.      if(index == 7) {pmin=-8.25; pmax=-5.25; qmin=-1.50; qmax=1.50;}
  174.  
  175.      if(index == 8) {pmin=-2.00; pmax=-1.00; qmin=-0.50; qmax=0.50;}
  176.  
  177.      if(index == 9) {pmin=-14.0; pmax=-6.00; qmin=-4.00; qmax=4.00;}
  178.  
  179.      if(index ==10) {pmin=-6.00; pmax=-4.00; qmin=-0.80; qmax=0.80;}
  180.  
  181.      if(index ==11) {pmin=-6.00; pmax=-5.00; qmin=-0.50; qmax=0.50;}
  182.  
  183.      if(index ==12) {pmin=-12.0; pmax=-8.00; qmin=-2.00; qmax=2.00;}
  184.  
  185.  
  186.  
  187.      ypy = (double)npiy - 0.5;
  188.  
  189.      deltap = (pmax-pmin)/(npix-1);
  190.  
  191.      deltaq = (qmax-qmin)/(npiy-1);
  192.  
  193.  
  194.  
  195.      if(qmin==-qmax)
  196.  
  197.     npy = npiy/2;
  198.  
  199.      else
  200.  
  201.         npy = npiy;
  202.  
  203.  
  204.  
  205.      cleardevice();
  206.  
  207.      for (np=0; np<=npix-1; np++) {
  208.  
  209.        p = pmin + (double)np*deltap;
  210.  
  211.        for (nq=0; nq<=npy-1; nq++) {
  212.  
  213.      q = qmin + (double)nq*deltaq;
  214.  
  215.      k  = 0;
  216.  
  217.      x = 0.0;
  218.  
  219.      y = 0.0;
  220.  
  221.      c = complex(p,q);
  222.  
  223. //
  224.  
  225.      do {
  226.  
  227.        z = complex(x,y);
  228.  
  229.  
  230.  
  231. //         ***** Definition of the Functions *****
  232.  
  233.  
  234.  
  235. //---------See "The Science of Fractal Images", Fig. 4.26a, pg. 213
  236.  
  237. //---------I am sorry, but this is difficult to match at the screen
  238.  
  239. //---------without distortion. I suggest to use maxiter = 50.
  240.  
  241.        if(index== 1) za = (z*z + c - 1.0)/(2*(z - 1.0) + c);
  242.  
  243. //
  244.  
  245.        if(index== 2) za = (z*z + c + 1.0)/(z + c - 2.0);
  246.  
  247.        if(index== 3) za = (z*z + c)/(z - c);
  248.  
  249. //
  250.  
  251. //---------Dust.
  252.  
  253.        if(index== 4) za = (z + c)/(z - c);
  254.  
  255. //
  256.  
  257. //---------Arabesque.
  258.  
  259.        if(index== 5) za = (z*z*z - 2.0*z - c)/(2.0*z + c);
  260.  
  261. //
  262.  
  263. //---------What the hell is the Mandelbrot Set doing here???
  264.  
  265.        if(index== 6) za = (z*z + c - 2.0)/(2.0*z + c + 1.0);
  266.  
  267. //
  268.  
  269.        if(index== 7) za = (z*z + c - 2.0)/(z + c + 1.0);
  270.  
  271.        if(index== 8) {z2=z*z; za = (z2 + 2.0*c + 5.0)/(z2 + c);}
  272.  
  273.        if(index== 9) {z2=z*z; za = (z*z2 + 2.*c + 9.)/(z2 - 3.*z - c);}
  274.  
  275. //
  276.  
  277. //---------A Mandelbrot Set? Again??
  278.  
  279.        if(index==10) za = (z*z + 2.0*c - 3.0)/(2.0*z - c);
  280.  
  281. //
  282.  
  283. //---------A variation on Formula 10 shows another... guess what?
  284.  
  285.        if(index==11) za = (z*z + 2.0*(c - 1.0))/(z - c);
  286.  
  287. //
  288.  
  289.        if(index==12) za = (z*z*z + z*z - c)/(z - c);
  290.  
  291.  
  292.  
  293.        z = za*za;
  294.  
  295.        r = abs(z);
  296.  
  297.        k++;
  298.  
  299. //
  300.  
  301. //---------Plots escaping points.
  302.  
  303. //
  304.  
  305.        if (r >= maxiter) {
  306.  
  307.          ipen = istart + k;
  308.  
  309.          if (qmin == -qmax) {
  310.  
  311.            putpixel(np,nq,ipen);
  312.  
  313.            putpixel(np,npiy-nq-1.0,ipen);
  314.  
  315.          }
  316.  
  317.          else
  318.  
  319.            putpixel(np,nq,ipen);
  320.  
  321.        }
  322.  
  323. //
  324.  
  325. //---------Plots atracted points.
  326.  
  327. //
  328.  
  329.        if (k == maxiter) {
  330.  
  331.          ipen = 1;
  332.  
  333.         if (qmin == -qmax) {
  334.  
  335.           ypy = double(npiy) - nq - 0.5;
  336.  
  337.           putpixel(np,ypy,ipen);
  338.  
  339.           putpixel(np,nq,ipen);
  340.  
  341.         }
  342.  
  343.         else
  344.  
  345.           putpixel(np,nq,ipen);
  346.  
  347.        }
  348.  
  349. //
  350.  
  351. //---------Prepares a new iteration.
  352.  
  353. //
  354.  
  355.        x = real(z);
  356.  
  357.        y = imag(z);
  358.  
  359.      } while (r <= maxiter && k<=maxiter);
  360.  
  361.        }
  362.  
  363.        if(kbhit()) break;
  364.  
  365.      }
  366.  
  367. //
  368.  
  369. //---Clean up and end.
  370.  
  371. //
  372.  
  373.      getch();
  374.  
  375.      closegraph();
  376.  
  377. }
  378.  
  379.