home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Interactive Guide / c-cplusplus-interactive-guide.iso / c_ref / csource4 / 287_01 / fcircle.c < prev    next >
Text File  |  1989-05-25  |  3KB  |  115 lines

  1. #include <stdio.h>
  2. #include <gds.h>
  3.  
  4. /*===========================================================*
  5.  *                                                           *
  6.  *  This file contains functions :-                          *
  7.  *      FillCircle(centerx,centery,radius)                   *
  8.  *          Draw a solid circle. Usage is same as Circle     *
  9.  *                                                           *
  10.  *      FillEllipse(centerx,centery,a,b)                     *
  11.  *          Draw a solid ellipse. Usage is same as Ellipse   *
  12.  *                                                           *
  13.  *===========================================================*/
  14.  
  15. FillCircle(centerx,centery,radius)
  16. int centerx,centery,radius;
  17. {
  18.     register int x,y;
  19.     int incr1,incr2,d,tmpstyle;
  20.  
  21.     if (radius <= 0) return;
  22.     if ((ORGX+centerx+radius < WINX1) || (ORGX+centerx-radius > WINX2) ||
  23.         (ORGY+centery+radius < WINY1) || (ORGY+centery-radius > WINY2)) return;
  24.     tmpstyle=STYLE;
  25.     STYLE=0xffff;
  26.     incr1=6;
  27.     incr2=10 - (radius << 2);
  28.     for (d=3-(radius<<1),x=0,y=radius; x<y; x++) {
  29.         HorzLine(centerx-y,centery+x,(y<<1)+1,1);
  30.         if (x) HorzLine(centerx-y,centery-x,(y<<1)+1,1);
  31.         if (d<0) {
  32.             d += incr1;
  33.             incr2 += 4;
  34.         } else {
  35.             HorzLine(centerx-x,centery+y,(x<<1)+1,1);
  36.             HorzLine(centerx-x,centery-y,(x<<1)+1,1);
  37.             d += incr2;
  38.             incr2 += 8;
  39.             y--;
  40.             }
  41.         incr1 += 4;
  42.         }
  43.     if (x==y) {
  44.         HorzLine(centerx-x,centery+y,(x<<1)+1,1);
  45.         HorzLine(centerx-x,centery-y,(x<<1)+1,1);
  46.         }
  47.     STYLE=tmpstyle;
  48. }
  49.  
  50. FillEllipse(ctrx,ctry,a,b)
  51. int ctrx,ctry,a,b;
  52. {
  53.     long incr1,incr2,a2,b2,d,step1,step2,test,a_b,step3;
  54.     int tmpstyle;
  55.     register int x,y;
  56.  
  57.     if ((a<=0) || (b <= 0)) return;
  58.     if ((ctrx+a < WINX1) || (ctrx-a > WINX2) ||
  59.         (ctry+b < WINY1) || (ctry-b > WINY2)) return;
  60.     tmpstyle=STYLE;
  61.     STYLE=0xffff;
  62.     x=0;
  63.     y=b;
  64.     a_b = (a2=(long) a*a) + (b2= (long) b*b);
  65.     step3=(step1=a2<<2)+(step2=b2<<2);
  66.     d=((b2-(test=a2*b)) << 1) + a2;
  67.     incr1=step2+(b2<<1);
  68.     incr2=incr1+step1-(test<<1);
  69.     incr2-=test << 1;
  70.     while (test>0) {
  71.         if (d<0) {
  72.             d+=incr1;
  73.             incr2+=step2;
  74.             test-=b2;
  75.         } else {
  76.             d+=incr2;
  77.             incr2+=step3;
  78.             test-=a_b;
  79.             HorzLine(ctrx-x,ctry+y,(x<<1)+1,1);
  80.             HorzLine(ctrx-x,ctry-y,(x<<1)+1,1);
  81.             y--;
  82.             }
  83.         incr1+=step2;
  84.         x++;
  85.         }
  86.     if (test==0) {
  87.         HorzLine(ctrx-x,ctry+y,(x<<1)+1,1);
  88.         HorzLine(ctrx-x,ctry-y,(x<<1)+1,1);
  89.         }
  90.     y=0;
  91.     x=a;
  92.     d=((a2-(test=b2*a)) << 1) + b2;
  93.     incr1=step1+(a2<<1);
  94.     incr2=incr1+step2-(test<<1);
  95.     incr2-=test<<1;
  96.     while (test>0) {
  97.         HorzLine(ctrx-x,ctry+y,(x<<1)+1,1);
  98.         if (y) HorzLine(ctrx-x,ctry-y,(x<<1)+1,1);
  99.         if (d<0) {
  100.             d+=incr1;
  101.             incr2+=step1;
  102.             test-=a2;
  103.         } else {
  104.             d+=incr2;
  105.             incr2+=step3;
  106.             test-=a_b;
  107.             x--;
  108.             }
  109.         incr1+=step1;
  110.         y++;
  111.         }
  112.     STYLE=tmpstyle;
  113. }
  114.  
  115.