#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

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

edited April 2016

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?

Tagged:

• Thanks, looks like this is the "from scratch" approach. I tried looking at some homemade circles compared to what `ellipse` gives. This question has a code snippet that does both.
It turns out the `ellipse()`-drawn circles really do look much smoother. Rather than reverse-engineering their rasterization technique, I was hoping there was a way I could get it for free.