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

  1. //
  2.  
  3. //+----------------------------------------------------------------------+
  4.  
  5. //+ Program SIEGEL.CPP                                                   +
  6.  
  7. //+ By Fausto A. A. Barbuto, BJ06@C53000.PETROBRAS.ANRJ.BR               +
  8.  
  9. //+ Rio de Janeiro, BRAZIL, on May 13, 1994.                             +
  10.  
  11. //+                                                                      +
  12.  
  13. //+ Plots a Julia set of the type z -> z^2 + lambda*z, where lambda =    +
  14.  
  15. //+ exp(2*Pi*GM*i) and GM = (5^(1/2) - 1)/2 (Golden Mean). Siegel disks  +
  16.  
  17. //+ (are they in fact Siegel disks? I'm not sure) are shown in the inner +
  18.  
  19. //+ part of the plot.                                                    +
  20.  
  21. //+                                                                      +
  22.  
  23. //+ Case with z -> z^2 + 1.001*z*exp(2*Pi*i/20) added Sunday, May 15.    +
  24.  
  25. //+ (see Fig. 30, pg. 40)                                                +
  26.  
  27. //+                                                                      +
  28.  
  29. //+                                                                      +
  30.  
  31. //+ REFERENCE: Peitgen, H.-O. & Richter, P.H.: "The Beauty of Fractals", +
  32.  
  33. //+            Springer-Verlag, 1986, pg. 30-31, pg. 77 (Map 25).        +
  34.  
  35. //+                                                                      +
  36.  
  37. //+ Needs SVGA256.BGI and SVGA256.H (from SVGABG50 package by Jordan     +
  38.  
  39. //+ Hargrave).                                                           +
  40.  
  41. //+                                                                      +
  42.  
  43. //+ Authorized version for spanky.triumf.ca site (Vancouver, BC, CANADA).+
  44.  
  45. //+----------------------------------------------------------------------+
  46.  
  47. //
  48.  
  49. #include <stdio.h>
  50.  
  51. #include <conio.h>
  52.  
  53. #include <graphics.h>
  54.  
  55. #include <math.h>
  56.  
  57. #include <stdlib.h>
  58.  
  59. #include <complex.h>
  60.  
  61. #include "Svga256.h"
  62.  
  63.  
  64.  
  65. //void far initgraph(int far *,int far *,char far *);
  66.  
  67.  
  68.  
  69. int Vid;  //* Global variable *//
  70.  
  71.  
  72.  
  73. int huge DetectSVGA256()
  74.  
  75. {
  76.  
  77.   printf("\n  Which video mode would you like to use? \n\n");
  78.  
  79.   printf("  0 - 320x200x256\n");
  80.  
  81.   printf("  1 - 640x400x256\n");
  82.  
  83.   printf("  2 - 640x480x256\n");
  84.  
  85.   printf("  3 - 800x600x256\n");
  86.  
  87.   printf("  4 - 1024x768x256\n\n  ==> ");
  88.  
  89.   scanf("%d",&Vid);
  90.  
  91.   return Vid;
  92.  
  93. }
  94.  
  95.  
  96.  
  97. void main(void)
  98.  
  99. {
  100.  
  101.       complex lambda, i;
  102.  
  103.       double xmin, xmax, ymin, ymax, r, R1, I1, RL, IL;
  104.  
  105.       double x,y, x0,y0, GM, deltax,deltay, s1, s2, Pi=3.1415926535897932;
  106.  
  107.       register int npix, npiy, kcolor=1024, k, np, nq, ipen;
  108.  
  109.       int graphdriver=DETECT, graphmode, icase;
  110.  
  111.  
  112.  
  113.       clrscr();
  114.  
  115.       printf("\n  Program SIEGEL.CPP \n");
  116.  
  117.       printf("\n\n  By Fausto A. A. Barbuto, May 14, 1994");
  118.  
  119.       printf("\n  Rio de Janeiro, Federal Republic of Brazil");
  120.  
  121.       printf("\n  E-mail: BJ06@C53000.PETROBRAS.ANRJ.BR\n\n");
  122.  
  123.       printf("\n  Reference: Peitgen, H.-O., and Richter, P.H. :");
  124.  
  125.       printf("\n  'The Beauty of Fractals', Springer-Verlag, 1986");
  126.  
  127.       printf("\n  Section 2, pp. 27-32\n\n");
  128.  
  129.       printf("\n  Enter the case:\n");
  130.  
  131.       printf("\n  (1) z -> z*z +   lambda*z  [lambda=exp(2*Pi*i*GM)]\n");
  132.  
  133.       printf("                   [GM = Golden Mean = (5^(1/2) - 1.0)/2]\n");
  134.  
  135.       printf("\n  (2) z -> z*z + s*lambda*z (lambda=exp(2*Pi*i/20), s=1.001");
  136.  
  137.       printf("\n\n > ");
  138.  
  139.       scanf("%d",&icase);
  140.  
  141.       if ((icase < 1) || (icase > 2)) icase = 1;
  142.  
  143.       clrscr();
  144.  
  145.  
  146.  
  147.       installuserdriver("Svga256",DetectSVGA256);
  148.  
  149.       initgraph(&graphdriver, &graphmode, "C:\\Borlandc\\Bgi");
  150.  
  151.  
  152.  
  153.       if (Vid == 0) {npix = 320; npiy = 200;}
  154.  
  155.       if (Vid == 1) {npix = 640; npiy = 400;}
  156.  
  157.       if (Vid == 2) {npix = 640; npiy = 480;}
  158.  
  159.       if (Vid == 3) {npix = 800; npiy = 600;}
  160.  
  161.       if (Vid == 4) {npix =1024; npiy = 768;}
  162.  
  163.       if((Vid<0) || (Vid)>4) Vid = 2;
  164.  
  165.  
  166.  
  167.       GM = 0.5*(sqrt(5.0) - 1.0);  //* The Golden Mean *//
  168.  
  169.       i = complex(0.0,1.0);
  170.  
  171.  
  172.  
  173.       if (icase == 1) {
  174.  
  175.     lambda = exp(2.0*Pi*GM*i);
  176.  
  177.     xmin = -1.05; xmax = 1.75;
  178.  
  179.     ymin = -0.75; ymax = 1.45;
  180.  
  181.       }
  182.  
  183.       else {
  184.  
  185.     lambda = 1.001*exp(0.1*Pi*i);
  186.  
  187.     xmin = -1.75; xmax = 0.75;
  188.  
  189.     ymin = -1.40; ymax = 1.15;
  190.  
  191.       }
  192.  
  193.       RL = real(lambda);
  194.  
  195.       IL = imag(lambda);
  196.  
  197.       deltax = (xmax-xmin)/(npix-1);
  198.  
  199.       deltay = (ymax-ymin)/(npiy-1);
  200.  
  201.  
  202.  
  203.       cleardevice();
  204.  
  205.       for (np=0; np<=npix-1; np++) {
  206.  
  207.     x0 = xmin + (double)np*deltax;
  208.  
  209.     for (nq=0; nq<=npiy-1; nq++) {
  210.  
  211.     y0 = ymin + (double)nq*deltay;
  212.  
  213.     x = x0;
  214.  
  215.     y = y0;
  216.  
  217.     k  = 0;
  218.  
  219.  
  220.  
  221.      do {
  222.  
  223.        s1 = (x+RL);
  224.  
  225.        s2 = (y+IL);
  226.  
  227.        R1 = x*s1 - y*s2;
  228.  
  229.        I1 = x*s2 + y*s1;
  230.  
  231.        r = sqrt(R1*R1 + I1*I1);
  232.  
  233.        k++;
  234.  
  235. //*
  236.  
  237. //*        If r >= kcolor the point escapes towards infinity.
  238.  
  239. //*
  240.  
  241.        if (r >= kcolor) {
  242.  
  243.          ipen = 29 + k;
  244.  
  245.          putpixel(np,nq,ipen);
  246.  
  247.        }
  248.  
  249. //*
  250.  
  251. //*        Converging points.
  252.  
  253. //*
  254.  
  255.        if (k == kcolor) {
  256.  
  257. //*
  258.  
  259. //*          The colour shades of the "Siegel disks" (?) are defined here.
  260.  
  261. //*          (as function of the  distance between the present point [x,y]
  262.  
  263. //*          and the "invariant" point [x=0,y=0])
  264.  
  265. //*
  266.  
  267.          ipen = 17 + (int)(25.0*sqrt(x*x+y*y));
  268.  
  269.          putpixel(np,nq,ipen);
  270.  
  271.        }
  272.  
  273. //*
  274.  
  275. //*        Returns if no convergence's achieved on either escape or atraction.
  276.  
  277. //*
  278.  
  279.        x = R1;
  280.  
  281.        y = I1;
  282.  
  283.      } while (r<=kcolor && k<=kcolor);
  284.  
  285.        }
  286.  
  287.        if(kbhit()) break;
  288.  
  289.      }
  290.  
  291. //*
  292.  
  293. //*  Clean-up.
  294.  
  295. //*
  296.  
  297.      getch();
  298.  
  299.      closegraph();
  300.  
  301. }
  302.  
  303.