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

  1. //
  2.  
  3. //+---------------------------------------------------------------------+
  4.  
  5. //+ Program LORENZ.CPP                                                  +
  6.  
  7. //+ By Ramiro Perez {RPEREZ@UTPVM1.BITNET}, (Panama)                    +
  8.  
  9. //+ and Fausto A. A. Barbuto {BJ06@C53000.PETROBRAS.ANRJ.BR}, (Brazil). +
  10.  
  11. //+ C++ 3.1 programme's creator: Fausto A. A. Barbuto, April 14, 1994.  +
  12.  
  13. //+ After a TURBO BASIC program by Ramiro Perez.                        +
  14.  
  15. //+                                                                     +
  16.  
  17. //+ Plots Lorenz attractors with shaded, colourful spheres.             +
  18.  
  19. //+                                                                     +
  20.  
  21. //+ SVGA 256 colours version. Needs SVGA256.BGI and SVGA256.H.          +
  22.  
  23. //+ We suggest to change the start-up values of the arrays xa, ya, and  +
  24.  
  25. //+ za to witness how the attractor's shape change.                     +
  26.  
  27. //+ Press any key to stop and PAUSE to freeze the execution.            +
  28.  
  29. //+ Authorized version for spanky.triumf.ca site.                       +
  30.  
  31. //+---------------------------------------------------------------------+
  32.  
  33. //
  34.  
  35. #include <graphics.h>
  36.  
  37. #include <conio.h>
  38.  
  39. #include <stdio.h>
  40.  
  41. #include <dos.h>
  42.  
  43. #include "svga256.h"
  44.  
  45.  
  46.  
  47. void Draw (double, double, int);
  48.  
  49. int Vid;  //Global variable
  50.  
  51.  
  52.  
  53. int huge DetectSVGA256()
  54.  
  55. {
  56.  
  57.   printf("\nWhich video mode would you like to use? \n\n");
  58.  
  59.   printf(" 0 - 320x200x256\n");
  60.  
  61.   printf(" 1 - 640x400x256\n");
  62.  
  63.   printf(" 2 - 640x480x256\n");
  64.  
  65.   printf(" 3 - 800x600x256\n");
  66.  
  67.   printf(" 4 - 1024x768x256\n\n> ");
  68.  
  69.   scanf("%d",&Vid);
  70.  
  71.   if((Vid<0) || (Vid)>4) Vid = 2;
  72.  
  73.   return Vid;
  74.  
  75. }
  76.  
  77.  
  78.  
  79. void main()
  80.  
  81. {
  82.  
  83.     int c, b, a, l, Iopt;
  84.  
  85.     double c1, cl, dt, xa[2], ya[2], za[2], x, y, z, x1, y1, z1, xd, yd;
  86.  
  87.     int graphdriver=DETECT, graphmode;
  88.  
  89.  
  90.  
  91.     clrscr();
  92.  
  93.     printf("\n                           Program LORENZ.CPP\n\n");
  94.  
  95.     printf("\n    Select an option:\n\n");
  96.  
  97.     printf("\n                0: Ramiro's default start-up values;\n");
  98.  
  99.     printf("\n                1: Enter your own start-up values;\n\n>> ");
  100.  
  101.     scanf("%d",&Iopt);
  102.  
  103.     if ((Iopt > 1) || (Iopt < 0)) Iopt = 0;
  104.  
  105.     clrscr();
  106.  
  107.  
  108.  
  109.     if (Iopt == 1) {
  110.  
  111.       printf("\n Enter initial value for xa[0] (-99: default is used)\n> ");
  112.  
  113.       scanf("%lf",&xa[0]);
  114.  
  115.       if (xa[0] == -99.0) xa[0] = 3.051522;
  116.  
  117.       printf("\n Enter initial value for xa[1] (-99: default is used)\n> ");
  118.  
  119.       scanf("%lf",&xa[1]);
  120.  
  121.       if (xa[1] == -99.0) xa[1] = 3.051522;
  122.  
  123.       printf("\n Enter initial value for ya[0] (-99: default is used)\n> ");
  124.  
  125.       scanf("%lf",&ya[0]);
  126.  
  127.       if (ya[0] == -99.0) ya[0] = 1.592542;
  128.  
  129.       printf("\n Enter initial value for ya[1] (-99: default is used)\n> ");
  130.  
  131.       scanf("%lf",&ya[1]);
  132.  
  133.       if (ya[1] == -99.0) ya[1] = 1.582542;
  134.  
  135.       printf("\n Enter initial value for za[0] (-99: default is used)\n> ");
  136.  
  137.       scanf("%lf",&za[0]);
  138.  
  139.       if (za[0] == -99.0) za[0] = 15.62388;
  140.  
  141.       printf("\n Enter initial value for za[1] (-99: default is used)\n> ");
  142.  
  143.       scanf("%lf",&za[1]);
  144.  
  145.       if (za[1] == -99.0) za[1] = 15.62388;
  146.  
  147.       clrscr();
  148.  
  149.     }
  150.  
  151.  
  152.  
  153.     installuserdriver("Svga256",DetectSVGA256);
  154.  
  155.     initgraph(&graphdriver,&graphmode,"C:\\BORLANDC\\BGI");
  156.  
  157.     cleardevice();
  158.  
  159.  
  160.  
  161.     c1 = 0.292893;
  162.  
  163.     dt = 0.02;
  164.  
  165.     a  = 5;
  166.  
  167.     b  = 15;
  168.  
  169.     c  = 1;
  170.  
  171. //
  172.  
  173. //  Default start-up values for xa, ya and za.
  174.  
  175. //
  176.  
  177.     if (Iopt == 0) {
  178.  
  179.       xa[0] = 3.051522;
  180.  
  181.       ya[0] = 1.592542;
  182.  
  183.       za[0] = 15.62388;
  184.  
  185.       xa[1] = xa[0];
  186.  
  187.       ya[1] = 1.582542;
  188.  
  189.       za[1] = za[0];
  190.  
  191.     }
  192.  
  193.  
  194.  
  195.     do {
  196.  
  197.       for (l=0;l<=1;l++) {
  198.  
  199.     x = xa[l];
  200.  
  201.     y = ya[l];
  202.  
  203.     z = za[l];
  204.  
  205.     x1 = x - a*x*dt + a*y*dt;
  206.  
  207.     y1 = y + b*x*dt - y*dt - z*x*dt;
  208.  
  209.     z1 = z - c*z*dt + x*y*dt;
  210.  
  211.     x = x1;
  212.  
  213.     y = y1;
  214.  
  215.     z = z1;
  216.  
  217.     xd = y - x*c1;
  218.  
  219.     yd = z + x*c1;
  220.  
  221.     if (l==1) {
  222.  
  223.       cl = 0;
  224.  
  225.       Draw(xd,yd,cl);
  226.  
  227.     }
  228.  
  229.     else {
  230.  
  231.       cl = 7;
  232.  
  233.       Draw(xd,yd,cl);
  234.  
  235.     }
  236.  
  237.     delay(4);  //Change the time delay to see it slower/faster;
  238.  
  239.     xa[l] = x;
  240.  
  241.     ya[l] = y;
  242.  
  243.     za[l] = z;
  244.  
  245.       }
  246.  
  247.     } while (!kbhit());
  248.  
  249.     getch();
  250.  
  251.     closegraph();
  252.  
  253. }
  254.  
  255.  
  256.  
  257. void Draw (double xd, double yd, int cl)
  258.  
  259. {
  260.  
  261.     double i1, j1, c, c1, c2, d1, d2;
  262.  
  263.     int i, k, colour;
  264.  
  265.  
  266.  
  267.     if (Vid == 0) { c1 =  9.7; c2 = 160.0; d1 =  4.6; d2 = 175.0; k=4;}
  268.  
  269.     if (Vid == 1) { c1 = 19.3; c2 = 325.0; d1 =  9.2; d2 = 345.0; k=7;}
  270.  
  271.     if (Vid == 2) { c1 = 19.3; c2 = 320.0; d1 = 11.0; d2 = 392.0; k=7;}
  272.  
  273.     if (Vid == 3) { c1 = 30.0; c2 = 400.0; d1 = 17.1; d2 = 550.0; k=9;}
  274.  
  275.     if (Vid == 4) { c1 = 38.4; c2 = 500.0; d1 = 21.9; d2 = 675.0; k=12;}
  276.  
  277.  
  278.  
  279.     i1 = c1*xd + c2;
  280.  
  281.     j1= -d1*yd + d2;
  282.  
  283.  
  284.  
  285.     for (i=1;i<=7;i++) {
  286.  
  287.       c = 0.09*i;
  288.  
  289.       colour = (i + cl) + 33;
  290.  
  291.       setcolor(colour);
  292.  
  293.       setfillstyle(SOLID_FILL,k);
  294.  
  295.       circle ((int)(i1+c),(int)(j1+c),k);
  296.  
  297.       k--;
  298.  
  299.     }
  300.  
  301.     return;
  302.  
  303. }
  304.  
  305.  
  306.  
  307.