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

  1. #include <stdio.h>
  2.  
  3. #include <conio.h>
  4.  
  5. #include <graphics.h>
  6.  
  7. #include <math.h>
  8.  
  9. #include <complex.h>
  10.  
  11. #include "svga256.h"
  12.  
  13. //void far initgraph(int far *,int far *,char far *);
  14.  
  15. //    PROGRAMA MANDELBROT_SHADINGS_1
  16.  
  17. //
  18.  
  19. //    By Fausto A. A. Barbuto, December 1993
  20.  
  21. //    (After a program by Fausto Barbuto and Jay R. Hill, 1993)
  22.  
  23. //
  24.  
  25. //    Plots the Mandelbrot Set with period checking.
  26.  
  27. //
  28.  
  29. //    Period 1 (also known as Main Cardioid) ---> is in colour
  30.  
  31. //    shadings from green to blue;
  32.  
  33. //
  34.  
  35. //    Formulation:
  36.  
  37. //               abs(1.0 - sqrt(1.0-4.0*c)) <= 1.0
  38.  
  39. //
  40.  
  41. //    Period 2 (also known as Main Circle) ---> is in colour
  42.  
  43. //    shadings from blue to orange;
  44.  
  45. //
  46.  
  47. //    Formulation:
  48.  
  49. //               abs(4.0*(c+1.0)) <= 1.0
  50.  
  51. //
  52.  
  53. //    Period 3 Buds (no generic name: periodic detected by Fausto Barbuto
  54.  
  55. //                   and Jay R. Hill, 1993) ---> is in dark blue;
  56.  
  57. //    Formulation:
  58.  
  59. //               p1*p1 + q1*q1 <= 0.0089138
  60.  
  61. //               p1 = p + 0.12486
  62.  
  63. //               q1 = fabs(q) - 0.7439
  64.  
  65. //
  66.  
  67. //    Period 4 (no generic name: periodic detected by Fausto Barbuto, 1993)
  68.  
  69. //             ---> is in dark blue;
  70.  
  71. //    Formulation:
  72.  
  73. //               abs(c+1.309) <= 0.0588
  74.  
  75. //
  76.  
  77. //    Other MSet points which do not belong to any of these periods are
  78.  
  79. //    coloured in dark blue.
  80.  
  81. //
  82.  
  83. //    Escaped points (external to the MSet) are in shadings from blue to
  84.  
  85. //    yellow.
  86.  
  87. //
  88.  
  89. //    PS: Needs SVGA256.BGI Graphics Driver and SVGA screen with 1024 x 768
  90.  
  91. //        points and 256 colours.
  92.  
  93. //
  94.  
  95.  
  96.  
  97. int huge DetectVGA256()
  98.  
  99. {
  100.  
  101.   int Vid=4;
  102.  
  103.   return Vid;
  104.  
  105. }
  106.  
  107.  
  108.  
  109. void main()
  110.  
  111. {
  112.  
  113.       double pmin=-2.05, pmax=0.5, qmin=-1.125, qmax=1.125, fact=1.0;
  114.  
  115.       double ypy,x,y,xp,yp,p,q,p1,q1,ya,xkp1,ykp1,r,c_scr=0.833333;
  116.  
  117.       double deltap, deltaq, r1=8.9138e-3, r2=5.88e-2;
  118.  
  119.       double test1, test2, test3, test4;
  120.  
  121.       register int npix=1024, npiy=768;
  122.  
  123.       register long int maxiter=512;
  124.  
  125.       register int k, np, nq, npy, ipen, icolour;
  126.  
  127.       complex c;
  128.  
  129.       int graphdriver=DETECT, graphmode;
  130.  
  131.  
  132.  
  133.       installuserdriver("Svga256",DetectVGA256);
  134.  
  135.       initgraph(&graphdriver, &graphmode, "c:\\borlandc\\bgi");
  136.  
  137.       if(fact>=1.0 || fact <=0.0)
  138.  
  139.         fact = 1.0;
  140.  
  141.       else {
  142.  
  143.         npix = (int)(npix*fact);
  144.  
  145.         npiy = (int)(npiy*fact);
  146.  
  147.       }
  148.  
  149.       ypy = (double)npiy - 0.5;
  150.  
  151.       deltap = (pmax-pmin)/(npix-1);
  152.  
  153.       deltaq = (qmax-qmin)/(npiy-1);
  154.  
  155.  
  156.  
  157.       if(qmin==-qmax)
  158.  
  159.          npy = npiy/2;
  160.  
  161.       else
  162.  
  163.          npy = npiy;
  164.  
  165.  
  166.  
  167.      cleardevice();
  168.  
  169.      for (np=0; np<=npix-1; np++) {
  170.  
  171.        p = pmin + (double)np*deltap;
  172.  
  173.        for (nq=0; nq<=npy-1; nq++) {
  174.  
  175.          q = qmin + (double)nq*deltaq;
  176.  
  177.          k  = 0;
  178.  
  179.          x = 0.0;
  180.  
  181.          y = 0.0;
  182.  
  183.          c = complex(p,q);
  184.  
  185. //
  186.  
  187. //-------Checks limits for Period 1.
  188.  
  189. //
  190.  
  191.          test1 = 2.0;
  192.  
  193.          if ((p >= -7.55e-1) && (p <= 4.0e-1)) {
  194.  
  195.            if ((q >= -6.6e-1) && (q <= 6.6e-1))
  196.  
  197.               test1 = abs(1.0 - sqrt(1.0-4.0*c));
  198.  
  199.          }
  200.  
  201. //
  202.  
  203. //-------Checks limits for Period 2.
  204.  
  205. //
  206.  
  207.          test2 = 2.0;
  208.  
  209.          if ((p >= -1.255e0) && (p <= -7.45e-1)) {
  210.  
  211.            if ((q >= -2.55e-1) && (q <= 2.55e-1)) test2 = abs(4.0*(c+1.0));
  212.  
  213.          }
  214.  
  215. //
  216.  
  217. //-------Checks limits for Period Bud 3.
  218.  
  219. //
  220.  
  221.          test3 = 2.0;
  222.  
  223.          if ((p >= -2.4e-1) && (p <= 0.0e0)) {
  224.  
  225.            if ((q >= -8.5e-1) && (q <= 8.5e-1)) {
  226.  
  227.              p1 = p + 1.2486e-1;
  228.  
  229.              q1 = fabs(q) - 7.4396e-1;
  230.  
  231.              test3 = p1*p1 + q1*q1;
  232.  
  233.            }
  234.  
  235.          }
  236.  
  237. //
  238.  
  239. //-------Checks limits for Period 4.
  240.  
  241. //
  242.  
  243.          test4 = 2.0;
  244.  
  245.          if ((p >= -1.37e0) && (p <= -1.245e0)) {
  246.  
  247.            if ((q >= -6.1e-2) && (q <= 6.1e-2)) test4 = abs(c+1.309);
  248.  
  249.          }
  250.  
  251. //
  252.  
  253.          if((test1<=1.0) || (test2<=1.0) || (test3<=r1) || (test4<=r2)) {
  254.  
  255.             if (test3<=r1)  ipen=1;
  256.  
  257.             if (test4<=r2)  ipen=1;
  258.  
  259.             xp = c_scr*(double)np;
  260.  
  261.             yp = (double)nq;
  262.  
  263.             if (test1<=1.0) {
  264.  
  265.               ipen = 33 + (int)(15.0*test1);
  266.  
  267.             }
  268.  
  269.             if (test2<=1.0) {
  270.  
  271.               ipen = 42 - (int)(10.0*test2);
  272.  
  273.             }
  274.  
  275.             if (qmin == -qmax) {
  276.  
  277.               putpixel(xp,yp,ipen);
  278.  
  279.               putpixel(xp,npiy-yp-1.0,ipen);
  280.  
  281.             }
  282.  
  283.             else
  284.  
  285.               putpixel(xp,yp,ipen);
  286.  
  287.             }
  288.  
  289.          else {
  290.  
  291.             do {
  292.  
  293.              xkp1 = (x+y)*(x-y) + p;
  294.  
  295.              ya   = x*y;
  296.  
  297.              ykp1 = ya + ya + q;
  298.  
  299.              r    = xkp1*xkp1 + ykp1*ykp1;
  300.  
  301.              k++;
  302.  
  303. //
  304.  
  305. //    If R > M, points escape towards infinity.
  306.  
  307. //    Se R > M, os pontos escapam para o infinito.
  308.  
  309. //
  310.  
  311.              if (r >= maxiter) {
  312.  
  313.                ipen = 28 + k;
  314.  
  315.                xp = c_scr*(double)np;
  316.  
  317.                yp = (double)nq;
  318.  
  319.                if (qmin == -qmax) {
  320.  
  321.                  putpixel(xp,yp,ipen);
  322.  
  323.                  putpixel(xp,npiy-yp-1.0,ipen);
  324.  
  325.                }
  326.  
  327.                else
  328.  
  329.                  putpixel(xp,yp,ipen);
  330.  
  331.              }
  332.  
  333. //
  334.  
  335. //    Points which do not belong to any period: Colour=1=Blue
  336.  
  337. //    Pontos que nao pertencem a quaisquer periodos: coloracao negra.
  338.  
  339. //
  340.  
  341.              if (k == maxiter) {
  342.  
  343.                xp = c_scr*(double)np;
  344.  
  345.                yp = (double)nq;
  346.  
  347.                ipen = 1;
  348.  
  349.                if (qmin == -qmax) {
  350.  
  351.                  ypy = double(npiy) - yp - 0.5;
  352.  
  353.                  putpixel(xp,ypy,ipen);
  354.  
  355.                  putpixel(xp,yp,ipen);
  356.  
  357.                }
  358.  
  359.                else
  360.  
  361.                  putpixel(xp,yp,ipen);
  362.  
  363.              }
  364.  
  365. //
  366.  
  367. //    Returns if no convergence is achieved on either escape or atraction.
  368.  
  369. //    Retorna se nao houver convergencia na fuga ou atracao.
  370.  
  371. //
  372.  
  373.              x = xkp1;
  374.  
  375.              y = ykp1;
  376.  
  377.            } while (r <= maxiter && k<=maxiter);
  378.  
  379.          }
  380.  
  381.        }
  382.  
  383.        if(kbhit()) break;
  384.  
  385.      }
  386.  
  387.      getch();
  388.  
  389.      closegraph();
  390.  
  391. }
  392.  
  393.