Click here to Skip to main content
15,850,103 members
Please Sign up or sign in to vote.
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

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);
        if (dist <= Math.Pow(bombRadius, 2))
        {
            Color c = MapColorData[k + j * Map01Color.Width];
            c.A = 0;
            MapCollisionData[k + j * Map01Collision.Width] = Color.White;
            MapColorData[k + j * Map01Color.Width] = c;
        }
    }
}
 
Share this answer
 
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.
 
Share this answer
 
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[^]
 
Share this answer
 
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?
 
Share this answer
 
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



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