home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 432b.lha / EzLib / demo_src / sin / sin.c < prev   
Encoding:
C/C++ Source or Header  |  1990-11-11  |  3.1 KB  |  114 lines

  1. /* This program draws Lissajous curves.  It is taken directly from the
  2.  * chapter 3 exercises in a book called _Computer Graphics: The Principles
  3.  * Behind the Art and Science_.  That book is written by Pokorny &
  4.  * Gerald.
  5.  *
  6.  * Believe it or not the Amiga is actually mentioned in that book! ;^)
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <math.h>
  11. #include <ezlib.h>
  12.  
  13. #define MAX 320
  14.  
  15. double sin_tab[360], cos_tab[360];
  16.  
  17. /* first we allocate two screens, set up some colors and make sure that
  18.  * all worked.
  19.  *
  20.  * Then we fill in a table so we don't have keep calling the sin() function
  21.  * when draw the actual curves.
  22.  *
  23.  * Finally we scroll the picture off the screen using a simple double
  24.  * buffering technique.
  25.  */
  26.  
  27. main()
  28. {
  29.  register int i;
  30.  int j, count = 0;
  31.  double ang, step, hold_x, hold_y;
  32.  struct Screen *screen1, *screen2;
  33.  struct BitMap *bm1, *bm2;
  34.  struct RastPort *rp, *rp2;
  35.  
  36.  screen2 = makescreen(HIRES, 3);
  37.  if (screen2 == NULL)
  38.    exit(10L);
  39.  
  40.  setcolor(screen2, 0, BLACK); setcolor(screen2, 1, WHITE);
  41.  setcolor(screen2, 2, BLUE);  setcolor(screen2, 5, GREEN);
  42.  setcolor(screen2, 3, PINK);  setcolor(screen2, 4, ORANGE);
  43.  setcolor(screen2, 6, GREY);  setcolor(screen2, 7, RED);
  44.  
  45.  screen1 = makescreen(HIRES, 3);
  46.  if (screen1 == NULL)
  47.    { killscreen(screen2); exit(10L); }
  48.  
  49.  setcolor(screen1, 0, BLACK); setcolor(screen1, 1, WHITE);
  50.  setcolor(screen1, 2, BLUE);  setcolor(screen1, 5, GREEN);
  51.  setcolor(screen1, 3, PINK);  setcolor(screen1, 4, ORANGE);
  52.  setcolor(screen1, 6, GREY);  setcolor(screen1, 7, RED);
  53.  
  54.  rp = &screen1->RastPort;  rp2 = &screen2->RastPort;
  55.  bm1 = &screen1->BitMap;   bm2 = &screen2->BitMap;
  56.  
  57.  /* this loop fills up a table with a sine curve     */
  58.  /* the magic number 0.01745 is radians per degree     */
  59.  for(i=0; i < 360; i++) {
  60.    sin_tab[i] = sin(0.01745 * (double)i);
  61.    cos_tab[i] = cos(0.01745 * (double)i);
  62.  }
  63.  
  64.  step = (double)360 /  (double)MAX;
  65.  Move(rp, 320, 100+90);
  66.  for(j=90; j > 30; j -= 2) {
  67.    ang = 0.0;
  68.    for(i=0; i < MAX; i++) {
  69.      hold_x =  sin_tab[ (2 * (int)ang) % 360] * (double)j * 2.0;
  70.      hold_y =  cos_tab[ (3 * (int)ang) % 360] * (double)j;
  71.      Draw(rp, 320+ (int)hold_x, 100+(int)hold_y);
  72.      ang = ang + step;
  73.    }
  74.    SetAPen(rp, (count++ % 8));
  75.  }
  76.  
  77.  Delay(125);
  78.  
  79.  /* now the curve has been fully drawn to the screen.
  80.   * First we'll copy it to the second screen behind this one, then
  81.   * we'll double buffer it off the screen
  82.   */
  83.  
  84.  /* scroll curve smoothly off the screen using double buffering */
  85.  for(i=140; i > 4; i-=8) {
  86.    WaitTOF();
  87.    BltBitMap(bm1, i,0,  bm2, i-4,0,  380,200,  0x0c0,0xff);
  88.    ScreenToFront(screen2);
  89.    WaitTOF();
  90.    BltBitMap(bm2, i-4,0,  bm1, i-8,0,  380,200,  0x0c0,0xff);
  91.    ScreenToFront(screen1);
  92.  }
  93.  
  94.  /* reached left edge, so from here we do things slightly different
  95.   * than above
  96.   */
  97.  for(i=2; i < 70 ; i++) {
  98.    WaitTOF();
  99.    BltBitMap(bm1, i,0,  bm2, 0,0,  380-i,200,  0x0c0,0xff);
  100.    ScreenToFront(screen2);
  101.    WaitTOF();
  102.    BltBitMap(bm2, i,0,  bm1, 0,0,  380-i,200,  0x0c0,0xff);
  103.    ScreenToFront(screen1);
  104.  }
  105.  
  106.  /* de-allocate everything and exit.  Simple eh? ;^) */
  107.  killscreen(screen1);
  108.  killscreen(screen2);
  109.  
  110.  closelibs();
  111.  exit(0L);
  112. }
  113.  
  114.