XNA 绘制实心圆
在 XNA 上的另一个线程中,Callum Rogers 编写了一些代码,它创建了具有圆形轮廓的纹理,但我正在尝试创建一个填充颜色的圆圈。我必须对此代码进行哪些修改才能用颜色填充圆圈?
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.Transparent;
// 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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不要对这样的东西使用纹理(特别是对于单一颜色的东西!) - 也不要尝试逐像素地进行。您获得 3D 加速是有原因的。
只需使用三角扇画出类似于饼图的圆圈即可。您将需要以下顶点。
前两个点将定义圆心与其边界之间的线。第三个顶点将定义第一个多边形。然后,顶点 1、3 和 4 将定义第二个多边形,依此类推。
要获取圆边界上的点,请使用示例中的公式。第一个角度为 0°,后面的角度为(360°/圆上的点)的倍数。要获得完整的圆,您需要一个与第二个点(边界上的第一个点)相匹配的附加点。
根据圆上顶点的数量,你会得到不同的多边形。使用的顶点越多,形状看起来就越圆(需要一定的性能成本):(
实际上 绘制 primites 的 XNA 示例 展示了如何使用三角形扇形来绘制圆形(或 n 边形)。
Don't use a texture for stuff like this (especially for things being in one single color!) - also don't try to do it pixel by pixel. You've got 3D acceleration for a reason.
Just draw the circle similar to a pie using a triangle fan. You'll need the following vertices.
The first two points will define a line between the center of the circle and its border. The third vertex will define the first polygon. Vertices 1, 3 and 4 will then define the second polygon, etc.
To get the points on the circle's border use the formulas from your example. The first angle will be 0°, the following ones multiples of (360° / points on circle). To get a full circle you'll need one additional point that matches the second point (the first point on the border).
Depending on the number of vertices on the circle you'll get different n-gons. The more vertices you use the rounder the shape will look (at some performance cost):
Actually the XNA example for drawing primites show how to draw a circle (or n-gon) using a triangle fan.
对于任何想要逐像素执行此操作的人来说,这都是好事......我根据给出的信息提出了一个解决方案。在 2d 纹理方法中添加以下代码来填充圆圈。我正在制作一款游戏,希望能够制作不同颜色和大小的圆圈。因此,在 CreateCircle(int radius) 方法中,创建轮廓后添加以下代码:
well for anyone who wants to do it pixel by pixel ... i made a solution based on the information given. In your 2d texture method add the following code to fill the circle. I'm making a game and wanted to be able to make circles different colors and sizes. So inside CreateCircle(int radius) method, add the following code after the outline has been created :
如果您需要从头开始(尽管我猜有更简单的方法),请更改执行渲染的方式。不要迭代角度并绘制像素,而是迭代像素并确定它们相对于圆的位置。如果它们是,则绘制为填充颜色。如果是
~= R
,则绘制为边框颜色。If you need to do it from scratch (though I'm guessing there are easier ways), change the way you perform the rendering. Instead of iterating through angles and plotting pixels, iterate through pixels and determine where they are relative to the circle. If they are
<R
, draw as fill color. If they are~= R
, draw as border color.我知道我有点晚了,但我修改了你的代码以填写中心
I know that I'm a little late, but I modified your code to fill in the center