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

  1. /*  SINE.C: Illustrates basic graphics commands */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <graph.h>
  5. #include <math.h>
  6. #include <conio.h>
  7. #define PI 3.14159
  8.  
  9. void graphics_mode( void );
  10. void draw_lines( void );
  11. void sine_wave( void );
  12. void draw_shapes( void );
  13. void end_program( void );
  14. int newx( int );
  15. int newy( int );
  16.  
  17. struct videoconfig myscreen;
  18. int maxx, maxy;
  19. unsigned char diagmask[8] =
  20. { 0x93, 0xC9, 0x64, 0xB2, 0x59, 0x2C, 0x96, 0x4B };
  21. unsigned char linemask[8] =
  22. { 0xFF, 0x00, 0x7F, 0xFE, 0x00, 0x00, 0x00, 0xCC };
  23.  
  24. main()
  25. {
  26.    graphics_mode();
  27.    draw_lines();
  28.    sine_wave();
  29.    draw_shapes();
  30.    end_program();
  31. }
  32.  
  33. void graphics_mode( void )
  34. {
  35.    _getvideoconfig( &myscreen );
  36.    switch( myscreen.adapter )
  37.    {
  38.       case _CGA:
  39.          _setvideomode( _HRESBW );
  40.          break;
  41.       case _OCGA:
  42.          _setvideomode( _ORESCOLOR );
  43.          break;
  44.       case _EGA:
  45.       case _OEGA:
  46.          if( myscreen.monitor == _MONO )
  47.             _setvideomode( _ERESNOCOLOR );
  48.          else
  49.             _setvideomode( _ERESCOLOR );
  50.          break;
  51.       case _VGA:
  52.       case _OVGA:
  53.       case _MCGA:
  54.          _setvideomode( _VRES2COLOR );
  55.          break;
  56.       case _HGC:
  57.          _setvideomode( _HERCMONO );
  58.          break;
  59.       default:
  60.          printf( "This program requires a CGA, EGA, VGA, or Hercules card\n" );
  61.          exit( 0 );
  62.    }
  63.    _getvideoconfig( &myscreen );
  64.    maxx = myscreen.numxpixels - 1;
  65.    maxy = myscreen.numypixels - 1;
  66. }
  67.  
  68. int newx( int xcoord )
  69. {
  70.    int nx;
  71.    float tempx;
  72.    tempx = ((float) maxx)/ 1000.0;
  73.    tempx = ((float) xcoord) * tempx + 0.5;
  74.    return( (int) tempx );
  75. }
  76.  
  77. int newy( int ycoord )
  78. {
  79.    int ny;
  80.    float tempy;
  81.    tempy = ((float) maxy)/ 1000.0;
  82.    tempy = ((float) ycoord) * tempy + 0.5;
  83.    return( (int) tempy );
  84. }
  85.  
  86. void sine_wave( void )
  87. {
  88.    int locx, locy;
  89.    double i, rad;
  90.  
  91.    for( i = 0.0; i < 1000.0; i += 3.0 )
  92.    {
  93.       rad = -sin( (PI * (float) i) / 250.0 );
  94.       locx = newx( (int) i );
  95.       locy = newy( (int) (rad * 250.0) );
  96.       _setpixel( locx, locy );
  97.    }
  98. }
  99.  
  100. void draw_shapes( void )
  101. {
  102.    _setlinestyle( 0xFFFF );
  103.    _setfillmask( diagmask );
  104.    _rectangle( _GBORDER, newx(50), newy(-325), newx(200), newy(-425) );
  105.    _rectangle( _GFILLINTERIOR, newx(550), newy(-325), newx(700), newy(-425) );
  106.  
  107.    _setfillmask( linemask );
  108.    _ellipse( _GBORDER, newx(50), newy(325), newx(200), newy(425) );
  109.    _ellipse( _GFILLINTERIOR, newx(550), newy(325), newx(700), newy(425) );
  110. }
  111.  
  112. void end_program( void )
  113. {
  114.    getch();
  115.    _setvideomode( _DEFAULTMODE );
  116. }
  117.  
  118. void draw_lines( void )
  119. {
  120.    _rectangle( _GBORDER, 0, 0, maxx, maxy );
  121.    /* _setcliprgn( 20, 20, maxx - 20, maxy - 20 ); */
  122.    _setvieworg( 0, newy( 500 ) );
  123.  
  124.    _moveto( 0, 0 );
  125.    _lineto( newx( 1000 ), 0 );
  126.    _setlinestyle( 0xAA3C );
  127.    _moveto( 0, newy( -250) );
  128.    _lineto( newx( 1000 ), newy( -250 ) );
  129.  
  130.    _setlinestyle( 0x8888 );
  131.    _moveto( 0, newy( 250 ) );
  132.    _lineto( newx( 1000 ), newy( 250 ) );
  133. }
  134.