如何绘制 (x,y,z) [英] How to plot (x,y,z)

查看:49
本文介绍了如何绘制 (x,y,z)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论如何要以 vb 形式绘制 x(来自 x 文本框)、y(来自 y 文本框)和 z(来自 z 文本框)?它是 Windows 应用程序.我有三个文本框,表示 x、y、z 坐标.我想知道是否有任何工具或方法可以向用户显示这一点.

Is there anyway to plot x(from x textbox), y(from y textbox), and z(from z textbox) in vb form? It is windows application. I have three text boxes that indicates x,y,z coordinate. I was wondering if there is any tool or method to show this point to user.

推荐答案

这里有两个主要途径.1) 将 (x,y,z) 坐标转换为平面投影 (x,y) 并使用 在屏幕上绘制,或 2) 使用 直接在 GLControl 中绘制一个点,但你必须先设置视口和投影.

There are two main avenues here. 1) Transform the (x,y,z) coordinate into a planar project (x,y) and use gdi+ to draw on the screen, or 2) Use opentk to draw a point directly into an GLControl, but you have to setup the viewport and projection first.

它不是很漂亮,但这里是使用 OpenTK 的 VS2010 概念证明.

It is not very pretty, but here is a proof of concept with VS2010 using OpenTK.

public partial class Form1 : Form
{
    bool loaded=false;

    public Form1()
    {
        InitializeComponent();
    }

    public Vector3 PointCoordinates
    {
        get
        {
            float x=0, y=0, z=0;
            float.TryParse(xTextBox.Text, out x);
            float.TryParse(yTextBox.Text, out y);
            float.TryParse(zTextBox.Text, out z);

            return new Vector3(x, y, z);
        }
        set
        {
            xTextBox.Text=value.X.ToString();
            yTextBox.Text=value.Y.ToString();
            zTextBox.Text=value.Z.ToString();
        }
    }
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        PointCoordinates=new Vector3(0, 0, 0);
        loaded=true;
        SetupViewPort();
    }

    private void glControl1_Resize(object sender, EventArgs e)
    {
        if(!loaded) return;
        SetupViewPort();
    }

    private void glControl1_Paint(object sender, PaintEventArgs e)
    {
        glControl1.MakeCurrent();
        GL.ClearColor(glControl1.BackColor);
        GL.Clear(ClearBufferMask.ColorBufferBit|ClearBufferMask.DepthBufferBit);

        GL.MatrixMode(MatrixMode.Modelview);
        GL.LoadIdentity();

        SetupCamera();

        // Draw Coordinate System
        GL.LineWidth(1.5f);
        GL.Begin(PrimitiveType.Lines);
        GL.Color3(Color.Red);
        GL.Vertex3(0, 0, 0);
        GL.Vertex3(1, 0, 0);
        GL.Vertex3(0.85, 0.05, -0.05);
        GL.Vertex3(1, 0, 0);
        GL.Vertex3(0.85, -0.05, 0.05);
        GL.Vertex3(1, 0, 0);
        GL.Color3(Color.Green);
        GL.Vertex3(0, 0, 0);
        GL.Vertex3(0, 1, 0);
        GL.Vertex3(-0.05, 0.85, 0.05);
        GL.Vertex3(0, 1, 0);
        GL.Vertex3(0.05, 0.85, -0.05);
        GL.Vertex3(0, 1, 0);
        GL.Color3(Color.Blue);
        GL.Vertex3(0, 0, 0);
        GL.Vertex3(0, 0, 1);
        GL.Vertex3(-0.05, 0.05, 0.85);
        GL.Vertex3(0, 0, 1);
        GL.Vertex3(0.05, -0.05, 0.85);
        GL.Vertex3(0, 0, 1);
        GL.End();

        // Draw a single point
        var vector=PointCoordinates;
        GL.PointSize(5f);
        GL.Begin(PrimitiveType.Points);
        GL.Color3(Color.Black);
        GL.Vertex3(vector);
        GL.End();
        GL.PointSize(3f);
        GL.Begin(PrimitiveType.Points);
        GL.Color3(Color.Yellow);
        GL.Vertex3(vector);
        GL.End();

        glControl1.SwapBuffers();
    }

    void SetupViewPort()
    {
        float wt=Math.Max(1, glControl1.Width);
        float ht=Math.Max(1, glControl1.Height);
        float sz=(float)Math.Sqrt(ht*wt);
        GL.Viewport((int)(wt-sz)/2, (int)(ht-sz)/2, (int)sz, (int)sz);
        var ortho=Matrix4.CreateOrthographic(
            10f, 10f, 1f, 200f);
        GL.MatrixMode(MatrixMode.Projection);
        GL.LoadMatrix(ref ortho);
    }

    void SetupCamera()
    {
        Matrix4 lookAt=Matrix4.LookAt(
                        10f, 5f, 15f,
                        0f, 0f, 0f,
                        0f, 1f, 0f);
        GL.MatrixMode(MatrixMode.Modelview);
        GL.LoadMatrix(ref lookAt);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        glControl1.Refresh();
    }

}

这篇关于如何绘制 (x,y,z)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆