RSS

Algorithm to draw a circle

Fri, Mar 5, 2010

Algorithm

Method 1) Slower Method
 
public void circleSimple(int xCenter, int yCenter, int radius, Color c)
{
         int pix = c.getRGB();
         int x, y, r2;
        
         r2 = radius * radius;
         for (x = -radius; x <= radius; x++) 
         {
           y = (int) (Math.sqrt(r2 – x*x) +  0.5);
           raster.setPixel(pix, xCenter + x,  yCenter + y);
           raster.setPixel(pix, xCenter + x,  yCenter – y);
         }
}
 
 

Method 2) Fastest Method
 
public void circleSym4(int xCenter, int yCenter, int radius, Color c)
{
         int pix = c.getRGB();
         int x, y, r2;
        
         r2 = radius * radius;
         raster.setPixel(pix, xCenter, yCenter +  radius);
         raster.setPixel(pix, xCenter, yCenter -  radius);
         for (x = 1; x <= radius; x++)
         {
           y = (int) (Math.sqrt(r2 – x*x) +  0.5);
           raster.setPixel(pix, xCenter + x,  yCenter + y);
           raster.setPixel(pix, xCenter + x,  yCenter – y);
           raster.setPixel(pix, xCenter – x, yCenter  + y);
           raster.setPixel(pix, xCenter – x,  yCenter – y);
         }
}

Sharing ~ Helping Other:
  • Print
  • email
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • BlinkList
  • DZone
  • Slashdot
  • YahooMyWeb
  • StumbleUpon
  • Live
  • IndianPad
  • DotNetKicks
  • Technorati

Other Posts:

This post was written by:

eXclusiveMinds - who has written 500 posts on eXclusiveMinds.


Contact the author

Leave a Reply

You must be logged in to post a comment.