如何在XNA中绘制具有特定颜色的圆?
XNA没有任何支持圆圈绘制的方法。
通常,当我不得不绘制圆圈时,总是使用相同的颜色,我只是使用该圆圈创建图像,然后可以将其显示为
但是现在圆的颜色是在运行时指定的,任何想法如何处理呢?
XNA doesn't have any methods which support circle drawing.
Normally when I had to draw circle, always with the same color, I just made image with that circle and then I could display it as a sprite.
But now the color of the circle is specified during runtime, any ideas how to deal with that?
您可以简单地使用透明
背景创建圆形的图像,圆形的彩色部分为白色
。然后,当在 Draw()
方法中绘制圆形时,选择色调作为您想要的颜色:
You can simply make an image of a circle with a Transparent
background and the coloured part of the circle as White
. Then, when it comes to drawing the circles in the Draw()
method, select the tint as what you want it to be:
Texture2D circle = CreateCircle(100);
// Change Color.Red to the colour you want
spriteBatch.Draw(circle, new Vector2(30, 30), Color.Red);
为了有趣,这里是CreateCircle方法:
Just for fun, here is the CreateCircle method:
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;
}