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

  1. //
  2.  
  3. //+-----------------------------------------------------------------+
  4.  
  5. //+ Program TRIGJUL.CPP                                             +
  6.  
  7. //+ Plots 10 Julia Set-like fractals from trigonometric functions.  +
  8.  
  9. //+ By F.A.A. Barbuto, February 6th 1994                            +
  10.  
  11. //+ E-Mail: BJ06@C53000.PETROBRAS.ANRJ.BR (Rio de Janeiro, BRAZIL). +
  12.  
  13. //+ Needs SVGA256.H and SVGA256.BGI, supports up to five screens.   +
  14.  
  15. //+ Suggested starting colours: 33, 16, 40, 145.                    +
  16.  
  17. //+-----------------------------------------------------------------+
  18.  
  19. //
  20.  
  21. #include <stdio.h>
  22.  
  23. #include "svga256.h"
  24.  
  25. #include <conio.h>
  26.  
  27. #include <graphics.h>
  28.  
  29. #include <math.h>
  30.  
  31. #include <complex.h>
  32.  
  33.  
  34.  
  35. //void far initgraph(int far *,int far *,char far *);
  36.  
  37.  
  38.  
  39. int Vid; // Global variable //
  40.  
  41.  
  42.  
  43. int huge DetectVGA256()
  44.  
  45. {
  46.  
  47.   printf("\nWhich video mode would you like to use? \n\n");
  48.  
  49.   printf(" 0 - 320x200x256\n");
  50.  
  51.   printf(" 1 - 640x400x256\n");
  52.  
  53.   printf(" 2 - 640x480x256\n");
  54.  
  55.   printf(" 3 - 800x600x256\n");
  56.  
  57.   printf(" 4 - 1024x768x256\n\n>");
  58.  
  59.   scanf("%d",&Vid);
  60.  
  61.   if((Vid<0) || (Vid)>4) Vid = 2;
  62.  
  63.   return Vid;
  64.  
  65. }
  66.  
  67.  
  68.  
  69. void main()
  70.  
  71. {
  72.  
  73.       double Pi=3.1415926535897932, Inv_e=1.0/2.7182818284590452;
  74.  
  75.       double xmin=-Pi, xmax=Pi, ymin=-Pi, ymax=Pi, fact=1.0;
  76.  
  77.       double ypy, x, y, yp, p, q, r, deltap, deltaq;
  78.  
  79.       int istart, maxiter;
  80.  
  81.       register int npix, npiy, np, nq, npy, k, ipen;
  82.  
  83.       complex c, z, I=(0.0,1.0);
  84.  
  85.       int graphdriver=DETECT, graphmode, index;
  86.  
  87.  
  88.  
  89.       clrscr();
  90.  
  91.       printf("\n                  Select a formulation:   \n\n\n");
  92.  
  93.       printf(" 1: Pi*cos(z)     2: Pi*sin(z)      3: (1/e + 0.1)*exp(z)\n\n");
  94.  
  95.       printf(" 4: z*sin(z)      5: 2.96*cos(z)    6: sin(z)*cos(z)\n\n");
  96.  
  97.       printf(" 7: 2.945*cos(z)  8: 2.935*cos(z)   9: cosh(z)\n\n");
  98.  
  99.       printf("10: sinh(z)       Default: exp(z)*cos(z) (Arara)");
  100.  
  101.       printf("\n\n>");
  102.  
  103.       scanf("%d",&index);
  104.  
  105.       printf("\n  Number of iterations (maximum=default=16000)? \n\n>");
  106.  
  107.       scanf("%d",&maxiter);
  108.  
  109.       printf("\n  Set starting colour (>=0 , <=256)?");
  110.  
  111.       printf("\n  (suggested: 33, 16, 40, 145)\n\n>");
  112.  
  113.       scanf("%d",&istart);
  114.  
  115.       if((maxiter>500) || (maxiter)<=0) maxiter = 500;
  116.  
  117.       if((istart)<=0 || (istart>256)) istart = 0;
  118.  
  119.       clrscr();
  120.  
  121.  
  122.  
  123.       installuserdriver("Svga256",DetectVGA256);
  124.  
  125.       initgraph(&graphdriver, &graphmode, "C:\\BORLANDC\\BGI");
  126.  
  127.  
  128.  
  129.       if (Vid == 0) { npix = 320; npiy = 200;}
  130.  
  131.       if (Vid == 1) { npix = 640; npiy = 400;}
  132.  
  133.       if (Vid == 2) { npix = 640; npiy = 480;}
  134.  
  135.       if (Vid == 3) { npix = 800; npiy = 600;}
  136.  
  137.       if (Vid == 4) { npix =1024; npiy = 768;}
  138.  
  139.       if ((Vid>4) || (Vid<0)) { npix = 640; npiy = 480;}
  140.  
  141.  
  142.  
  143.       if(fact>=1.0 || fact <=0.0)
  144.  
  145.     fact = 1.0;
  146.  
  147.       else {
  148.  
  149.     npix = (int)(npix*fact);
  150.  
  151.     npiy = (int)(npiy*fact);
  152.  
  153.       }
  154.  
  155.       ypy = (double)npiy - 0.5;
  156.  
  157.       deltap = (xmax-xmin)/(npix-1);
  158.  
  159.       deltaq = (ymax-ymin)/(npiy-1);
  160.  
  161.  
  162.  
  163.       if(ymin==-ymax)
  164.  
  165.      npy = npiy/2;
  166.  
  167.       else
  168.  
  169.      npy = npiy;
  170.  
  171.  
  172.  
  173.      cleardevice();
  174.  
  175.      for (np=0; np<=npix-1; np++) {
  176.  
  177.        x = xmin + (double)np*deltap;
  178.  
  179.        for (nq=0; nq<=npy-1; nq++) {
  180.  
  181.      y = ymin + (double)nq*deltaq;
  182.  
  183.      k  = 0;
  184.  
  185.      p = 0.0;
  186.  
  187.      q = 0.0;
  188.  
  189.      c = complex(p,q);
  190.  
  191.      z = complex(x,y);
  192.  
  193. //
  194.  
  195.      do {
  196.  
  197.        if    (index == 1) z = Pi*cos(z) + c;
  198.  
  199.        else
  200.  
  201.           if (index == 2) z = Pi*sin(z) + c;
  202.  
  203.        else
  204.  
  205.           //Much better than "The Science of Fractal Images" Fig 3.9,
  206.  
  207.           //pg. 161, uh?
  208.  
  209.           if (index == 3) z = (Inv_e + 0.1)*exp(z) + c;
  210.  
  211.        else
  212.  
  213.           if (index == 4) z = z*sin(z) + c;
  214.  
  215.        else
  216.  
  217.           //See a zoom at "The Science of Fractal Images" pg. 119,
  218.  
  219.           //Plate 19.
  220.  
  221.           if (index == 5) z = 2.96*cos(z) + c;
  222.  
  223.        else
  224.  
  225.           //This is quite similar to the picture shown in "The Science
  226.  
  227.           //of Fractal Images", pg.162, Fig. 3.10a.
  228.  
  229.           if (index == 6) z = sin(z)*cos(z) + c;
  230.  
  231.        else
  232.  
  233.           //See a zoom at "The Science of Fractal Images" pg. 119,
  234.  
  235.           //Plate 20.
  236.  
  237.           if (index == 7) z = 2.945*cos(z) + c;
  238.  
  239.        else
  240.  
  241.           //See a zoom at "The Science of Fractal Images" pg. 119,
  242.  
  243.           //Plate 21.
  244.  
  245.           if (index == 8) z = 2.935*cos(z) + c;
  246.  
  247.        else
  248.  
  249.           if (index == 9) z = cosh(z) + c;
  250.  
  251.        else
  252.  
  253.           if (index ==10) z = sinh(z) + c;
  254.  
  255.        else
  256.  
  257.           //Default: Arara
  258.  
  259.           if ((index < 1) || (index > 10))  z = cos(z)*exp(z) + c;
  260.  
  261.  
  262.  
  263.        r = abs(z);
  264.  
  265.        k++;
  266.  
  267.  
  268.  
  269.        if (r >= maxiter) {
  270.  
  271.          ipen = istart + k;
  272.  
  273.          if (ymin == -ymax) {
  274.  
  275.            putpixel(np,nq,ipen);
  276.  
  277.            putpixel(np,npiy-nq-1,ipen);
  278.  
  279.          }
  280.  
  281.          else
  282.  
  283.            putpixel(np,nq,ipen);
  284.  
  285.        }
  286.  
  287.  
  288.  
  289.        if (k == maxiter) {
  290.  
  291.          ipen = 33;
  292.  
  293.          if (ymin == -ymax) {
  294.  
  295.            ypy = double(npiy) - nq - 0.5;
  296.  
  297.            putpixel(np,ypy,ipen);
  298.  
  299.            putpixel(np,nq,ipen);
  300.  
  301.          }
  302.  
  303.          else
  304.  
  305.            putpixel(np,nq,33);
  306.  
  307.        }
  308.  
  309.  
  310.  
  311.        p = real(z);
  312.  
  313.        q = imag(z);
  314.  
  315.      } while (r <= maxiter && k<=maxiter);
  316.  
  317.        }
  318.  
  319.        if(kbhit()) break;
  320.  
  321.      }
  322.  
  323.      getch();
  324.  
  325.      closegraph();
  326.  
  327. }
  328.  
  329.