home *** CD-ROM | disk | FTP | other *** search
/ Teach Yourself Game Programming in 21 Days / TYGAMES_R.ISO / source / day_08 / lookup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-17  |  2.6 KB  |  121 lines

  1.  
  2. // I N C L U D E S ///////////////////////////////////////////////////////////
  3.  
  4. #include <io.h>
  5. #include <conio.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <dos.h>
  9. #include <bios.h>
  10. #include <fcntl.h>
  11. #include <memory.h>
  12. #include <malloc.h>
  13. #include <math.h>
  14. #include <string.h>
  15.  
  16. #include "graph3.h"  // include our graphics stuff
  17. #include "graph4.h"
  18.  
  19. // D E F I N E S /////////////////////////////////////////////////////////////
  20.  
  21. #define NUM_CIRCLES 1000
  22. #define FULL_CIRCLE 360
  23.  
  24. // G L O B A L S  ////////////////////////////////////////////////////////////
  25.  
  26. float sin_table[360], cos_table[360];
  27.  
  28. // M A I N ///////////////////////////////////////////////////////////////////
  29.  
  30. void main(void)
  31. {
  32.  
  33. int index,x,y,xo,yo,radius,ang;
  34.  
  35. // create look up tables
  36.  
  37. for (index=0; index<FULL_CIRCLE; index++)
  38.      {
  39.  
  40.      sin_table[index]= sin(index*3.14159/180);
  41.      cos_table[index] = cos(index*3.14159/180);
  42.  
  43.      } // end for index
  44.  
  45. // set video mode to 320x200 256 color mode
  46.  
  47. Set_Video_Mode(VGA256);
  48.  
  49. printf("\nHit any key to draw circles with internal sin and cosine.");
  50. getch();
  51.  
  52. // draw circles using built in sin and cos
  53.  
  54. for (index=0; index<NUM_CIRCLES; index++)
  55.      {
  56.  
  57.      // get a random circle
  58.  
  59.      radius = rand()%50;
  60.      xo     = rand()%320;
  61.      yo     = rand()%200;
  62.  
  63.      for (ang=0; ang<360; ang++)
  64.           {
  65.           x = xo + cos(ang*3.14159/180)*radius;
  66.           y = yo + sin(ang*3.14159/180)*radius;
  67.  
  68.           // plot the point of the circle with a little image space clipping
  69.  
  70.           if (x>=0 && x<320 && y>=0 && y<200)
  71.               Plot_Pixel_Fast(x,y,9);
  72.  
  73.           } // end for ang
  74.      } // end for index
  75.  
  76. // done, halt the system and wait for user to hit a key
  77.  
  78. printf("\nHit any key to see circles drawn with look up tables.");
  79. getch();
  80.  
  81. // set video mode to 320x200 256 color mode
  82.  
  83. Set_Video_Mode(VGA256);
  84.  
  85. // draw circles using look up tables
  86.  
  87. for (index=0; index<NUM_CIRCLES; index++)
  88.      {
  89.  
  90.      // get a random circle
  91.  
  92.      radius = rand()%50;
  93.      xo     = rand()%320;
  94.      yo     = rand()%200;
  95.  
  96.      for (ang=0; ang<FULL_CIRCLE; ang++)
  97.           {
  98.  
  99.           x = xo + cos_table[ang]*radius;
  100.           y = yo + sin_table[ang]*radius;
  101.  
  102.           // plot the point of the circle with a little image space clipping
  103.  
  104.           if (x>=0 && x<320 && y>=0 && y<200)
  105.               Plot_Pixel_Fast(x,y,12);
  106.  
  107.           } // end for ang
  108.      } // end for index
  109.  
  110. // let user hit a key to exit
  111.  
  112. printf("\nWow! Hit any key to exit.");
  113. getch();
  114.  
  115. // reset the video mode back to text
  116.  
  117. Set_Video_Mode(TEXT_MODE);
  118.  
  119. } // end main
  120.  
  121.