Help w/Plotting a circle...

Dave Harris Dave.Harris at f14.n15.z1.fidonet.org
Mon Jun 17 00:18:15 AEST 1991


In a message of <Jun 14 17:52>, William Bishop (1:114/15) writes: 
 >Hi,

 >Does anyone out there have an algorithim for plotting the points
 >of a circle.   Any all all help is appreciated.  Thanks!

You only need to plot 1/8th of the circle then relect it about y=x, then x=0, 
then y=0.  The obvious solution is to use x^2 + y^2 = r^2 then set y= 
sqrt(r^2-x^2) then plot the x and y until x is less than y.  On each of these 
points plot (x,y), (y,x), (-x,y), (x, -y), (-x, -y), (-y, x), (y, -x), and 
(-y, -x).  Add your center point to each of these coordinates before plotting. 
 

Now if you need some real speed, use the Brezenheim circle alogithm.  It 
reduces the problem such that you do not need to call the sqrt() function.

circle(int radius, int xcenter, int ycenter)
{
   int x,y,s;

   x = 0;
   y = radius;
   s = 3-2*radius;

   while (x<=y)
   {
      plot_eight(x,y,xcenter,ycenter);
      if (s <= 0)
         s+= 4*x+6;
      else
      {
         s+= 4*(x-y)+10;
         y--;
      }
      x++;
   }
}

#define plot_eight(int x, int y, int xcenter, int ycenter)  \
{                                                           \
   register int t1,t2,t3,t4;                                \
                                                            \
   t1 = x+xcenter;                                          \
   t2 = -x+xcenter;                                         \
   t3 = y+ycenter;                                          \
   t4 = -y+ycenter;                                         \

   plot(t1, t3);    /* actual pixel plot function */        \
   plot(t2, t3);                                            \
   plot(t1, t4);                                            \
   plot(t2, t4);                                            \

   plot(t3, t1);                                            \
   plot(t3, t2);                                            \
   plot(t4, t1);                                            \
   plot(t4, t2);                                            \
}

That should be real close to what you want anyway.  plot() probably takes a 
color as well but you get the idea.  I made plot_eight a macro for additional 
speed.  This should be within about 10% of the maximum speed possible for a 
circle algorithm.  Notice that there are no floating points or power functions 
needed.  That makes this function very fast.

Dave Harris.
 


 

--  
Uucp: ...{gatech,ames,rutgers}!ncar!asuvax!stjhmc!15!14!Dave.Harris
Internet: Dave.Harris at f14.n15.z1.fidonet.org



More information about the Comp.lang.c mailing list