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

  1. //+-------------------------------------------------------------------------+
  2.  
  3. //+ Program TCHEBY.CPP - November 13, 1994                                  +
  4.  
  5. //+                                                                         +
  6.  
  7. //+ By Fausto Arinos de Almeida Barbuto, Rio de Janeiro, RJ, BRAZIL         +
  8.  
  9. //+ E-mails: BJ06@C53000.PETROBRAS.ANRJ.BR  and  barbuto@ax.ibase.org.br    +
  10.  
  11. //+                                                                         +
  12.  
  13. //+ Repeated iterations over the Tschebysheff function over the real domain.+
  14.  
  15. //+ Based on Pseudocode 15.1 (see REFERENCE below)                          +
  16.  
  17. //+ Press ESC at any time to fade the screen out and quit.                  +
  18.  
  19. //+                                                                         +
  20.  
  21. //+ REFERENCE: Pickover, Clifford A. "Computers, Pattern, Chaos and Beauty" +
  22.  
  23. //+            1990, St.-Martin's Press, pp. 293.                           +
  24.  
  25. //+                                                                         +
  26.  
  27. //+ Uses SVGA256.BGI by Jordan Powell Hargrave and fade-out routine by      +
  28.  
  29. //+ Michael E. Sargent and Quintessential Sophistry.                        +
  30.  
  31. //+                                                                         +
  32.  
  33. //+ Version for spanky.triumf.ca (142.90.112.1), Vancouver, B.C. Canada.    +
  34.  
  35. //+*************************************************************************+
  36.  
  37.  
  38.  
  39. #include <graphics.h>
  40.  
  41. #include <stdio.h>
  42.  
  43. #include <dos.h>
  44.  
  45. #include <conio.h>
  46.  
  47. #include <math.h>
  48.  
  49. #include "svga256.h"
  50.  
  51.  
  52.  
  53. int Video; /* Global variable */
  54.  
  55.  
  56.  
  57. double Chebyshev(int, double);
  58.  
  59. void fade(void);
  60.  
  61.  
  62.  
  63. int huge DetectSVGA256()
  64.  
  65. {
  66.  
  67.   printf("\n Which video mode would you like to use? \n\n");
  68.  
  69.   printf(" 0 - 320x200x256\n");
  70.  
  71.   printf(" 1 - 640x400x256\n");
  72.  
  73.   printf(" 2 - 640x480x256 (suggested when available)\n");
  74.  
  75.   printf(" 3 - 800x600x256\n");
  76.  
  77.   printf(" 4 - 1024x768x256\n\n> ");
  78.  
  79.   scanf("%d",&Video);
  80.  
  81.   if ((Video>4) || (Video<0)) Video = 2;
  82.  
  83.   return Video;
  84.  
  85. }
  86.  
  87.  
  88.  
  89. void main()
  90.  
  91. {
  92.  
  93.       double xmin=-1.0, xmax=1.0, ymin=-1.0, ymax=1.0;
  94.  
  95.       double xold, yold, r, deltap, deltaq, p, q, x, y, T1, T2, h;
  96.  
  97.       unsigned int maxiter;
  98.  
  99.       register int k, np, nq;
  100.  
  101.       int npix, npiy, order, initcolour, graphdriver=DETECT, graphmode;
  102.  
  103. //
  104.  
  105.       clrscr();
  106.  
  107.       printf("\n Program CHEBY.C - By Fausto A. A. Barbuto, November 1994");
  108.  
  109.       printf("\n\n WARNING: Some combinations of high 'order', 'h' and");
  110.  
  111.       printf(" 'maxiter' may cause\n          overflow before the end");
  112.  
  113.       printf(" of the execution.");
  114.  
  115.       printf("\n\n Select the order of the polynomial (3 <= order <= 20):");
  116.  
  117.       printf("\n\n> ");
  118.  
  119.       scanf("%d",&order);
  120.  
  121.       if ((order < 3) || (order > 20)) order = 10;
  122.  
  123.       printf("\n Select a step 'h', 0.0 < h < 1.0 (suggested: 0.1):\n\n> ");
  124.  
  125.       scanf("%lf",&h);
  126.  
  127.       printf("\n\n Select the maximum number of iterations");
  128.  
  129.       printf("\n (Suggestion: start with 20, then increase):\n\n> ");
  130.  
  131.       scanf("%u",&maxiter);
  132.  
  133.       clrscr();
  134.  
  135.       printf("\n Choose a colour scheme:\n\n    'Tropical'    : Enter 30\n");
  136.  
  137.       printf("    Shades of gray: Enter 16\n    Your own      : ");
  138.  
  139.       printf("Enter ???\n\n> ");
  140.  
  141.       scanf("%d",&initcolour);
  142.  
  143.  
  144.  
  145.       installuserdriver("Svga256",DetectSVGA256);
  146.  
  147.       initgraph(&graphdriver,&graphmode,"C:\\BORLANDC\\BGI");
  148.  
  149.  
  150.  
  151.       cleardevice();
  152.  
  153.  
  154.  
  155.       if (Video == 0) {npix=320;  npiy=200;}
  156.  
  157.       if (Video == 1) {npix=640;  npiy=400;}
  158.  
  159.       if (Video == 2) {npix=640;  npiy=480;}
  160.  
  161.       if (Video == 3) {npix=800;  npiy=600;}
  162.  
  163.       if (Video == 4) {npix=1024; npiy=768;}
  164.  
  165.  
  166.  
  167.       deltap = (xmax-xmin)/(double)(npix-1);
  168.  
  169.       deltaq = (ymax-ymin)/(double)(npiy-1);
  170.  
  171.  
  172.  
  173.       for (np=0; np<=npix-1; np++) {
  174.  
  175.     p = xmin + (double)np*deltap;
  176.  
  177.     for (nq=0; nq<=npiy-1; nq++) {
  178.  
  179.       q = ymin + (double)nq*deltaq;
  180.  
  181.       x = p; y = q;
  182.  
  183.       k = 0;
  184.  
  185.       do {
  186.  
  187.         xold = x; yold = y;
  188.  
  189.         T1 = Chebyshev(order,yold);
  190.  
  191.         x = x - h*sin(T1);
  192.  
  193.         T2 = Chebyshev(order,xold);
  194.  
  195.         y = y + h*sin(T2);
  196.  
  197.         r = sqrt(T1*T1 + T2*T2);
  198.  
  199.         k++;
  200.  
  201.  
  202.  
  203.         if (r >= maxiter) {
  204.  
  205.           putpixel(np,nq,(initcolour+k));
  206.  
  207.         }
  208.  
  209.  
  210.  
  211.         if (k == maxiter) {
  212.  
  213.           putpixel(np,nq,1);
  214.  
  215.         }
  216.  
  217.  
  218.  
  219.       } while (r<=maxiter && k<=maxiter);
  220.  
  221.     }
  222.  
  223.     if (kbhit()) break;
  224.  
  225.       }
  226.  
  227.       getch();
  228.  
  229.       fade();
  230.  
  231.       closegraph();
  232.  
  233. }
  234.  
  235.  
  236.  
  237. double Chebyshev(int nn, double xx)
  238.  
  239. {
  240.  
  241.    double z3, Tc[20], T;
  242.  
  243.    register int ii;
  244.  
  245.  
  246.  
  247.      z3 = xx*xx*xx;
  248.  
  249.      Tc[2] = 2.0*xx*xx - 1.0;
  250.  
  251.      Tc[3] = 4.0*z3 - 3.0*xx;
  252.  
  253.      for (ii=3;ii<=nn;ii++) {
  254.  
  255.        Tc[ii+1] = 2.0*xx*Tc[ii] - Tc[ii-1];
  256.  
  257.        T = Tc[ii+1];
  258.  
  259.      }
  260.  
  261.      return T;
  262.  
  263. }
  264.  
  265. //+======================================================================+
  266.  
  267. // Fade-out routine by Michael E. Sargent & The Quintessential Sophistry +
  268.  
  269. //=======================================================================+
  270.  
  271. #pragma warn -eff
  272.  
  273. void fade(void)
  274.  
  275. {
  276.  
  277.    int a, b, p1, p2, p3;
  278.  
  279.  
  280.  
  281.    for (a=0; a<64; a++)
  282.  
  283.    {
  284.  
  285.       for (b=0; b<256; b++)
  286.  
  287.       {
  288.  
  289.      outp(0x3C7, b);
  290.  
  291.      p1 = inp(0x3C9);
  292.  
  293.      p2 = inp(0x3C9);
  294.  
  295.      p3 = inp(0x3C9);
  296.  
  297.      outp (0x3C8, b);
  298.  
  299.      if (p1 > 0) outp(0x3C9, p1 - 1);
  300.  
  301.         else outp(0x3C9, 0);
  302.  
  303.      if (p2 > 0) outp(0x3C9, p2 - 1);
  304.  
  305.         else outp(0x3C9, 0);
  306.  
  307.      if (p3 > 0) outp(0x3C9, p3 - 1);
  308.  
  309.         else outp(0x3C9, 0);
  310.  
  311.       }
  312.  
  313.    delay(75);
  314.  
  315.    }
  316.  
  317. }
  318.  
  319. #pragma warn +eff
  320.  
  321.