【发布时间】:2010-03-25 20:56:29
【问题描述】:
我想画一个 2d 的实心圆。我到处寻找,似乎找不到任何可以远程帮助我画一个圆圈的东西。我只想在画布上指定高度和宽度以及位置。
有人知道怎么做吗?
谢谢!
【问题讨论】:
我想画一个 2d 的实心圆。我到处寻找,似乎找不到任何可以远程帮助我画一个圆圈的东西。我只想在画布上指定高度和宽度以及位置。
有人知道怎么做吗?
谢谢!
【问题讨论】:
XNA 通常不知道您可以在其上绘画的画布。相反,您可以在自己喜欢的绘图程序中创建一个圆并将其渲染为精灵,或者在 3D 网格中创建一系列顶点来近似一个圆并进行渲染。
【讨论】:
您还可以查看 Jeff Weber 在 Farseer 中使用的示例框架:
http://www.codeplex.com/FarseerPhysics
演示中有一个动态纹理生成器,可以让他制作圆形和矩形(示例随后将其用作物理模拟的可视化)。你可以重复使用它:-)
【讨论】:
遇到了同样的问题,因为其他人已经建议您需要绘制一个带有圆形纹理的正方形或矩形。下面是我创建圆形纹理运行时的方法。这不是最有效或最花哨的方法,但它确实有效。
Texture2D createCircleText(int radius)
{
Texture2D texture = new Texture2D(GraphicsDevice, radius, radius);
Color[] colorData = new Color[radius*radius];
float diam = radius / 2f;
float diamsq = diam * diam;
for (int x = 0; x < radius; x++)
{
for (int y = 0; y < radius; y++)
{
int index = x * radius + y;
Vector2 pos = new Vector2(x - diam, y - diam);
if (pos.LengthSquared() <= diamsq)
{
colorData[index] = Color.White;
}
else
{
colorData[index] = Color.Transparent;
}
}
}
texture.SetData(colorData);
return texture;
}
【讨论】:
开箱即用,XNA 中不支持此功能。我假设你来自一些 GDI 背景,只是想在屏幕上看到一些移动的东西。但在真正的游戏中,如果需要的话,这很少见。
这里有一些有用的信息:
http://forums.xna.com/forums/t/7414.aspx
我对你的建议是直接启动油漆或其他东西,然后自己创建基本形状并使用内容管道。
【讨论】:
另一种选择(如果您想使用更复杂的渐变笔刷或其他东西)是绘制与屏幕对齐的四边形并使用像素着色器。
【讨论】:
我为解决这个问题所做的是绘制一个矩形纹理,使矩形中不包含圆形的区域保持透明。您检查数组中的一个点是否包含在源自矩形中心的圆内。
使用颜色数据数组有点奇怪,因为它不是二维数组。我的解决方案是在场景中引入一些 2D 数组逻辑。
public Texture2D GetColoredCircle(float radius, Color desiredColor)
{
radius = radius / 2;
int width = (int)radius * 2;
int height = width;
Vector2 center = new Vector2(radius, radius);
Circle circle = new Circle(center, radius,false);
Color[] dataColors = new Color[width * height];
int row = -1; //increased on first iteration to zero!
int column = 0;
for (int i = 0; i < dataColors.Length; i++)
{
column++;
if(i % width == 0) //if we reach the right side of the rectangle go to the next row as if we were using a 2D array.
{
row++;
column = 0;
}
Vector2 point = new Vector2(row, column); //basically the next pixel.
if(circle.ContainsPoint(point))
{
dataColors[i] = desiredColor; //point lies within the radius. Paint it.
}
else
{
dataColors[i] = Color.Transparent; //point lies outside, leave it transparent.
}
}
Texture2D texture = new Texture2D(GraphicsDevice, width, height);
texture.SetData(0, new Rectangle(0, 0, width, height), dataColors, 0, width * height);
return texture;
}
这是检查一个点是否包含在您的圈子内的方法:
public bool ContainsPoint(Vector2 point)
{
return ((point - this.Center).Length() <= this.Radius);
}
希望这会有所帮助!
【讨论】:
public Texture2D createCircleText(int radius, GraphicsDevice Devise,Color color,int tickenes)
{
Texture2D texture = new Texture2D(Devise, radius, radius);
Color[] colorData = new Color[radius * radius];
if (tickenes >= radius) tickenes = radius - 5;
float diam = radius / 2f;
float diamsq = diam * diam;
float intdiam = (radius-tickenes) / 2f;
float intdiamsq = intdiam * intdiam;
for (int x = 0; x < radius; x++)
{
for (int y = 0; y < radius; y++)
{
int index = x * radius + y;
Vector2 pos = new Vector2(x - diam, y - diam);
if (pos.LengthSquared() <= diamsq)
{
colorData[index] = color;
}
else
{
colorData[index] = Color.Transparent;
}
if (pos.LengthSquared() <= intdiamsq)
{
colorData[index] = Color.Transparent;
}
}
}
texture.SetData(colorData);
return texture;
}
【讨论】: