15,850,103 members
5.00/5 (1 vote)
See more:
Given a Color[] TextureData representing the texture data (it's a one dimensional array), a Vector2 CircleCenter representing the center of the circle and a float R representing the circles radius; how would one go about determining every pixel that the area of the circle covers?
Posted

## Solution 4

Well, it doesn't seem like my question was clear enough and I created my own solution.

Assuming the center of the circle is at x,y and the radius is r we can create a square with the points x-r, x+r, y-r, and y+r. This gives us a square that contains the circle. Then we can go through every pixel inside that square and determine if it's inside the circle using Pythagoras's equation. Basically it foes something like

C#
```int Left = (int)centerOfExplosion.Value.X - bombRadius;
int Right = Left + bombRadius * 2;
int Top = (int)centerOfExplosion.Value.Y - bombRadius;
int Bottom = Top + bombRadius * 2;
for (int j = Top; j <= Bottom; ++j)
{
for (int k = Left; k <= Right; ++k)
{
double dist = Math.Pow(centerOfExplosion.Value.X - k, 2.0) + Math.Pow(centerOfExplosion.Value.Y - j, 2.0);
{
Color c = MapColorData[k + j * Map01Color.Width];
c.A = 0;
MapCollisionData[k + j * Map01Collision.Width] = Color.White;
MapColorData[k + j * Map01Color.Width] = c;
}
}
}```

## Solution 5

Josh : good idea comparing with the distance squared instead of computing a square root every time. Another performance improvement would be to compute the square of bombRadius once and save it in a temporary variable instead of computing for each point since it does not change.

## Solution 3

Seems that your requirement is just inverse of painting a circle. For painting we use 8-way symmetry algorithm or similar algorithms. You can use the same algorithms and in place of painting the pixed pick the pixel from the image. This link can be helpful 8-way symmetry algorithm[^]

## Solution 1

Hi Josh_Jackson,
I am not sure if I have understood what you really mean or not, but it does not look a challenging problem.

```Bitmap bmp = new Bitmap(100, 100);
// Change Bitmap here to image you aim
for (int i = 0; i<bmp.Width ; i++)
for (int j = 0; j<bmp.Height ; j++)
if((i-CenterX)*(i-CenterX)+(j-CenterY)*(j-CenterY)< R*R)
{//Inside the Circle
Color c = bmp.GetPixel(i, j);
if(c==.../* My favorite color */)
{
//...
```

Did you mean that?

## Solution 2

This question makes no sense at all. You have 1 dimensional array containing texture data and a tuple (C, R) where C is the center and R is the radius of circle.

How does one use this information to determine every pixel circle covers?

-Saurabh

Top Experts
Last 24hrsThis month
 Dave Kreskowiak 33 _Asif_ 20 CPallini 10 OriginalGriff 10 Matt Bond 10
 Richard Deeming 40 _Asif_ 30 OriginalGriff 30 Richard MacCutchan 20 Dave Kreskowiak 18

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900