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

  1. //+-------------------------------------------------------------------+
  2.  
  3. //+ Program SHADES3.CPP - By Fausto A. A. Barbuto, July 3, 1994       +
  4.  
  5. //+ E-Mail: BJ06@C53000.PETROBRAS.ANRJ.BR                             +
  6.  
  7. //+ Uses Michael Sargent's graphical functions and SVGA256.BGI driver +
  8.  
  9. //+ by Jordan Powell Hargrave.                                        +
  10.  
  11. //+                                                                   +
  12.  
  13. //+ Based on the formula z -> exp(i*Theta)*z*(1-z),                   +
  14.  
  15. //+ where Theta is a fixed angle (c*Pi) and i = sqrt(-1).             +
  16.  
  17. //+                                                                   +
  18.  
  19. //+ REFERENCE: Pickover, Clifford A.: "Computers, Pattern, Chaos and  +
  20.  
  21. //+            Beauty", St. Martin's Press, pp. 200-202, Sept. 1991.  +
  22.  
  23. //+                                                                   +
  24.  
  25. //+ Press the '+' key at anytime to rotate palettes; any other key    +
  26.  
  27. //+ will fade the screen out.                                         +
  28.  
  29. //+-------------------------------------------------------------------+
  30.  
  31. #include <stdio.h>
  32.  
  33. #include <conio.h>
  34.  
  35. #include <graphics.h>
  36.  
  37. #include <math.h>
  38.  
  39. #include <complex.h>
  40.  
  41. #include "svga256.h"
  42.  
  43. #include <dos.h>
  44.  
  45.  
  46.  
  47. //*--Graphical routines by Michael Sargent, 1994.--*//
  48.  
  49. void getpalette(void);
  50.  
  51. void cycle(void);
  52.  
  53. void fade(void);
  54.  
  55. //*------------------------------------------------*//
  56.  
  57.  
  58.  
  59. int Vid;
  60.  
  61. char palette[256][3];
  62.  
  63.  
  64.  
  65. int huge DetectVGA256()
  66.  
  67. {
  68.  
  69.   printf("\n Which video mode would you like to use? \n\n");
  70.  
  71.   printf(" 0 - 320x200x256\n");
  72.  
  73.   printf(" 1 - 640x400x256\n");
  74.  
  75.   printf(" 2 - 640x480x256\n");
  76.  
  77.   printf(" 3 - 800x600x256\n");
  78.  
  79.   printf(" 4 - 1024x768x256\n\n> ");
  80.  
  81.   scanf("%d",&Vid);
  82.  
  83.   if ((Vid>4) || (Vid<0)) Vid = 2;
  84.  
  85.   return Vid;
  86.  
  87. }
  88.  
  89.  
  90.  
  91. void main(void)
  92.  
  93. {
  94.  
  95.      float xmin=-1.0, xmax=2.0, ymin=-1.5, ymax=1.5, ST, CT, a, b;
  96.  
  97.      float x, y, r, deltax, deltay, Theta, Rz, Iz, Mult, Const, aa, bb;
  98.  
  99.      register int npix, npiy, k, nx, ny;
  100.  
  101.      long int maxiter;
  102.  
  103.      complex I;
  104.  
  105.      char ch;
  106.  
  107.      int graphdriver=DETECT, graphmode, ipen, iopt;
  108.  
  109.  
  110.  
  111.      clrscr();
  112.  
  113.      printf("\n Program SHADES3\n");
  114.  
  115.      printf(" [z -> exp(i*Theta)*z*(1-z) , i=sqrt(-1)]\n\n");
  116.  
  117.      printf(" Enter a multiplier for the angle Pi=3.1415... radians\n");
  118.  
  119.      printf(" (Suggested: 0.15, 0.25, 0.333..., -0.03457834, 0.95)\n\n > ");
  120.  
  121.      scanf("%f",&Mult);
  122.  
  123.      printf("\n Enter the maximum number of iterations\n");
  124.  
  125.      printf(" (Suggested: 64 or higher)\n\n > ");
  126.  
  127.      scanf("%li",&maxiter);
  128.  
  129.      if (maxiter <= 0) maxiter = 100;
  130.  
  131.      printf("\n Inner shadings: \n");
  132.  
  133.      printf("                 Black to light gray: 1\n");
  134.  
  135.      printf("                 Light gray to black: 2\n\n > ");
  136.  
  137.      scanf("%d",&iopt);
  138.  
  139.      if ((iopt < 1) || (iopt > 2)) iopt = 1;
  140.  
  141.      clrscr();
  142.  
  143.      installuserdriver("Svga256",DetectVGA256);
  144.  
  145.      initgraph(&graphdriver, &graphmode, "C:\\BORLANDC\\BGI");
  146.  
  147.  
  148.  
  149.      if (Vid == 0) { npix = 320; npiy = 200;}
  150.  
  151.      if (Vid == 1) { npix = 640; npiy = 400;}
  152.  
  153.      if (Vid == 2) { npix = 640; npiy = 480;}
  154.  
  155.      if (Vid == 3) { npix = 800; npiy = 600;}
  156.  
  157.      if (Vid == 4) { npix =1024; npiy = 768;}
  158.  
  159.  
  160.  
  161.      I = complex(0.0,1.0);
  162.  
  163.      Const = 35.0;
  164.  
  165.      Theta = 3.1415926535897932*Mult;
  166.  
  167.      CT = real(exp(I*Theta));
  168.  
  169.      ST = imag(exp(I*Theta));
  170.  
  171.      deltax = (xmax-xmin)/(npix-1);
  172.  
  173.      deltay = (ymax-ymin)/(npiy-1);
  174.  
  175.  
  176.  
  177.      cleardevice();
  178.  
  179.      for (nx=0; nx<=npix-1; nx++) {
  180.  
  181.        x = xmin + (float)nx*deltax;
  182.  
  183.        for (ny=0; ny<=npiy-1; ny++) {
  184.  
  185.      y = ymin + (float)ny*deltay;
  186.  
  187.      k  = 0;
  188.  
  189.      aa = x;
  190.  
  191.      bb = y;
  192.  
  193.      do {
  194.  
  195.        a = aa - (aa+bb)*(aa-bb);
  196.  
  197.        b = bb*(1.0 - aa - aa);
  198.  
  199.        Rz = CT*a - ST*b;
  200.  
  201.        Iz = ST*a + CT*b;
  202.  
  203.        r = sqrt(Rz*Rz + Iz*Iz);
  204.  
  205.        k++;
  206.  
  207. //
  208.  
  209. //         Escaped points.
  210.  
  211. //
  212.  
  213.        if (r >= maxiter) putpixel(nx,ny,(29+k));
  214.  
  215. //
  216.  
  217. //         Internal points.
  218.  
  219. //
  220.  
  221.        if (k == maxiter) {
  222.  
  223.          if (fabs(Mult) <= 0.05) Const = 100;
  224.  
  225.          if (fabs(Mult) <= 0.01) Const = 500;
  226.  
  227.          if (iopt == 1) {       //* Black to light gray *//
  228.  
  229.            ipen = (29 - (int)(Const*sqrt(Rz*Rz + Iz*Iz)));
  230.  
  231.            if (ipen < 16) ipen = 0;
  232.  
  233.          }
  234.  
  235.          else if (iopt == 2) {  //* Light gray to black *//
  236.  
  237.            ipen = (int)(16.0 + Const*sqrt(Rz*Rz + Iz*Iz));
  238.  
  239.            if (ipen > 29) ipen = 29;
  240.  
  241.          }
  242.  
  243.          putpixel(nx,ny,ipen);
  244.  
  245.        }
  246.  
  247.        aa = Rz;
  248.  
  249.        bb = Iz;
  250.  
  251.      } while ((r<=maxiter) && (k<=maxiter));
  252.  
  253.        }
  254.  
  255.        if(kbhit()) break;
  256.  
  257.      }
  258.  
  259.      ch = getche();
  260.  
  261.      if (ch == '+') { //* Rotate palettes *//
  262.  
  263.       getpalette();
  264.  
  265.       cycle();
  266.  
  267.      }
  268.  
  269.      else fade();
  270.  
  271.      closegraph();
  272.  
  273. }
  274.  
  275.  
  276.  
  277. //+======================+
  278.  
  279. // Color-cycling routine +
  280.  
  281. //+======================+
  282.  
  283. #pragma warn -eff
  284.  
  285. void cycle(void)
  286.  
  287. {
  288.  
  289.    int x=0, y, z;
  290.  
  291.  
  292.  
  293.    do
  294.  
  295.    {
  296.  
  297.       for (z=1; z<256; z++)
  298.  
  299.       {
  300.  
  301.      y=z+x;
  302.  
  303.      if (y > 255)
  304.  
  305.         y -= 255;
  306.  
  307.      outp(0x3C8, z);
  308.  
  309.      outp(0x3C9, palette[y][0]);
  310.  
  311.      outp(0x3C9, palette[y][1]);
  312.  
  313.      outp(0x3C9, palette[y][2]);
  314.  
  315.       }
  316.  
  317.       x += 1;
  318.  
  319.       if (x==255)
  320.  
  321.      x=0;
  322.  
  323.       delay(100); //* Originally, delay(50) *//
  324.  
  325.    }
  326.  
  327.    while (kbhit() == 0);
  328.  
  329.    getch();
  330.  
  331. }
  332.  
  333. #pragma warn +eff
  334.  
  335.  
  336.  
  337. //+=================+
  338.  
  339. // Fade-out routine +
  340.  
  341. //==================+
  342.  
  343. #pragma warn -eff
  344.  
  345. void fade(void)
  346.  
  347. {
  348.  
  349.    int a, b, p1, p2, p3;
  350.  
  351.  
  352.  
  353.    for (a=0; a<64; a++)
  354.  
  355.    {
  356.  
  357.       for (b=0; b<256; b++)
  358.  
  359.       {
  360.  
  361.      outp(0x3C7, b);
  362.  
  363.      p1 = inp(0x3C9);
  364.  
  365.      p2 = inp(0x3C9);
  366.  
  367.      p3 = inp(0x3C9);
  368.  
  369.      outp (0x3C8, b);
  370.  
  371.      if (p1 > 0) outp(0x3C9, p1 - 1);
  372.  
  373.         else outp(0x3C9, 0);
  374.  
  375.      if (p2 > 0) outp(0x3C9, p2 - 1);
  376.  
  377.         else outp(0x3C9, 0);
  378.  
  379.      if (p3 > 0) outp(0x3C9, p3 - 1);
  380.  
  381.         else outp(0x3C9, 0);
  382.  
  383.       }
  384.  
  385.    delay(100);
  386.  
  387.    }
  388.  
  389. }
  390.  
  391. #pragma warn +eff
  392.  
  393.  
  394.  
  395. //+============================================+
  396.  
  397. // Function to obtain current palette in array +
  398.  
  399. //+============================================+
  400.  
  401. #pragma warn -eff
  402.  
  403. void getpalette(void)
  404.  
  405. {
  406.  
  407.    int p, a, b;
  408.  
  409.  
  410.  
  411.    for (a=0; a<255; a++)                    /* a<256 if last color ok */
  412.  
  413.    {
  414.  
  415.       outp(0x3C7, a);                       /* Read values directly from */
  416.  
  417.       for (b=0; b<3; b++)                   /*    hardware port          */
  418.  
  419.       {
  420.  
  421.       p = inp(0x3C9);
  422.  
  423.       palette[a][b] = p;
  424.  
  425.       }
  426.  
  427.     }
  428.  
  429.     for (a=0; a<3; a++)                     /* Avoid white band if */
  430.  
  431.        palette[255][a] = palette[254][a];   /*   255 == white      */
  432.  
  433. }
  434.  
  435. #pragma warn +eff
  436.  
  437.