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

  1. //
  2.  
  3. //+--------------------------------------------------------+
  4.  
  5. //+ Program TRIGON.CPP v. 0.2                              +
  6.  
  7. //+ Plots some fractals from trigonometric functions       +
  8.  
  9. //+ (Hmmm... I guess I've already seen this somewhere...)  +
  10.  
  11. //+ By F.A.A. Barbuto, February 4th 1994                   +
  12.  
  13. //+ Needs SVGA256.H and SVGA256.BGI, supports five screens +
  14.  
  15. //+                                                        +
  16.  
  17. //+ Suggested starting colours: 16, 33, 145.               +
  18.  
  19. //+--------------------------------------------------------+
  20.  
  21. //
  22.  
  23. #include <stdio.h>
  24.  
  25. #include "svga256.h"
  26.  
  27. #include <conio.h>
  28.  
  29. #include <graphics.h>
  30.  
  31. #include <math.h>
  32.  
  33. #include <complex.h>
  34.  
  35.  
  36.  
  37. //void far initgraph(int far *,int far *,char far *);
  38.  
  39.  
  40.  
  41. int Vid; // Global variable //
  42.  
  43.  
  44.  
  45. int huge DetectVGA256()
  46.  
  47. {
  48.  
  49.   printf("\nWhich video mode would you like to use? \n\n");
  50.  
  51.   printf(" 0 - 320x200x256\n");
  52.  
  53.   printf(" 1 - 640x400x256\n");
  54.  
  55.   printf(" 2 - 640x480x256\n");
  56.  
  57.   printf(" 3 - 800x600x256\n");
  58.  
  59.   printf(" 4 - 1024x768x256\n\n>");
  60.  
  61.   scanf("%d",&Vid);
  62.  
  63.   if((Vid<0) || (Vid)>4) Vid = 2;
  64.  
  65.   return Vid;
  66.  
  67. }
  68.  
  69.  
  70.  
  71. void main()
  72.  
  73. {
  74.  
  75.       double Pi=3.14159265357;
  76.  
  77.       double pmin=-Pi, pmax=Pi, qmin=-Pi, qmax=Pi, fact=1.0;
  78.  
  79.       double ypy, x, y, yp, p, q, r, deltap, deltaq;
  80.  
  81.       int istart, maxiter;
  82.  
  83.       register int npix, npiy, k, np, nq, npy, ipen;
  84.  
  85.       complex c, z, I=(0.0,1.0);
  86.  
  87.       int graphdriver=DETECT, graphmode, index;
  88.  
  89.  
  90.  
  91.       clrscr();
  92.  
  93.       printf("\n                  Select a formulation:   \n\n");
  94.  
  95.       printf("\n  1 = Pi*cos(z)    2 = Pi*sin(z)   3 = z*|z|*cos(z)\n");
  96.  
  97.       printf("\n  4 = z*z*sin(z)   5 = z*cos(z)    6 = sin(z)*cos(z)\n\n>");
  98.  
  99.       scanf("%d",&index);
  100.  
  101.       printf("\n  Number of iterations (maximum=default=16000)? \n\n>");
  102.  
  103.       scanf("%d",&maxiter);
  104.  
  105.       printf("\n  Set starting colour (>=0 , <=256)? \n\n>");
  106.  
  107.       scanf("%d",&istart);
  108.  
  109.       if((maxiter>16000) || (maxiter)<=0) maxiter = 16000;
  110.  
  111.       if((istart)<=0 || (istart>256)) istart = 0;
  112.  
  113.       clrscr();
  114.  
  115.  
  116.  
  117.       installuserdriver("Svga256",DetectVGA256);
  118.  
  119.       initgraph(&graphdriver, &graphmode, "C:\\BORLANDC\\BGI");
  120.  
  121.  
  122.  
  123.       if (Vid == 0) { npix = 320; npiy = 200;}
  124.  
  125.       if (Vid == 1) { npix = 640; npiy = 400;}
  126.  
  127.       if (Vid == 2) { npix = 640; npiy = 480;}
  128.  
  129.       if (Vid == 3) { npix = 800; npiy = 600;}
  130.  
  131.       if (Vid == 4) { npix =1024; npiy = 768;}
  132.  
  133.       if ((Vid>4) || (Vid<0)) { npix = 640; npiy = 480;}
  134.  
  135.  
  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.      z = complex(x,y);
  186.  
  187. //
  188.  
  189.      do {
  190.  
  191.        if    (index == 1) z = Pi*cos(z) + c;
  192.  
  193.        else
  194.  
  195.           if (index == 2) z = Pi*sin(z) + c;
  196.  
  197.        else
  198.  
  199.           if (index == 3) z = z*abs(z)*cos(z) + c;
  200.  
  201.        else
  202.  
  203.           if (index == 4) z = z*z*sin(z) + c;
  204.  
  205.        else
  206.  
  207.           if (index == 5) z = z*cos(z) + c;
  208.  
  209.        else
  210.  
  211.           if (index == 6) z = sin(z)*cos(z) + c;
  212.  
  213.        else
  214.  
  215.           if ((index < 1) || (index > 6))  z = Pi*cos(z) + c;
  216.  
  217.  
  218.  
  219.        r = abs(z);
  220.  
  221.        k++;
  222.  
  223.  
  224.  
  225.        if (r >= maxiter) {
  226.  
  227.          ipen = istart + k;
  228.  
  229.          if (qmin == -qmax) {
  230.  
  231.            putpixel(np,nq,ipen);
  232.  
  233.            putpixel(np,npiy-nq-1,ipen);
  234.  
  235.          }
  236.  
  237.          else
  238.  
  239.            putpixel(np,nq,ipen);
  240.  
  241.        }
  242.  
  243.  
  244.  
  245.        if (k == maxiter) {
  246.  
  247.          ipen = 33;
  248.  
  249.          if (qmin == -qmax) {
  250.  
  251.            ypy = double(npiy) - nq - 0.5;
  252.  
  253.            putpixel(np,ypy,ipen);
  254.  
  255.            putpixel(np,nq,ipen);
  256.  
  257.          }
  258.  
  259.          else
  260.  
  261.            putpixel(np,nq,33);
  262.  
  263.        }
  264.  
  265.  
  266.  
  267.        x = real(z);
  268.  
  269.        y = imag(z);
  270.  
  271.      } while (r <= maxiter && k<=maxiter);
  272.  
  273.        }
  274.  
  275.        if(kbhit()) break;
  276.  
  277.      }
  278.  
  279.      getch();
  280.  
  281.      closegraph();
  282.  
  283. }
  284.  
  285.