【问题标题】:Draw an Ellipse in XNA在 XNA 中画一个椭圆
【发布时间】:2014-02-02 13:44:28
【问题描述】:

我正在尝试编写代码以在 XNA 中绘制椭圆。假设我们有以下参数:

  1. 半长轴 (a)。
  2. 短半轴 (b)。
  3. 椭圆中心 (h,k)。
  4. 和 2D 旋转 (theta)。

如何使用上述参数绘制椭圆。

这里是函数

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;
    }
}

【问题讨论】:

  • 到目前为止你尝试过什么?请显示一些代码,以便我们更好地帮助您。

标签: c# xna ellipse


【解决方案1】:

我已经修改了取自 here 的代码以满足您的要求。

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);
}

【讨论】:

  • 非常感谢您的回答。但我在最后一行遇到语法错误。 vertexCount * VertexPositionColor.SizeInBytes。 “Microsoft.Xna.Framework.Graphics.VertexPositionColor”不包含“SizeInBytes”的定义。有什么想法吗?
  • 您之前是如何使用VertexPositionColor vertices 的?用你之前的方法画出来,TBH我不确定......
【解决方案2】:

您可以仅根据 x 或 y 找出椭圆的方程。然后根据方向递增/递减 x 或 y 以获取其他坐标。这是我为一个圆圈所做的

 this.x+=1.5*Gdx.graphics.getDeltaTime() * direction;

 if(direction==1)
 {

     if(this.x>=(centerx+radius))
    {


        direction=-1;
    }

 }

 else
 {

     if(this.x<=(centerx-radius))
        {


            direction=1;
        } 
 }

    this.y=(  (float) Math.sqrt(Math.abs(((radius*radius)- ( (this.x -centerx )*    (this.x -centerx )) )) )*direction  +  centery )  ;

PS:libgdx 的代码在 java 中。你可以相应地调整

【讨论】:

    猜你喜欢
    • 2020-04-14
    • 1970-01-01
    • 1970-01-01
    • 2016-09-15
    • 2017-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多