C# 在XNA中绘制简单的圆
我想画一个二维填充圆。我到处找了,似乎找不到任何能帮我画一个圆圈的东西。我只想在画布上指定高度、宽度和位置 有人知道怎么做吗C# 在XNA中绘制简单的圆,c#,xna,C#,Xna,我想画一个二维填充圆。我到处找了,似乎找不到任何能帮我画一个圆圈的东西。我只想在画布上指定高度、宽度和位置 有人知道怎么做吗 谢谢 开箱即用,XNA不支持这一点。我假设你来自某个GDI背景,只想看到屏幕上有东西在移动。但在真正的游戏中,这是很少需要的 这里有一些有用的信息: 我给你的建议是,只需启动绘画或其他东西,然后自己创建基本形状并使用内容管道。XNA通常不知道可以在画布上绘画。相反,您可以在您喜爱的绘制程序中创建一个圆并将其渲染为精灵,或者在3D网格中创建一系列顶点以近似圆并进行渲染。另
谢谢 开箱即用,XNA不支持这一点。我假设你来自某个GDI背景,只想看到屏幕上有东西在移动。但在真正的游戏中,这是很少需要的 这里有一些有用的信息:
我给你的建议是,只需启动绘画或其他东西,然后自己创建基本形状并使用内容管道。XNA通常不知道可以在画布上绘画。相反,您可以在您喜爱的绘制程序中创建一个圆并将其渲染为精灵,或者在3D网格中创建一系列顶点以近似圆并进行渲染。另一个选项(如果您想使用更复杂的渐变笔刷或其他工具)就是绘制一个与屏幕对齐的四边形并使用像素着色器。您还可以查看Jeff Weber在Farseer中使用的示例框架:
演示有一个动态纹理生成器,让他制作圆形和矩形(然后样本将其用作物理模拟的可视化)。你可以重复使用:-)也有同样的问题,因为其他人已经建议你需要绘制一个带有圆形纹理的正方形或矩形。下面是我创建圆形纹理运行时的方法。这不是最有效或最奇特的方法,但它确实有效
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;
}
Texture2D createCircleText(整数半径)
{
纹理2D纹理=新纹理2D(图形设备、半径、半径);
颜色[]颜色数据=新颜色[半径*半径];
浮动直径=半径/2f;
浮动直径SQ=直径*直径;
对于(int x=0;x公共纹理2D createCircleText(整数半径、图形设备、颜色、整数标记)
{
纹理2D纹理=新纹理2D(设计、半径、半径);
颜色[]颜色数据=新颜色[半径*半径];
如果(tickens>=半径)tickens=半径-5;
浮动直径=半径/2f;
浮动直径SQ=直径*直径;
浮动内径=(半径标记)/2f;
浮动intdiamsq=intdiam*intdiam;
对于(int x=0;x if(pos.LengthSquared()我解决这个问题的方法是绘制一个矩形纹理,使不包含圆的矩形区域保持透明。检查数组中的点是否包含在从矩形中心开始的圆内
使用颜色数据数组有点奇怪,因为它不是2D数组。我的解决方案是在场景中引入一些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 Texture2D GetColoredCircle(浮动半径、所需颜色颜色)
{
半径=半径/2;
整数宽度=(整数)半径*2;
整数高度=宽度;
矢量2中心=新矢量2(半径,半径);
圆=新圆(中心、半径、假);
颜色[]数据颜色=新颜色[宽度*高度];
int row=-1;//在第一次迭代时增加到零!
int列=0;
for(int i=0;i
下面是检查圆中是否包含点的方法:
public bool ContainsPoint(Vector2 point)
{
return ((point - this.Center).Length() <= this.Radius);
}
public bool ContainsPoint(矢量2点)
{
return((point-this.Center).Length()。如果交换这些变量名会更好。
public bool ContainsPoint(Vector2 point)
{
return ((point - this.Center).Length() <= this.Radius);
}