VB.NET笛卡尔坐标系

时间:2012-05-07 15:20:13

标签: vb.net forms system coordinate cartesian

我想在Windows窗体中制作笛卡尔坐标系,并能够在其中绘制(x,y)坐标。

我该怎么做?我已经完成了我的研究但不幸的是我只登陆“图表”而不是笛卡尔飞机。

关于我的问题的任何链接都会有所帮助......谢谢......

3 个答案:

答案 0 :(得分:2)

您应该创建一个自定义UserControl并使用Paint甚至在控件的表面上绘制。 Paint事件为您提供了一个Graphics对象,您可以使用它来绘制图形。但是,要知道的是,您需要更换Y轴。在Windows中,屏幕的左上角是0,0而不是左下角。

因此,例如,以下代码将在控件上绘制图形的x和y轴:

Public Class CartesianGraph
    Public Property BottomLeftExtent() As Point
        Get
            Return _bottomLeftExtent
        End Get
        Set(ByVal value As Point)
            _bottomLeftExtent = value
        End Set
    End Property
    Private _bottomLeftExtent As Point = New Point(-100, -100)


    Public Property TopRightExtent() As Point
        Get
            Return _topRightExtent
        End Get
        Set(ByVal value As Point)
            _topRightExtent = value
        End Set
    End Property
    Private _topRightExtent As Point = New Point(100, 100)


    Private Sub CartesianGraph_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim extentHeight As Integer = _topRightExtent.Y - _bottomLeftExtent.Y
        Dim extentWidth As Integer = _topRightExtent.X - _bottomLeftExtent.X
        If (extentHeight <> 0) And (extentWidth <> 0) Then
            If (_bottomLeftExtent.Y <= 0) And (_topRightExtent.Y >= 0) Then
                Dim xAxis As Integer = e.ClipRectangle.Height - (_bottomLeftExtent.Y * -1 * e.ClipRectangle.Height \ extentHeight)
                e.Graphics.DrawLine(New Pen(ForeColor), 0, xAxis, e.ClipRectangle.Width, xAxis)
            End If
            If (_bottomLeftExtent.X <= 0) And (_topRightExtent.X >= 0) Then
                Dim yAxis As Integer = e.ClipRectangle.Width * _bottomLeftExtent.X * -1 \ extentWidth
                e.Graphics.DrawLine(New Pen(ForeColor), yAxis, 0, yAxis, e.ClipRectangle.Height)
            End If
        End If
    End Sub
End Class

答案 1 :(得分:2)

在WinForms中,您可以使用 PictureBox 控件,然后使用DrawLine,DrawEllipse等基元绘制它。以下SO问题包含一个示例:

在WPF中,您可以类似地使用画布控件:

如果您想要自动轴和标签,图表确实是可行的方法。对于您的用例,点图似乎是正确的解决方案:

答案 2 :(得分:0)

.NET有一个图表库,但有一些开源项目可以很好地完成这类工作。如果你想绘制坐标Zedgraph使这个相对容易,而且非常灵活。

ZedGraph Example

Dynamic Data Display也值得一看,但它是WPF,而不是Windows Forms