【发布时间】:2010-06-06 10:12:23
【问题描述】:
XNA 没有任何支持画圆的方法。
通常,当我必须绘制圆形时,总是使用相同的颜色,我只是用那个圆形制作图像,然后我可以将它显示为精灵。
但是现在圆圈的颜色是在运行时指定的,有什么想法可以处理吗?
【问题讨论】:
-
我记得在 XNA 的论坛上读过类似的东西。
标签: colors drawing xna geometry
XNA 没有任何支持画圆的方法。
通常,当我必须绘制圆形时,总是使用相同的颜色,我只是用那个圆形制作图像,然后我可以将它显示为精灵。
但是现在圆圈的颜色是在运行时指定的,有什么想法可以处理吗?
【问题讨论】:
标签: colors drawing xna geometry
您可以简单地制作一个带有Transparent 背景的圆圈图像,圆圈的彩色部分为White。然后,在Draw() 方法中绘制圆圈时,选择您想要的色调:
Texture2D circle = CreateCircle(100);
// Change Color.Red to the colour you want
spriteBatch.Draw(circle, new Vector2(30, 30), Color.Red);
只是为了好玩,这里是 CreateCircle 方法:
public Texture2D CreateCircle(int radius)
{
int outerRadius = radius*2 + 2; // So circle doesn't go out of bounds
Texture2D texture = new Texture2D(GraphicsDevice, outerRadius, outerRadius);
Color[] data = new Color[outerRadius * outerRadius];
// Colour the entire texture transparent first.
for (int i = 0; i < data.Length; i++)
data[i] = Color.TransparentWhite;
// Work out the minimum step necessary using trigonometry + sine approximation.
double angleStep = 1f/radius;
for (double angle = 0; angle < Math.PI*2; angle += angleStep)
{
// Use the parametric definition of a circle: http://en.wikipedia.org/wiki/Circle#Cartesian_coordinates
int x = (int)Math.Round(radius + radius * Math.Cos(angle));
int y = (int)Math.Round(radius + radius * Math.Sin(angle));
data[y * outerRadius + x + 1] = Color.White;
}
texture.SetData(data);
return texture;
}
【讨论】: