Yuri Morales
Yuri Morales

Reputation: 2518

Get all pixel array inside circle

I have this:

enter image description here

And I need to know all pixels in array inside the circle.

Thanks.

Upvotes: 15

Views: 11848

Answers (2)

Blanko
Blanko

Reputation: 11

Shouldn´t this be an more efficient approach? (Don't iterating over the whole picture, just the desired square)

List<int> indices = new List<int>();

xmin = m1 - r;
xmax = m1 + r;
ymin = m2 - r;
ymax = m2 + r;

for (int x = xmin; x < xmax ; x++)
{
    for (int y = ymin; y < ymax; y++)
    {
        double dx = x - m1;
        double dy = y - m2;
        double distanceSquared = dx * dx + dy * dy;

        if (distanceSquared <= radiusSquared)
        {
            indices.Add(x + y * width);
        }
    }
}

Upvotes: 1

Lucius
Lucius

Reputation: 3745

You are looking for the following set of pixels:

Circle equation

with r being the radius of your circle and (m1, m2) the center.

In order to get these pixels iterate over all positions and store those which meet the criteria in a list:

List<int> indices = new List<int>();

for (int x = 0; x < width; x++)
{
    for (int y = 0; y < height; y++)
    {
        double dx = x - m1;
        double dy = y - m2;
        double distanceSquared = dx * dx + dy * dy;

        if (distanceSquared <= radiusSquared)
        {
            indices.Add(x + y * width);
        }
    }
}

Upvotes: 26

Related Questions