简体   繁体   English

c# 绘制z轴

[英]c# draw the z axis

I'm drawing coordinate axes in picturebox我在图片框中绘制坐标轴

    void draw_cor()
    {
        int w = pictureBox1.ClientSize.Width / 2;
        int h = pictureBox1.ClientSize.Height / 2;
        Refresh();
        Graphics e = pictureBox1.CreateGraphics();
        e.TranslateTransform(w, h);
        DrawXAxis(new Point(-w, 0), new Point(w, 0), e);
        DrawYAxis(new Point(0, h), new Point(0, -h), e);
        DrawZAxis(new Point(-pictureBox1.ClientSize.Width , pictureBox1.ClientSize.Height), new Point(pictureBox1.ClientSize.Width, -pictureBox1.ClientSize.Height ), e);

    }

markup and text for the x axis as an example以 x 轴的标记和文本为例

    private void DrawXAxis(Point start, Point end, Graphics g)
    {
        for (int i = Step; i < end.X; i += Step)
        {
            g.DrawLine(Pens.Black, i, -5, i, 5);
            DrawText(new Point(i, 5), (i / Step).ToString(), g, false);
        }

        for (int i = -Step; i > start.X; i -= Step)
        {
            g.DrawLine(Pens.Black, i, -5, i, 5);
            DrawText(new Point(i, 5), (i / Step).ToString(), g, false);
        }
        g.DrawLine(Pens.Black, start, end);
        g.DrawString("X", new Font(Font.FontFamily, 10, FontStyle.Bold), Brushes.Black, new Point(end.X - 15, end.Y));

    }

    private void DrawText(Point point, string text, Graphics g, bool isYAxis)
    {
        var f = new Font(Font.FontFamily, 6);
        var size = g.MeasureString(text, f);
        var pt = isYAxis
            ? new PointF(point.X + 1, point.Y - size.Height / 2)
            : new PointF(point.X - size.Width / 2, point.Y + 1);
        var rect = new RectangleF(pt, size);
        g.DrawString(text, f, Brushes.Black, rect);
    }

can someone explain how to make a method for marking the z axis?有人可以解释如何制作一种标记 z 轴的方法吗? I understand that the shift should be diagonal in both x and y, but nothing worked out for me and no markup appears on the screen.(so far I have managed to draw only a straight line diagonally )我知道 x 和 y 的偏移都应该是对角线的,但是对我来说没有任何结果,屏幕上也没有出现任何标记。(到目前为止,我只设法在对角线上画了一条直线)

upd:更新:

    private void DrawZAxis(Point start, Point end, Graphics g)
    {
        

        for (int i = -Step, j=Step ; i > start.X; i -= Step,j += Step)
        {
            g.DrawLine(Pens.Black, new Point(i-5, j), new Point(i+5, j));
           
            DrawText(new Point(i, j), (i / -Step).ToString(), g, false);
        }
...
}

I seem to have succeeded, but I ran into such a problem:我似乎成功了,但我遇到了这样一个问题:

在此处输入图像描述

在此处输入图像描述

that is, the markup is not always on the coordinate axis.也就是说,标记并不总是在坐标轴上。 How to avoid this?如何避免这种情况? It is necessary that the numbers are always on the axis (I suppose I should calculate the coefficient when the window is scaled, but only where to add it or by what to multiply?)数字必须始终在轴上(我想我应该在缩放 window 时计算系数,但只在哪里添加它或乘以什么?)

You are dealing with 3D data, so you should use 3D tools to transform your axes, and your data for that matter.您正在处理 3D 数据,因此您应该使用 3D 工具来转换您的轴,以及您的数据。

So you need to define a projection from 3D space to 2D space.所以你需要定义一个从 3D 空间到二维空间的投影。 This is usually done by defining a projection matrix.这通常通过定义投影矩阵来完成。 There are multiple projections to chose from, it looks like your projection is Oblique, but orthographic and perspective projections are also common.有多种 投影可供选择,看起来您的投影是倾斜的,但正交投影和透视投影也很常见。 The System.Numerics.Vectors library has classes for Matrix4x4 , vector2/3/4, with methods to create your projection and transform your vectors. System.Numerics.Vectors 库包含Matrix4x4和 vector2/3/4 的类,以及创建投影和变换向量的方法。

After transforming a vector you can simply keep the x/y values and discard the z-value to get your image coordinates.转换矢量后,您可以简单地保留 x/y 值并丢弃 z 值以获取图像坐标。 Note that if using a perspective transform you need a vector4 and divide the x/y/z elements by W.请注意,如果使用透视变换,您需要一个向量 4 并将 x/y/z 元素除以 W。

Armed with these tools it should be a fairly simple thing to generate start/end points for each axis, and create tick-marks in 3D, before projecting everything to 2D for drawing.有了这些工具,为每个轴生成起点/终点,并在 3D 中创建刻度线,然后将所有内容投影到 2D 以进行绘图,这应该是一件相当简单的事情。

Another option would be to just do everything in Wpf3D to start with, this will likely make some functionality like rotating the camera simpler.另一种选择是在Wpf3D中开始做所有事情,这可能会使旋转相机等一些功能更简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM