home *** CD-ROM | disk | FTP | other *** search
/ 8bitfiles.net/archives / archives.tar / archives / genie-commodore-file-library / C128CPM / SGTOOLS17.ARC / VDCELLIP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-01  |  1.4 KB  |  73 lines

  1. /*
  2. SG C Tools 1.7
  3.  
  4. (C) 1994 Steve Goldsmith
  5. All Rights Reserved
  6.  
  7. Compiled with HI-TECH C 3.09 (CP/M-80).
  8. */
  9.  
  10. #include <hitech.h>
  11. #include <vdc.h>
  12.  
  13. /*
  14. Draw ellipse using digital differential analyzer (DDA) method.
  15. */
  16.  
  17. void ellipsevdc(int XC, int YC, int A, int B)
  18. {
  19.   long AA = (long) A*A; /* a^2 */
  20.   long BB = (long) B*B; /* b^2 */
  21.   long AA2 = AA << 1;   /* 2(a^2) */
  22.   long BB2 = BB << 1;   /* 2(b^2) */
  23.  
  24.   {
  25.     long X = 0;
  26.     long Y = B;
  27.     long XBB2 = 0;
  28.     long YAA2 = Y*AA2;
  29.     long ErrVal = -Y*AA; /* b^2 x^2 + a^2 y^2 - a^2 b^2 -b^2x */
  30.  
  31.     while (XBB2 <= YAA2) /* draw octant from top to top right */
  32.     {
  33.       setpixvdc(XC+X,YC+Y);
  34.       setpixvdc(XC+X,YC-Y);
  35.       setpixvdc(XC-X,YC+Y);
  36.       setpixvdc(XC-X,YC-Y);
  37.       X += 1;
  38.       XBB2 += BB2;
  39.       ErrVal += XBB2-BB;
  40.       if (ErrVal >= 0)
  41.       {
  42.         Y -= 1;
  43.         YAA2 -= AA2;
  44.         ErrVal -= YAA2;
  45.       }
  46.     }
  47.   }
  48.   {
  49.     long X = A;
  50.     long Y = 0;
  51.     long XBB2 = X*BB2;
  52.     long YAA2 = 0;
  53.     long ErrVal = -X*BB;
  54.  
  55.     while (XBB2 > YAA2)  /* draw octant from right to top right */
  56.     {
  57.       setpixvdc(XC+X,YC+Y);
  58.       setpixvdc(XC+X,YC-Y);
  59.       setpixvdc(XC-X,YC+Y);
  60.       setpixvdc(XC-X,YC-Y);
  61.       Y += 1;
  62.       YAA2 += AA2;
  63.       ErrVal += YAA2-AA;
  64.       if (ErrVal >= 0)
  65.       {
  66.         X -= 1;
  67.         XBB2 -= BB2;
  68.         ErrVal -= XBB2;
  69.       }
  70.     }
  71.   }
  72. }
  73.