c# - 如何在 C# 中使用投影/相机技术

标签 c# system.drawing graph-drawing drawing2d

我画了下面的网格:

Grid (Top-View)

上面的网格是使用以下两种方法绘制的,一种是计算网格,另一种是计算每个单元格的中心:

//makes grid in picture box
private void drawGrid(int numOfCells, int cellSize, Graphics gr)
{
    Pen p = new Pen(Color.SteelBlue);

    for (int i = 0; i < Math.Sqrt(numOfCells) + 1; i++)
    {
        // Vertical
        gr.DrawLine(p, i * cellSize + 300, 200, i * cellSize + 300, 700);
        // Horizontal
        gr.DrawLine(p, 300, i * cellSize+200, 800, i * cellSize+200);
    }

    this.topology.SendToBack();
}


//draw the center point for each cell of the grid 
private void drawCenters(Graphics gr)
{
    for (int j = 0; j < rows; j++)
    {
        for (int i = 0; i < columns; i++)
        {
            gr.FillRectangle(Brushes.IndianRed, cellsCenters[0, i], cellsCenters[1, j], 3, 3);
        }
    }
}

我的问题是如何使这个网格如下图所示,以及如何将节点放置在这样的网格中的不同单元格(随机部署)。

Expected result

我需要在 3D View 中绘制网格,其中我有 z 以及 x 和 y!

最佳答案

注意:许多结构已经以一种或另一种形式存在,在这里我将向您展示如何从头开始。 enter image description here 相同的控件,相同的数据,不同的视角

由于您的数据是 3 维的,因此您需要存储 3 维的位置数据,这样您就不必在每次更改视角时都手动计算所有内容:

[TypeConverter(typeof(ExpandableObjectConverter))]
public struct Point3D
{
    public int X { get; set; }
    public int Y { get; set; }
    public int Z { get; set; }

    public Point3D(int x, int y, int z)
    {
        X = x;
        Y = y;
        Z = z;
    }
}

要将这些 3D 点投影到屏幕坐标 (2D),您需要某种投影/相机系统。通常您会使用矩阵/向量数学来执行此操作,但对于此示例,以下正交方法就足够了。

透视处理从 3D 到 2D 的转换。根据参数,您的图形将倾斜/旋转/平移等。

[TypeConverter(typeof(ExpandableObjectConverter))]
public class Perspective
{
    public float X_shift { get; set; } = 0.0f;
    public float Y_shift { get; set; } = 0.0f;

    public float X_x { get; set; } = 1.0f;
    public float X_y { get; set; } = 0.0f;
    public float X_z { get; set; } = 0.0f;

    public float Y_x { get; set; } = 0.0f;
    public float Y_y { get; set; } = 1.0f;
    public float Y_z { get; set; } = 0.0f;

    public PointF Project(Point3D p)
    {
        return new PointF(X_shift + X_x * p.X + X_y * p.Y + X_z * p.Z, Y_shift + Y_x * p.X + Y_y * p.Y + Y_z * p.Z);
    }
}

您现在需要做的就是照常绘制所有内容,但将 3D 坐标转换为 2D。以下控件绘制了一个网格(深度为 400)和两个传感器。

using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public class PerspectiveGrid : Control
    {
        private Perspective _perspective;

        public Perspective Perspective
        {
            get { return _perspective; }
            set
            {
                _perspective = value; 
                Invalidate();
            }
        }

        public PerspectiveGrid()
        {
            Perspective = new Perspective
            {
                X_shift = 100,
                Y_shift = 10,

                X_x = -0.2f,
                X_y = 1.0f,
                X_z = 0.0f,

                Y_x = 0.2f,
                Y_y = 0.0f,
                Y_z = 1.0f,
            };
        }

        /// <summary>
        /// Paints a Grid at Z = 400 and two Sensors
        /// </summary>
        protected override void OnPaint(PaintEventArgs e)
        {
            DrawGrid(10,40,400,e.Graphics);

            DrawSensor(new Point3D(80, 120, 400), new Point3D(80, 120, 200), e.Graphics);
            DrawSensor(new Point3D(240, 240, 400), new Point3D(240, 240, 120), e.Graphics);
        }

        /// <summary>
        /// Draws a sensor at the specified position(s)
        /// </summary>
        private void DrawSensor(Point3D from, Point3D to, Graphics gr)
        {
            DrawLine(gr, Pens.Black, from, to);
            DrawSphere(gr, Pens.Black, Brushes.Orange, to, 6);
        }

        /// <summary>
        /// Draws a sphere as a Circle at the specified position
        /// </summary>
        private void DrawSphere(Graphics gr, Pen outline, Brush fill, Point3D center, float radius)
        {
            PointF center2D = Project(center);
            gr.FillEllipse(fill, center2D.X - radius, center2D.Y - radius, radius * 2, radius * 2);
            gr.DrawEllipse(outline, center2D.X - radius, center2D.Y - radius, radius * 2, radius * 2);
        }

        /// <summary>
        /// Draws the grid at the specified depth
        /// </summary>
        private void DrawGrid(int numOfCells, int cellSize, int depth, Graphics gr)
        {
            Pen p = Pens.SteelBlue;

            for (int i = 0; i <= numOfCells; i++)
            {
                // Vertical
                DrawLine(gr, p, new Point3D(i * cellSize, 0 , depth), new Point3D(i * cellSize, numOfCells * cellSize, depth));
                // Horizontal
                DrawLine(gr, p, new Point3D(0, i * cellSize, depth), new Point3D(numOfCells * cellSize, i * cellSize, depth));
            }
        }

        /// <summary>
        /// Draws a line from one 3DPoint to another
        /// </summary>
        private void DrawLine(Graphics graphics, Pen pen, Point3D p1, Point3D p2)
        {
            PointF pointFrom = Project(p1);
            PointF pointTo = Project(p2);

            graphics.DrawLine(pen, pointFrom, pointTo);
        }

        /// <summary>
        /// Projects a Point3D to a PointF
        /// </summary>
        private PointF Project(Point3D p)
        {
            return Perspective.Project(p);
        }
    }
}

一些链接可能会帮助您构建这些概念:

Orthographic projection
Quaternion
Math Library with Matrix and Vector support

关于c# - 如何在 C# 中使用投影/相机技术,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45159118/

相关文章:

c# - 一个聪明的技巧来防止 Task<IDisposable> 在 C# using 子句中被使用而无需 await

c# - 如何从 C# 中的图形对象获取位图/图像?

javascript - 图德古拉 : How to detect onclick and onmouseover events?

r - 力有向图绘制 : Edit the force between specific nodes (R)

c# - 您可以将 LINQ 与内存对象一起使用而不是 SQL Server 查询来提高性能吗?

c# - C# 8.0 是否支持 IAsyncEnumerable?

c# - 当我的 IValueConverter 抛出异常时,如何运行 IDataErrorInfo 验证代码?

c# - 获取 RotateTransform 的结果大小

c# - 选择 System.Drawing.Icon 的大小?