Get all pixel array inside circle

2020-03-01 18:19发布

问题:

I have this:

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

Thanks.

回答1:

You are looking for the following set of pixels:

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);
        }
    }
}


标签: xna