home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 24 / CD_ASCQ_24_0995.iso / vrac / butter10.zip / BUTTER.C next >
C/C++ Source or Header  |  1995-03-03  |  4KB  |  147 lines

  1. /*************************************************************************/
  2. /*                    Multi-Colored Butterfly Figure                     */
  3. /*     Based on an algorithm in Clifford Pickover's excellent book,      */
  4. /*                    COMPUTERS AND THE IMAGINATION                      */
  5. /*                                                                       */
  6. /*                              M\Cooper                                 */
  7. /*                      3425 Chestnut Ridge Rd.                          */
  8. /*                    Grantsville, MD 21536-9801                         */
  9. /*                    --------------------------                         */
  10. /*                    Email:  thegrendel@aol.com                         */
  11. /*************************************************************************/
  12.  
  13. #include <graphics.h>
  14. #include <stdio.h>
  15. #include <conio.h>
  16. #include <math.h>
  17.  
  18. #include <dos.h>
  19.  
  20. //set PIXEL_COLOR to desired
  21. #define PIXEL_COLOR LIGHTMAGENTA
  22. #define GR_ERROR 10
  23.  
  24. #define SF1 56.0
  25. #define SF2 250.0
  26. #define THETA_DIV 12.0
  27. #define MAXCOORDS 200.0
  28. #define X_SHIFT 1.23
  29. #define DELTA .02
  30.  
  31. void graphics_setup( int background_color );
  32. void butterfly( double maxxy, double step );
  33. void setpal();
  34.  
  35. void main()
  36. {
  37.    double maxcoords = MAXCOORDS,
  38.       step = .010;
  39.  
  40.      graphics_setup( WHITE );
  41.       setpal();
  42.  
  43.  
  44.      butterfly( maxcoords, step );
  45.  
  46.      closegraph();
  47.  
  48. }
  49.  
  50.  
  51.  
  52. /*************************************************************************/
  53. /*                         BUTTERFLY CURVES                 */
  54. /*         int maxxy = max coordinates on x- and y-axes             */
  55. /*                     stepsize = size of step                 */
  56. /*       see Pickover, COMPUTERS AND THE IMAGINATION, p. 21         */
  57. /*************************************************************************/
  58.  
  59. void butterfly( double maxxy, double stepsize )
  60. {
  61.    double theta,
  62.       r,  //radius, polar form
  63.       x,
  64.       y,
  65.       xx,
  66.       yy,
  67.    dummy,
  68.       i_res; //intermediate result (theta/12.0)
  69.    int tcolor = MAGENTA;
  70.  
  71.       for( theta = 0.0; theta <= maxxy * M_PI, !kbhit(); theta += stepsize )
  72.      {
  73.  
  74.      i_res = sin( theta / THETA_DIV );
  75.      r = exp( cos( theta ) ) - 2.0*cos( 4.0 * theta ) + pow( i_res, 5.0 );
  76.  
  77.      x = r * cos( theta );  //Convert to rectangular coordinates
  78.      y = r * sin( theta );  //from polar.
  79.  
  80.      xx = ( x * SF1 ) + X_SHIFT * SF2;
  81.      yy = ( y * SF1 ) + SF2;
  82.  
  83.      if( !theta )
  84.         moveto( xx, yy );
  85.      else
  86.      {
  87.         lineto( xx, yy );
  88.      if( modf( theta / M_PI, &dummy ) < DELTA )
  89.         {
  90.         tcolor = (int)yy & 0xf;
  91.         if( tcolor < 0 || tcolor > 15 ) tcolor = (int)xx & 0xf;
  92.         setcolor( tcolor );
  93.         }
  94.      }
  95.  
  96.      }
  97.  
  98.       return;
  99.  
  100. }
  101.  
  102.  
  103.  
  104. /*************************************************************************/
  105. /*                    GENERIC VGA GRAPHICS SETUP                         */
  106. /*************************************************************************/
  107.  
  108. void graphics_setup( int background_color )
  109. {
  110.    int grdriver = VGA,
  111.        grmode = VGAHI;
  112.  
  113.        registerfarbgidriver( EGAVGA_driver_far );
  114.        registerfarbgifont( triplex_font_far );
  115.        initgraph( &grdriver, &grmode, "" );
  116.  
  117.        if( graphresult() )  //Error in opening graphics mode..
  118.           {
  119.           closegraph();
  120.       puts( "Error in opening graphics systems!" );
  121.       exit( GR_ERROR );
  122.           }
  123.  
  124.        setbkcolor( background_color );
  125.        setcolor( PIXEL_COLOR );
  126.  
  127.        return;
  128.  
  129. }
  130.  
  131.  
  132. void setpal()
  133. {
  134.       setpalette( 6, EGA_BROWN );
  135.       setpalette( 8, EGA_DARKGRAY );
  136.       setpalette( 9, EGA_LIGHTBLUE );
  137.       setpalette( 10, EGA_LIGHTGREEN );
  138.       setpalette( 11, EGA_LIGHTCYAN );
  139.       setpalette( 12, EGA_LIGHTRED );
  140.       setpalette( 13, EGA_LIGHTMAGENTA );
  141.       setpalette( 14, EGA_YELLOW );
  142.       setpalette( 15, EGA_LIGHTRED );
  143.  
  144.       return;
  145. }
  146.  
  147.