在 XNA 中绘制椭圆 [英] Draw an Ellipse in XNA
本文介绍了在 XNA 中绘制椭圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试编写代码以在 XNA 中绘制椭圆.假设我们有以下参数:
I'm trying to write a code to draw an ellipse in XNA. Suppose we have the following parameters:
- 半长轴 (a).
- 半短轴 (b).
- 椭圆中心 (h,k).
- 和 2D 旋转 (theta).
如何使用上述参数绘制椭圆.
How it possible to draw the ellipse using the above parameters.
这是函数
public VertexPositionColor Set2dEllipse(int x, int y, int z, Color color)
{
VertexPositionColor vertices= new VertexPositionColor[100];
for(int i= 0;i<100;i++)
{
double angle = (i / 100 * Math.PI * 2);
vertices[i].Position = New Vector3((x + (Math.Cos(angle)) * size.Width), (y + Math.Sin(angle) * size.Height), z);
vertices[i].Color = color;
}
}
推荐答案
我修改了取自 此处 满足您的要求.
I've modified the code taken from here to meet your requirements.
public void CreateEllipse(int a, int b, int h, int k, float theta)
{
VertexPositionColor[] vertices = new VertexPositionColor[vertexCount];
//Drawing an Ellipse with its major axis parallel to the x-axis. Rotation can be applied to change this.
Vector3 position;
const float max = MathHelper.Pi;
//2 * max since we're moving from -Pi to +Pi in the loop.
float step = 2 * max / (float)vertexCount;
int i = 0;
//Optional Axis and angle rotation for the ellipse (See later notes):
//Vector3 axis = new Vector3(0, 0, -1);
float angle = MathHelper.ToRadians(theta);
for (float t = -max; t <= max; t += step)
{
//Formula shamelessly taken from wikipedia
position = new Vector3(h + a * (float)Math.Cos((double)t), k + b * (float)Math.Sin((double)t), 0f);
//Optional Rotation for the Ellipse:
//position = Vector3.Transform(position, Matrix.CreateFromAxisAngle(axis, angle));
vertices[i] = new VertexPositionColor(position, Color.DarkOrange);
i++;
}
//Optional Rotation for the Ellipse:
position = Vector3.Transform(position, Matrix.CreateFromAxisAngle(axis, angle));
//then add the first vector again so it's a complete loop (sounds familiar)
position = new Vector3(h + a * (float)Math.Cos((double)-max), k + b * (float)Math.Sin((double)-max), 0f);
vertices[vertexCount - 1] = new VertexPositionColor(position, Color.DarkOrange);
vertexBuffer = new VertexBuffer(device, vertexCount * VertexPositionColor.SizeInBytes,
BufferUsage.WriteOnly);
vertexBuffer.SetData<VertexPositionColor>(vertices);
}
这篇关于在 XNA 中绘制椭圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文