2

I want to get the values of pixels in a circumference in an image, and plot them. I know Circumference is C=2*pi*radius, but I am uncertain about how to iterate through all the points in a circle to get the pixel data.

To get a single pixel, this would work. But I need to get pixel values along a circles circumference. how should I iterate through to get that data?

$pixel=getPixel($image, $x, $y);
j0h
  • 1,675
  • 6
  • 27
  • 50
  • [How do I calculate a point on a circle’s circumference?](http://stackoverflow.com/q/839899) – mario Sep 12 '14 at 03:43

2 Answers2

1

One way to do this would be to copy the low level code used to plot the pixels when creating the image of a circle on the screen. This works by incrementing (or decrementing) one of the co-ordinates and then adjusting the other one so as to keep the same distance from the centre of the circle. To ensure it is symmetrical, you make sure that each octant of the circle is plotted in exactly the same way. Details at http://www.asksatyam.com/2011/01/bresenhams-circle-algorithm_22.html (Or http://en.wikipedia.org/wiki/Midpoint_circle_algorithm of course).

mcdowella
  • 19,301
  • 2
  • 19
  • 25
1

look at answers here 3D sphere boundary

it is 3D equivalent of your problem so it may help but if you need also the pixel order to be right then most likely is this not for you.

If you want speed use Bresenham

but for newbies it can be difficult to implement and even more to understand

if you want simplicity instead (or for start) then:

  1. use parametric circle equation

    x=x0+r*cos(t)
    y=y0+r*sin(t)
    

    which get you pixel position for the circle boundary while t= <0,2*pi) [rad] use deg or rad according to your sin,cos functions

  2. pixels only

    circle circumference is 2*pi*r [pixels] so step for parameter t should be small enough to reach as many points

    dt <= 2*pi/2*pi*r // whole circle / number of pixels
    dt <= 1/r         // let use half of that for safety
    

    so for extracting points use this C++ code:

        int x0=...,y0=...,r=...; // input values
        int xx=x0+r+r,yy=y0,x,y;
        double t,dt=0.5/r;
        for (t=0.0;t<2.0*M_PI;t+=dt)
         {
         x=x0+int(double(double(r)*cos(t)));
         y=y0+int(double(double(r)*sin(t)));
         if ((xx!=x)&&(yy!=y)) // check if the coordinates crossed pixel barrier
          {
          xx=x; yy=y;
          // here do what you need to do with pixel x,y
          }             
         }
    

    if there are holes inside your perimeter then lower the dt more. The less it is the smaller step you use but it also slows down the whole thing. You can have r as double or has its copy to avoid 2 int/double conversions. xx,yy are last used pixel coordinates to avoid processing single pixel multiple times. At start it is set point that is not inside circle for safety. if r==0 then you should set dt to some safety value like dt=M_PI;

Spektre
  • 49,595
  • 11
  • 110
  • 380
  • If I were using the Bresenham's circle algorithm, would I only need to iterate through x (or y)? – j0h Sep 12 '14 at 10:15
  • @j0h on mcdowella's answer is a link to it with C++ implementation, you for loop x and iterate y along 1/8 circle and putpixel's all the mirroring's to get full circle. that is why stop condition is `x<=y` and there are 8 putpixel calls per iteration – Spektre Sep 12 '14 at 10:58