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

use*_*322 1 xna graph

在我最近的问题的答案中,有一些代码绘制图形,但我无法将其编辑为接受任何点列表作为参数的东西.

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

  • 列表Vector2,Point或者VertexPositionColor,我可以与任何一个一起工作.
  • 整个图表的偏移量

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

  • 颜色可能会覆盖VertexPositionColor颜色并适用于所有点.
  • 图形的大小,因此可以缩小或扩展,可以Vector2是乘数,也可以Point是目标大小.甚至可能将此与偏移相结合Rectangle.

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


这是代码(由NikoDraš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();
}
Run Code Online (Sandbox Code Playgroud)

和绘图方法:

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

nee*_*eKo 6

Graph可以在此处找到执行请求的类.
大约200行代码似乎太贴了.

在此输入图像描述

Graph通过使浮的列表(任选地与颜色)到它的绘制Draw(..)方法.

Graph 属性是:

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

使用线列表/三角形条绘制图形.