Koordinatensystem in Picturebox

  • VB.NET

Es gibt 5 Antworten in diesem Thema. Der letzte Beitrag () ist von RodFromGermany.

    Koordinatensystem in Picturebox

    Hi,

    Ich versuche, mit diesem Code, ein Koordinatensystem in eine Picturebox zu zeichnen:

    VB.NET-Quellcode

    1. Dim g As Graphics = PictureBox1.CreateGraphics
    2. Dim pointy, pointy2, pointx, pointx2 As Point
    3. pointy.X = CInt(PictureBox1.Width / 2)
    4. pointy.Y = 0
    5. pointy2.X = CInt(PictureBox1.Width / 2)
    6. pointy2.Y = PictureBox1.Height
    7. pointx.X = 0
    8. pointx.Y = CInt(PictureBox1.Height / 2)
    9. pointx2.X = PictureBox1.Width
    10. pointx2.Y = CInt(PictureBox1.Height / 2)
    11. g.DrawLine(Pens.Black, pointy, pointy2)
    12. g.DrawLine(Pens.Black, pointx, pointx2)


    Das Problem ist das nichts passiert.
    Könnt ihr mit bitte auf die Sprünge helfen?

    MfG
    optz
    Probier das ganze mal in der PaintMethode deiner PictureBox.
    Und überprüf mal deine Punkte. (Lass sie dir anzeigen z.B mit'm Debugger und schau ob das sein kann)

    VB.NET-Quellcode

    1. Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
    2. e.Graphics.DrawLine(Pens.Black, New Point(10, 10), New Point(20, 10))
    3. End Sub
    Probier doch mal das hier. (Form der PB muss ein Quadrat sein)

    VB.NET-Quellcode

    1. For i As Integer = 0 To PictureBox1.Width Step CInt(PictureBox1.Width / 10)
    2. e.Graphics.DrawLine(Pens.Black, New Point(i, 0), New Point(i, PictureBox1.Height))
    3. e.Graphics.DrawLine(Pens.Black, New Point(0, i), New Point(PictureBox1.Width, i))
    4. Next
    Ich würde as so anfangen:
    ymax = 10
    ymin = -10
    xmax = 10
    xmin = -10

    Das abgedeckte "Spektrum" ist in beide Richtungen 20. Da 10 die Hälfe von zwanzig ist muss der Mittelpunkt des Koordinatensystems in der Mitte der Picturebox liegen. Dann musst du noch die Skalierung auftragen und das wars. Ich hatte vor langer Zeit mal ein Projekt eben einen solchen Graphen zu zeichnen. Vielleicht kannst du was damit anfangen ;) :
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Option Strict On
    2. Imports System.Windows.Forms
    3. Imports System.Drawing
    4. Imports System.Collections.Generic
    5. Imports System.Text
    6. Public Class Graph
    7. Inherits System.Windows.Forms.UserControl
    8. Public Sub New()
    9. MyBase.New()
    10. MyBase.SetStyle(ControlStyles.UserPaint, True)
    11. MyBase.SetStyle(ControlStyles.DoubleBuffer, True) '
    12. MyBase.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
    13. End Sub
    14. Dim höhe_urpsrung As Integer
    15. Dim breite_ursprung As Integer
    16. Dim pic_height As Integer
    17. Dim pic_width As Integer
    18. Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
    19. pic_height = Me.Height
    20. pic_width = Me.Width
    21. ' Koordinaten des Ursprungs
    22. höhe_urpsrung = CInt((ymax / (Math.Abs(ymax) + Math.Abs(ymin))) * pic_height)
    23. breite_ursprung = CInt((xmax / (Math.Abs(xmax) + Math.Abs(xmin))) * pic_width)
    24. With e.Graphics
    25. 'Graphics konfigurieren
    26. .SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
    27. .TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
    28. .PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
    29. .InterpolationMode = Drawing2D.InterpolationMode.HighQualityBilinear
    30. .CompositingQuality = Drawing2D.CompositingQuality.HighQuality
    31. 'Achsen zeichnen
    32. .FillRectangle(New SolidBrush(Me.BackColor), 0, 0, Me.Width, Me.Height)
    33. .DrawLine(Pens.Black, pic_width - breite_ursprung, höhe_urpsrung, pic_width - breite_ursprung, 0)
    34. .DrawLine(Pens.Black, pic_width - breite_ursprung, höhe_urpsrung, pic_width - breite_ursprung, pic_height)
    35. .DrawLine(Pens.Black, pic_width - breite_ursprung, höhe_urpsrung, 0, höhe_urpsrung)
    36. .DrawLine(Pens.Black, pic_width - breite_ursprung, höhe_urpsrung, pic_width, höhe_urpsrung)
    37. 'Skalierung der Achsen
    38. Dim disx As Double = (pic_width * xscl) / (Math.Abs(xmin) + Math.Abs(xmax)) 'XAchse zeichnen rechter Teil
    39. For z = 0 To (xmax) / xscl
    40. Dim i As Integer = CInt(z * disx) + Math.Abs(pic_width - breite_ursprung)
    41. .DrawLine(Pens.Black, i, CInt(höhe_urpsrung + 5), i, CInt(höhe_urpsrung - 5))
    42. Next
    43. For z = Math.Abs(Math.Abs((xmin)) / xscl) To 0 Step -1
    44. Dim i As Integer = CInt(z * disx)
    45. .DrawLine(Pens.Black, i, CInt(höhe_urpsrung + 5), i, CInt(höhe_urpsrung - 5))
    46. Next
    47. Dim dego As Double = pic_height / (Math.Abs(ymax) + Math.Abs(ymin))
    48. For u = (ymax) / yscl To 0 Step -1
    49. Dim j As Integer = CInt(0 + dego * u * yscl)
    50. .DrawLine(Pens.Black, pic_width - breite_ursprung + 5, j, pic_width - breite_ursprung - 5, j)
    51. Next
    52. For u = 0 To (Math.Abs(ymin)) / yscl Step 1
    53. Dim trye As Integer = CInt((dego * u * yscl) + höhe_urpsrung)
    54. .DrawLine(Pens.Black, pic_width - breite_ursprung + 5, trye, pic_width - breite_ursprung - 5, trye)
    55. Next
    56. End With
    57. 'Prüfen ob DrawWithGrid auf on ist
    58. If DrawWithGrid = True Then
    59. For u = xmin To xmax
    60. For i = ymin To xmax
    61. If u <> 0 And i <> 0 Then
    62. AddPoint(u * xscl, i * yscl, Pens.Black, e)
    63. End If
    64. Next
    65. Next
    66. End If
    67. End Sub
    68. Private Sub AddPoint(ByVal x As Double, ByVal y As Double, ByVal pen As Pen, ByVal p As PaintEventArgs)
    69. Dim gesamthöhe As Double = ((Math.Abs(ymax) + Math.Abs(ymin)))
    70. Dim schritte As Double = Me.Height / gesamthöhe
    71. Dim gesammtlänge As Double = (Math.Abs(xmax) + Math.Abs(xmin))
    72. Dim schritte_2 As Double = Me.Width / gesammtlänge
    73. Dim x_koor As Single = CSng((schritte_2 * x) + (Math.Abs(breite_ursprung - pic_width)))
    74. Dim y_koor As Single = CSng(höhe_urpsrung - (schritte * y))
    75. p.Graphics.DrawRectangle(pen, x_koor, y_koor, 1.0F, 1.0F)
    76. End Sub
    77. #Region "Properties"
    78. Dim minerx As Integer = -10
    79. Public Property xmin As Integer
    80. Get
    81. Return minerx
    82. End Get
    83. Set(ByVal value As Integer)
    84. minerx = value
    85. Invalidate()
    86. End Set
    87. End Property
    88. Dim maxerx As Integer = 10
    89. Public Property xmax As Integer
    90. Get
    91. Return maxerx
    92. End Get
    93. Set(ByVal value As Integer)
    94. maxerx = value
    95. Invalidate()
    96. End Set
    97. End Property
    98. Dim sclerx As Integer = 1
    99. Public Property xscl As Integer
    100. Get
    101. Return sclerx
    102. End Get
    103. Set(ByVal value As Integer)
    104. sclerx = value
    105. Invalidate()
    106. End Set
    107. End Property
    108. Dim minery As Integer = -10
    109. Public Property ymin As Integer
    110. Get
    111. Return minery
    112. End Get
    113. Set(ByVal value As Integer)
    114. minery = value
    115. Invalidate()
    116. End Set
    117. End Property
    118. Dim maxery As Integer = 10
    119. Public Property ymax As Integer
    120. Get
    121. Return maxery
    122. End Get
    123. Set(ByVal value As Integer)
    124. maxery = value
    125. Invalidate()
    126. End Set
    127. End Property
    128. Dim sclery As Integer = 1
    129. Public Property yscl As Integer
    130. Get
    131. Return sclery
    132. End Get
    133. Set(ByVal value As Integer)
    134. sclery = value
    135. Invalidate()
    136. End Set
    137. End Property
    138. Public Property DrawWithGrid As Boolean = False
    139. #End Region
    140. End Class


    Mit diesem Control kannst du auch rechteckige Graphen basteln.

    8-) faxe1008 8-)

    optz schrieb:

    VB.NET-Quellcode

    1. pointy.X = CInt(PictureBox1.Width / 2)
    Verwende doch gleich Integerdivision:

    VB.NET-Quellcode

    1. pointy.X = PictureBox1.Width \ 2
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!