home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / c / other / graphics / realg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-07  |  3.2 KB  |  140 lines

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