得到一个圆圈覆盖的每个像素 [英] Get every pixel a circle covers

查看:1138
本文介绍了得到一个圆圈覆盖的每个像素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出代表纹理数据(它是一维数组)的Color [] TextureData,代表圆心的Vector2 CircleCenter和代表圆半径的float R;

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?

推荐答案

好吧,看来我的问题还不够清楚,我创建了我自己的解决方案.

假设圆心在x,y处,半径为r,我们可以创建一个包含点x-r,x + r,y-r和y + r的正方形.这给了我们一个包含圆的正方形.然后,我们可以遍历该正方形内的每个像素,并使用毕达哥拉斯方程确定它是否在圆内.基本上它是针对

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

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


Josh:比较距离平方的好主意,而不是每次都计算平方根.另一个性能改进将是只计算一次bombRadius的平方,然后将其保存在一个临时变量中,而不是针对每个点进行计算,因为它不会改变.
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.


这个问题根本没有道理.您有一个包含纹理数据和元组(C,R)的一维数组,其中C是中心,R是圆的半径.

如何使用此信息来确定每个像素圈的覆盖范围?

-Saurabh
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


这篇关于得到一个圆圈覆盖的每个像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆