0

I have seen many apps which draw circles e.g pygame for python, p5.js for javascript. But I cannot find a method to find out points on a circle efficiently. My current solution to the problem involves trying out all the numbers in the square in which the circle can be inscribed.

A circle inscribed in a square

This can't be the most efficient method to do it. What is the method used at the industry level? Does it involve optimization or is it a whole new method?

ashir
  • 111
  • 7
  • 1
    using `x^2+y^2=r^2` is ok if you are rendering filled circle and pies and can be [optimized quite a lot](https://stackoverflow.com/a/61097673/2521214) however for just single pixel thick circle and arcs are better algorithms out there like Bresenham circle, ... or exploiting parametric circle equation with step slightly less than 1 pixel etc or render lines instead ... – Spektre Oct 05 '20 at 08:51

1 Answers1

1

A Midpoint Circle Algorithm could be used.

And an implementation e.g. in C from rosettacode.org:

#define plot(x, y) put_pixel_clip(img, x, y, r, g, b)
 
void raster_circle(
        image img,
        unsigned int x0,
        unsigned int y0,
        unsigned int radius,
        color_component r,
        color_component g,
        color_component b )
{
    int f = 1 - radius;
    int ddF_x = 0;
    int ddF_y = -2 * radius;
    int x = 0;
    int y = radius;
 
    plot(x0, y0 + radius);
    plot(x0, y0 - radius);
    plot(x0 + radius, y0);
    plot(x0 - radius, y0);
 
    while(x < y) 
    {
        if(f >= 0) 
        {
            y--;
            ddF_y += 2;
            f += ddF_y;
        }
        x++;
        ddF_x += 2;
        f += ddF_x + 1;    
        plot(x0 + x, y0 + y);
        plot(x0 - x, y0 + y);
        plot(x0 + x, y0 - y);
        plot(x0 - x, y0 - y);
        plot(x0 + y, y0 + x);
        plot(x0 - y, y0 + x);
        plot(x0 + y, y0 - x);
        plot(x0 - y, y0 - x);
    }
}
Renat
  • 7,718
  • 2
  • 20
  • 34