2012-05-07 127 views
0

我想在Windows窗體中創建一個笛卡兒座標系,並能夠繪製座標(x,y)。VB.NET笛卡爾座標系

我怎麼做到這一點?我已經完成了我的研究,但不幸的是,我只落在「圖表」而不是笛卡爾飛機上。

關於我的問題,將有助於...感謝任何鏈接...

+0

單詞 「笛卡爾」 不會幫助你在你的搜索。只是使用術語「x-y圖」。笛卡兒是隱含的,很少被提及。如果你谷歌它有很多的例子。 – 2012-05-07 15:28:53

+0

@jaime在我看來,你的編輯把它變成了一個關於簡單的x-y散點圖的問題。我認爲OP希望能夠創建自定義2D圖紙 – MarkJ

+0

@您需要創建自定義2D圖紙還是標準x-y散點圖? – MarkJ

回答

2

你應該創建一個自定義用戶控件,並使用畫圖甚至繪製控件的表面上。 Paint事件爲您提供了一個可用於繪製圖形的Graphics對象。然而,要知道的重要一點是,您需要交換Y軸。在窗口中,屏幕的左上角是0,0而不是左下角。

所以,舉例來說,下面的代碼將利用一個contorl的曲線圖的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 
+0

謝謝你給我提供這個樣本,但我對這個Paint事件真的很陌生。不知道一條簡單的線路穿過彼此會採取這麼多的代碼。我將研究這個Paint事件和Graphics對象,並將其用作參考......再次感謝。 –

+0

畫線很簡單。這是計算線路所需的所有思想和努力。正如你在我的例子中看到的那樣,我提供了屬性來設置圖的外部範圍,所以我的代碼必須計算圖的比例。如果你在屏幕上每一個點上只有一個簡單的像素,代碼就會簡單得多。 –

+0

自定義用戶控件看起來過多。爲什麼不像Heinzi在他的回答中所說的那樣畫一個PictureBox? – MarkJ

相關問題