home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / qc / qc25 / realg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-11-15  |  3.0 KB  |  129 lines

  1. /* REALG.C: Example of real-coordinate graphics */
  2. #include <stdio.h>
  3. #include <conio.h>
  4. #include <graph.h>
  5.  
  6. #define TRUE 1
  7. #define FALSE 0
  8.  
  9. int four_colors( void );
  10. void three_graphs( void );
  11. void grid_shape( void );
  12.  
  13. int halfx, halfy;
  14. struct videoconfig screen;
  15. double bananas[] =
  16. {   -0.3, -0.2, -0.224, -0.1, -0.5, +0.21, +2.9,
  17.    +0.3, +0.2, 0.0, -0.885, -1.1, -0.3, -0.2,
  18.    +.001, +.005, +0.14, 0.0, -0.9, -0.13, +0.3
  19. };
  20.  
  21. main()
  22. {
  23.    if( four_colors() )
  24.       three_graphs();
  25.    else
  26.       printf( "This program requires a CGA, EGA, or VGA graphics card.\n" );
  27. }
  28.  
  29. int four_colors( void )
  30. {
  31.    _getvideoconfig( &screen );
  32.    switch( screen.adapter )
  33.    {
  34.       case _CGA:
  35.       case _OCGA:
  36.          _setvideomode( _MRES4COLOR );
  37.          break;
  38.       case _EGA:
  39.       case _OEGA:
  40.          _setvideomode( _ERESCOLOR );
  41.          break;
  42.       case _VGA:
  43.       case _OVGA:
  44.          _setvideomode( _VRES16COLOR );
  45.          break;
  46.       default:
  47.          return( FALSE );
  48.    }
  49.    _getvideoconfig( &screen );
  50.    return( TRUE );
  51. }
  52.  
  53. void three_graphs( void )
  54. {
  55.    int xwidth, yheight, cols, rows;
  56.    struct _wxycoord upleft, botright;
  57.  
  58.    _clearscreen( _GCLEARSCREEN );
  59.    xwidth = screen.numxpixels;
  60.    yheight = screen.numypixels;
  61.    halfx = xwidth/2;
  62.    halfy = yheight/2;
  63.    cols = screen.numtextcols;
  64.    rows = screen.numtextrows;
  65.    
  66.    /* first window */
  67.    _setviewport( 0, 0, halfx-1, halfy-1 );
  68.    _settextwindow( 1, 1, rows/2, cols/2 );
  69.    _setwindow( FALSE, -2.0, -2.0, 2.0, 2.0 );
  70.    grid_shape();
  71.    _rectangle( _GBORDER, 0, 0, halfx-1, halfy-1 );
  72.    
  73.    /* second window */
  74.    _setviewport( halfx, 0, xwidth-1, halfy-1 );
  75.    _settextwindow( 1, cols/2+1, rows/2, cols );
  76.    _setwindow( FALSE, -3.0, -3.0, 3.0, 3.0 );
  77.    grid_shape();
  78.    _rectangle_w( _GBORDER, -3.0, -3.0, 3.0, 3.0 );   
  79.    
  80.    /* third window */
  81.    _setviewport( 0, halfy, xwidth-1, yheight-1 );
  82.    _settextwindow( rows/2+1, 1, rows, cols );
  83.    _setwindow( TRUE, -3.0, -1.5, 1.5, 1.5 );
  84.    grid_shape();
  85.    upleft.wx = -3.0;
  86.    upleft.wy = -1.5;
  87.    botright.wx = 1.5;
  88.    botright.wy = 1.5;
  89.    _rectangle_wxy( _GBORDER, &upleft, &botright );
  90.    
  91.    getch();
  92.    _setvideomode( _DEFAULTMODE );
  93. }
  94.  
  95. void grid_shape( void )
  96. {
  97.    int i, numc, x1, y1, x2, y2;
  98.    double x, y;
  99.    char txt[80];
  100.    
  101.    numc = screen.numcolors;
  102.    for( i = 1; i < numc; i++ )
  103.    {
  104.       _settextposition( i, 2 );
  105.       _settextcolor( i );
  106.       sprintf( txt, "Color %d", i );
  107.       _outtext( txt );
  108.    }
  109.    
  110.    _setcolor( 1 );
  111.    _rectangle_w( _GBORDER, -1.0, -1.0, 1.0, 1.0 );
  112.    _rectangle_w( _GBORDER, -1.02, -1.02, 1.02, 1.02 );
  113.    
  114.    for( x = -0.9, i = 0; x < 0.9; x += 0.1 )
  115.    {
  116.       _setcolor( 2 );
  117.       _moveto_w( x, -1.0 );
  118.       _lineto_w( x, 1.0 );
  119.       _moveto_w( -1.0, x );
  120.       _lineto_w( 1.0, x );
  121.    
  122.       _setcolor( 3 );
  123.       _moveto_w( x - 0.1, bananas[i++] );
  124.       _lineto_w( x, bananas[i] );
  125.    }
  126.    _moveto_w( 0.9, bananas[i++] );
  127.    _lineto_w( 1.0, bananas[i] );
  128. }
  129.