How to enumerate the pixels in a region defined by a particular ellipse

edited April 2016 in How To...

Let's say I want to calculate the average brightness in a particular region of the display. If that region is a rectangle, with top-left corner (a, b), width w, and height h, I can just do something like.

for (int x=a; x<a+w; x++) {
  for (int y=b; y<b+h; y++) {
    // do something with pixel at (x, y)
  }
}

But let's say that instead I want to know the brightness of the pixels in the circle centered at (a, b) with radius r. How would I do that?

I could imagine doing some math to figure out which pixels are enclosed in the circle, but it seems like there are some rasterization subtleties here - i.e. some pixels are going to contain only a portion of the circle, so I probably need to at least define some threshold, and maybe apply some other techniques to avoid weird aliasing effects. Ideally, I'd like to just get the range of pixels that would be affected if I called ellipse(a, b, r, r)'for free'. Is this possible?

Answers

Sign In or Register to comment.