如何从点列表中绘制图表?

时间:2012-12-21 04:14:01

标签: graph xna

在我最近的问题的this answer中,有一些代码可以绘制图表,但我无法将其编辑为接受任何点列表作为参数的内容。

我希望Drawing方法接受这些参数:

  • Vector2PointVertexPositionColor的列表,我可以与之合作。
  • 整个图表的偏移量

这些可选要求将不胜感激:

  • 可能会覆盖VertexPositionColor颜色并适用于所有点的颜色。
  • 图表的大小,因此可以缩小或展开,可以是Vector2作为乘数,也可以是Point作为目标大小。甚至可以将其与Rectangle中的偏移量结合起来。

如果有可能,我希望将它全部放在一个类中,因此图形可以彼此分开使用,每个图形都有自己的Effect.world矩阵等。


以下是代码(Niko Drašković):

Matrix worldMatrix;
Matrix viewMatrix;
Matrix projectionMatrix;
BasicEffect basicEffect;

VertexPositionColor[] pointList;
short[] lineListIndices;

protected override void Initialize()
{
    int n = 300;
    //GeneratePoints generates a random graph, implementation irrelevant
    pointList = new VertexPositionColor[n];
    for (int i = 0; i < n; i++)
        pointList[i] = new VertexPositionColor() { Position = new Vector3(i, (float)(Math.Sin((i / 15.0)) * height / 2.0 + height / 2.0 + minY), 0), Color = Color.Blue };

    //links the points into a list
    lineListIndices = new short[(n * 2) - 2];
    for (int i = 0; i < n - 1; i++)
    {
        lineListIndices[i * 2] = (short)(i);
        lineListIndices[(i * 2) + 1] = (short)(i + 1);
    }

    worldMatrix = Matrix.Identity;
    viewMatrix = Matrix.CreateLookAt(new Vector3(0.0f, 0.0f, 1.0f), Vector3.Zero, Vector3.Up);
    projectionMatrix = Matrix.CreateOrthographicOffCenter(0, (float)GraphicsDevice.Viewport.Width, (float)GraphicsDevice.Viewport.Height, 0, 1.0f, 1000.0f);

    basicEffect = new BasicEffect(graphics.GraphicsDevice);
    basicEffect.World = worldMatrix;
    basicEffect.View = viewMatrix;
    basicEffect.Projection = projectionMatrix;

    basicEffect.VertexColorEnabled = true; //important for color

    base.Initialize();
}

绘图方法:

foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
{
    pass.Apply();
    GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>(
        PrimitiveType.LineList,
        pointList,
        0,
        pointList.Length,
        lineListIndices,
        0,
        pointList.Length - 1
    );
}

1 个答案:

答案 0 :(得分:6)

可以找到执行请求的Graphhere
这里有大约200行代码似乎太多了。

enter image description here

通过将浮动列表(可选择带颜色)传递给其Graph方法来绘制Draw(..)

Graph属性是:

  • Vector2 Position - 图表的底部左下角
  • Point Size - 图表的宽度(.X)和高度(.Y)。水平地,值将被分配以完全适合宽度。在垂直方向上,所有值都将使用Size.Y / MaxValue进行缩放。
  • float MaxValue - 将位于图表顶部的值。所有关闭图表值(大于MaxValue)都将设置为此值。
  • GraphType Type - 使用可能的值GraphType.LineGraphType.Fill,确定图表是仅绘制线条还是底部填充。

使用line list / triangle strip绘制图表。

相关问题