Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all pixel array inside circle

Tags:

xna

I have this:

enter image description here

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

Thanks.

like image 264
Yuri Morales Avatar asked Jan 23 '13 19:01

Yuri Morales


People also ask

How do you find all points in a circle?

Distance Formula for a Point and the Center of a Circle: d=√(x−h)2+(y−k)2 d = ( x − h ) 2 + ( y − k ) 2 , where (x, y) is the point and (h,k) is the center of the circle. This formula is derived from the Pythagorean Theorem.


1 Answers

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);
        }
    }
}
like image 154
Lucius Avatar answered Sep 25 '22 03:09

Lucius