Search code examples
javaalgorithmgeometryfill

Fill a circle on a grid without builtin methods?


I want to fill a circle in an image with a color without using the built-in methods such as drawOval(). I've found algorithms to draw the border of a circle, but I can't figure out how to make a filled circle.


Solution

  • Walk through rows with coordinates from center.Y - R to center.Y + R and fill horizontal lines of needed length.

     for dy = - R to  R
        dx  = Math.Sqrt(R * R - dy * dy)    //semi-width
        drawline(center.X - dx, center.Y + dy, center.X + dx, center.Y + dy)
    

    That's all.

    (If you already use some circle-drawing algorithm like midpoint one or Bresenham one, you can substitute dx and dy with values from that algorithm)