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

  1. //+------------------------------------------------------------------------+
  2.  
  3. //+ PROGRAM GHOSTS.CPP                                                     +
  4.  
  5. //+                                                                        +
  6.  
  7. //+ By Ramiro Perez (rperez%ns.pa@uga.cc.uga.edu) and Fausto A. A. Barbuto +
  8.  
  9. //+ (BJ06@C53000.PETROBRAS.ANRJ.BR), August 17, 1994.                      +
  10.  
  11. //+                                                                        +
  12.  
  13. //+ Plots phantom-like Julia sets; very fast.  Initial parameters cx & cy  +
  14.  
  15. //+ are randomically generated. Press any key to jump to another plot and  +
  16.  
  17. //+ ESC to quit.                                                           +
  18.  
  19. //+                                                                        +
  20.  
  21. //+ Uses SVGA256 Package by Jordan Powell Hargrave  (hargrave@dellgate.us. +
  22.  
  23. //+ dell.com) and  Random Number Generator plus Screen Fading routines by  +
  24.  
  25. //+ Michael Sargent (msargent@moose.uvm.edu) and Quintessential Sophistry. +
  26.  
  27. //+------------------------------------------------------------------------+
  28.  
  29. #include <math.h>
  30.  
  31. #include <stdio.h>
  32.  
  33. #include <stdlib.h>
  34.  
  35. #include <conio.h>
  36.  
  37. #include <graphics.h>
  38.  
  39. #include <dos.h>
  40.  
  41. #include "svga256.h"
  42.  
  43.  
  44.  
  45. double qsrandom(void);
  46.  
  47. void fade(void);
  48.  
  49.  
  50.  
  51. int Video;
  52.  
  53.  
  54.  
  55. int huge DetectSVGA256()
  56.  
  57. {
  58.  
  59.   clrscr();
  60.  
  61.   printf("\n Program GHOSTS.CPP ");
  62.  
  63.   printf("\n\n\n Which video mode would you like to use? \n\n");
  64.  
  65.   printf(" 0 - 320x200x256\n");
  66.  
  67.   printf(" 1 - 640x400x256\n");
  68.  
  69.   printf(" 2 - 640x480x256\n");
  70.  
  71.   printf(" 3 - 800x600x256\n");
  72.  
  73.   printf(" 4 - 1024x768x256\n\n> ");
  74.  
  75.   scanf("%d",&Video);
  76.  
  77.   if ((Video>4) || (Video<0)) Video = 2;
  78.  
  79.   return Video;
  80.  
  81. }
  82.  
  83.  
  84.  
  85. void main(void)
  86.  
  87. {
  88.  
  89.    int i, npix, npiy, graphdriver=DETECT, graphmode, k, size, dist;
  90.  
  91.    double a[3] = {0.5, 0.95, 0.05};
  92.  
  93.    double x[3], y[3], PI, rand1, rand2, cx, cy, wx, wy, theta, r, xt, yt;
  94.  
  95.    char choicekey, buffer[15];
  96.  
  97.  
  98.  
  99.    installuserdriver("Svga256",DetectSVGA256);
  100.  
  101.    initgraph(&graphdriver,&graphmode,"C:\\BORLANDC\\BGI");
  102.  
  103.  
  104.  
  105.    if (Video == 0) { npix =  80; npiy =  50; size = 4; dist = 10;}
  106.  
  107.    if (Video == 1) { npix = 160; npiy = 100; size = 5; dist = 13;}
  108.  
  109.    if (Video == 2) { npix = 160; npiy = 120; size = 6; dist = 16;}
  110.  
  111.    if (Video == 3) { npix = 200; npiy = 150; size = 7; dist = 21;}
  112.  
  113.    if (Video == 4) { npix = 256; npiy = 192; size = 8; dist = 26;}
  114.  
  115.  
  116.  
  117.    PI = 3.1415926535897932;
  118.  
  119.  
  120.  
  121.    do {
  122.  
  123.       for (i=0;i<=2;i++) {
  124.  
  125.      x[i] = 0.0;
  126.  
  127.      y[i] = 0.0;
  128.  
  129.       }
  130.  
  131.       rand1 = 0.5*(qsrandom() + qsrandom());
  132.  
  133.       if (rand1 > 0.5) cx = -2.25*qsrandom();
  134.  
  135.       else cx = 0.75*qsrandom();
  136.  
  137.  
  138.  
  139.       if (cx < 0) cy = 0.65*cx + 1.5;
  140.  
  141.       else cy = -1.8*cx + 1.5;
  142.  
  143.  
  144.  
  145.       rand1 = qsrandom();
  146.  
  147.       rand2 = qsrandom();
  148.  
  149.       cy = cy*(rand1 - rand2);
  150.  
  151.       cleardevice();
  152.  
  153.       setcolor(39);
  154.  
  155.       settextstyle(SMALL_FONT,HORIZ_DIR,size);
  156.  
  157.       sprintf(buffer," cx = %+e  ",cx);
  158.  
  159.       outtextxy(0,0,buffer);
  160.  
  161.       sprintf(buffer," cy = %+e  ",cy);
  162.  
  163.       outtextxy(0,dist,buffer);
  164.  
  165.  
  166.  
  167.       do {
  168.  
  169.      for (i=0;i<=2;i++) {
  170.  
  171.         wx = x[i] - cx;
  172.  
  173.         wy = y[i] - cy;
  174.  
  175.         if (wx > 0) theta = atan(wy/wx);
  176.  
  177.         if (wx < 0) theta = PI + atan(wy/wx);
  178.  
  179.         if (wx == 0) theta = 0.5*PI;
  180.  
  181.         theta = 0.5*theta;
  182.  
  183.         r = wx*wx + wy*wy;
  184.  
  185.         r = sqrt(sqrt(sqrt(r*r*r)));
  186.  
  187.         rand1 = qsrandom();
  188.  
  189.         if (rand1 > a[i]) r = -r;
  190.  
  191.         x[i] = r*cos(theta);
  192.  
  193.         y[i] = r*sin(theta);
  194.  
  195.         putpixel((int)npix*(x[i]+2.0),(int)npiy*(y[i]+2.0),34);
  196.  
  197.         putpixel((int)npix*(2.0-x[i]),(int)npiy*(2.0-y[i]),33);
  198.  
  199.      }
  200.  
  201.      rand1 = qsrandom();
  202.  
  203.      if (rand1 < 0.1) {
  204.  
  205.         xt = x[1];
  206.  
  207.         yt = y[1];
  208.  
  209.         x[1] = x[2];
  210.  
  211.         y[1] = y[2];
  212.  
  213.         x[2] = xt;
  214.  
  215.         y[2] = yt;
  216.  
  217.      }
  218.  
  219.       } while (!kbhit());
  220.  
  221.       choicekey = getch();
  222.  
  223.    } while (choicekey != 0x1B);
  224.  
  225.    fade();
  226.  
  227.    closegraph();
  228.  
  229. }
  230.  
  231.  
  232.  
  233. double qsrandom(void)
  234.  
  235. {
  236.  
  237.    int random_integer, temp_integer;
  238.  
  239.    double random_double, temp_double;
  240.  
  241.     
  242.  
  243.    random_integer = random(RAND_MAX);
  244.  
  245.    random_double = (double)random_integer / RAND_MAX;
  246.  
  247.     
  248.  
  249.    temp_integer = random(30519);
  250.  
  251.    temp_double = (double)temp_integer / 1000000000L;
  252.  
  253.    random_double += temp_double;
  254.  
  255.  
  256.  
  257.    return(random_double);
  258.  
  259. }
  260.  
  261.  
  262.  
  263. #pragma warn -eff
  264.  
  265. void fade(void)
  266.  
  267. {
  268.  
  269.    int a, b, p1, p2, p3;
  270.  
  271.  
  272.  
  273.    for (a=0; a<64; a++)
  274.  
  275.    {
  276.  
  277.       for (b=0; b<256; b++)
  278.  
  279.       {
  280.  
  281.      outp(0x3C7, b);
  282.  
  283.      p1 = inp(0x3C9);
  284.  
  285.      p2 = inp(0x3C9);
  286.  
  287.      p3 = inp(0x3C9);
  288.  
  289.      outp (0x3C8, b);
  290.  
  291.      if (p1 > 0) outp(0x3C9, p1 - 1);
  292.  
  293.         else outp(0x3C9, 0);
  294.  
  295.      if (p2 > 0) outp(0x3C9, p2 - 1);
  296.  
  297.         else outp(0x3C9, 0);
  298.  
  299.      if (p3 > 0) outp(0x3C9, p3 - 1);
  300.  
  301.         else outp(0x3C9, 0);
  302.  
  303.       }
  304.  
  305.    delay(50);
  306.  
  307.    }
  308.  
  309. }
  310.  
  311. #pragma warn +eff
  312.  
  313.