home *** CD-ROM | disk | FTP | other *** search
/ The Devil's Doorknob BBS Capture (1996-2003) / devilsdoorknobbbscapture1996-2003.iso / Dloads / OTHERUTI / TCPP30-3.ZIP / EXAMPLES.ZIP / PLANETS.CPP < prev    next >
Text File  |  1992-02-18  |  3KB  |  107 lines

  1. // PLANETS.CPP--Example from chapter 3
  2.  
  3. //#include <stdio.h>
  4. #include <graphics.h>           // For graphics library functions
  5. #include <stdlib.h>             // For exit()
  6. #include <iostream.h>
  7. #include <conio.h>
  8.  
  9. int set_graph(void);            // Initialize graphics
  10. void calc_coords(void);         // Scale distances onscreen
  11. void draw_planets(void);        // Draw and fill planet circles
  12.  
  13. // Draw one planet circle
  14. void draw_planet(float x_pos, float radius, int color, int fill_style);
  15. void get_key(void);         // Display text on graphics screen,
  16.                             // Wait for key
  17.  
  18. // Global variables -- set by calc_coords()
  19. int max_x, max_y;           // Maximum x- and y-coordinates
  20. int y_org;                  // Y-coordinate for all drawings
  21. int au1;                    // One astronomical unit in pixels
  22.                         // (inner planets)
  23.  
  24. int au2;                    // One astronomical unit in pixelskeepfollowing
  25.                             // (outer planets)
  26. int erad;                   // One earth radius in pixels
  27.  
  28. int main()
  29. {
  30.     // Exit if not EGA or VGA
  31.     // Find out if they have what it takes
  32.     if (set_graph() != 1) {
  33.         cout << "This program requires EGA or VGA graphics\n";
  34.         exit(0);
  35.     }
  36.     calc_coords();       // Scale to graphics resolution in use
  37.     draw_planets();      // Sun through Uranus 
  38.     get_key();           // Display message and wait for key press
  39.     closegraph();        // Close graphics system
  40.  
  41.     return 0;
  42. }
  43.  
  44. int set_graph(void)
  45. {
  46.     int graphdriver = DETECT, graphmode, error_code;
  47.  
  48.     //Initialize graphics system; must be EGA or VGA
  49.     initgraph(&graphdriver, &graphmode, ".\\bgi");
  50.     error_code = graphresult();
  51.     if (error_code != grOk)
  52.         return(-1);               // No graphics hardware found
  53.     if ((graphdriver != EGA) && (graphdriver != VGA))
  54.     {
  55.         closegraph();
  56.         return 0;
  57.     }
  58.     return(1);                   // Graphics OK, so return "true"
  59. }
  60.  
  61. void calc_coords(void)
  62. {
  63.     // Set global variables for drawing
  64.     max_x = getmaxx();           // Returns maximum x-coordinate
  65.     max_y = getmaxy();           // Returns maximum y-coordinate
  66.     y_org = max_y / 2;           // Set Y coord for all objects
  67.     erad = max_x  / 200;         // One earth radius in pixels
  68.     au1 = erad * 20;             // Scale for inner planets
  69.     au2 = erad * 10;             // scale for outer planets
  70. }
  71.  
  72. void draw_planets()
  73. {
  74.  
  75.     // Each call specifies x-coordinate in au, radius, and color
  76.     // arc of Sun
  77.     draw_planet(-90, 100, EGA_YELLOW, EMPTY_FILL);
  78.     // Mercury
  79.     draw_planet(0.4 * au1, 0.4 * erad, EGA_BROWN, LTBKSLASH_FILL);
  80.     // Venus
  81.     draw_planet(0.7 * au1, 1.0 * erad, EGA_WHITE, SOLID_FILL);
  82.     // Earth
  83.     draw_planet(1.0 * au1, 1.0 * erad, EGA_LIGHTBLUE, SOLID_FILL);
  84.     // Mars
  85.     draw_planet(1.5 * au1, 0.4 * erad, EGA_LIGHTRED, CLOSE_DOT_FILL);
  86.     // Jupiter
  87.     draw_planet(5.2 * au2, 11.2 * erad, EGA_WHITE, LINE_FILL);
  88.     // Saturn
  89.     draw_planet(9.5 * au2, 9.4 * erad, EGA_LIGHTGREEN, LINE_FILL);
  90.     // Uranus
  91.     draw_planet(19.2 * au2, 4.2 * erad, EGA_GREEN, LINE_FILL);
  92. }
  93.  
  94. void draw_planet(float x_pos, float radius, int color, int fill_style)
  95. {
  96.     setcolor (color);                // This becomes drawing color
  97.     circle(x_pos, y_org, radius);    // Draw the circle
  98.     setfillstyle(fill_style, color); // Set pattern to fill interior
  99.     floodfill(x_pos, y_org, color);  // Fill the circle
  100. }
  101.  
  102. void get_key(void)
  103. {
  104.     outtextxy(50, max_y - 20, "Press any key to exit");
  105.     getch();
  106. }
  107.