Results 1 to 4 of 4

Thread: [RESOLVED] Use Lines to draw Geometric Figures(Polygons)?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2010
    Posts
    131

    Resolved [RESOLVED] Use Lines to draw Geometric Figures(Polygons)?

    Hi guys. I'm trying to do something that is turning out to be out of my league at the moment. I need to draw geometric figures. These geometric figures have all the coordinates of their corners. Say I have a trapezoid, and I would want to draw it. The drawing would be done in a picturebox or a panel or a groupbox (some part of the form where only the geometric figure will be).

    I saw the method on how to do that Here and that's exactly what I'm trying to achieve, but there it's drawn on a newly created form, and I need it in a picbox, panel or groupbox as I mentioned above. I also need the drawn figure to be at the center of the picbox/panel/groupbox. If anyone's willing to help, I'd really appreciate it.

    The coordinates of the lines should be like (xa.text,ya.text) to (xa.text - xb.text, ya.text + yb.text) if I'm not wrong for the first line of the geometric figure and so on.
    Last edited by Legjendat; May 9th, 2012 at 05:24 PM.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: Use Lines to draw Geometric Figures(Polygons)?

    using that link for the example, try this:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         PictureBox1.Invalidate()
    5.     End Sub
    6.  
    7.     Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
    8.         Dim pn As New Pen(Color.Blue)
    9.  
    10.         Dim pt1 As New Point(30, 30)
    11.         Dim pt2 As New Point(110, 100)
    12.         Dim pts As Point() = {pt1, pt2}
    13.         'Stop
    14.         Project(pts, PictureBox1.Width, PictureBox1.Height)
    15.         e.Graphics.DrawLines(pn, pts)
    16.  
    17.         ' Create a pen with red color and width 3
    18.         Dim redPen As New Pen(Color.Red, 3)
    19.         ' Create an array of points
    20.         Dim ptsArray() As Point = {New Point(10, 20), New Point(50, 40), New Point(220, 120), New Point(120, 20)}
    21.  
    22.         Project(ptsArray, PictureBox1.Width, PictureBox1.Height)
    23.         e.Graphics.DrawLines(redPen, ptsArray)
    24.  
    25.     End Sub
    26.  
    27.     Public Sub Project(ByRef p() As Point, ByVal viewWidth As Integer, ByVal viewHeight As Integer)
    28.  
    29.         Dim MinX As Integer = p.Min(Function(pt) pt.X)
    30.         Dim MaxX As Integer = p.Max(Function(pt) pt.X)
    31.  
    32.         Dim MinY As Integer = p.Min(Function(pt) pt.Y)
    33.         Dim MaxY As Integer = p.Max(Function(pt) pt.Y)
    34.  
    35.         Dim leftOffset As Integer = (viewWidth - (MaxX - MinX)) \ 2
    36.         Dim topOffset As Integer = (viewHeight - (MaxY - MinY)) \ 2
    37.  
    38.         For x As Integer = 0 To p.GetUpperBound(0)
    39.             p(x) = New Point(p(x).X + leftOffset, p(x).Y + topOffset)
    40.         Next
    41.  
    42.     End Sub
    43.  
    44.     Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
    45.         PictureBox1.Invalidate()
    46.     End Sub
    47.  
    48. End Class

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: Use Lines to draw Geometric Figures(Polygons)?

    i just noticed. these 2 lines:

    vb Code:
    1. Dim leftOffset As Integer = (viewWidth - (MaxX - MinX)) \ 2
    2. Dim topOffset As Integer = (viewHeight - (MaxY - MinY)) \ 2

    should be:

    vb Code:
    1. Dim leftOffset As Integer = ((viewWidth - (MaxX - MinX)) \ 2) - MinX
    2. Dim topOffset As Integer = ((viewHeight - (MaxY - MinY)) \ 2) - MinY

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Apr 2010
    Posts
    131

    Re: Use Lines to draw Geometric Figures(Polygons)?

    Quote Originally Posted by .paul. View Post
    using that link for the example, try this:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         PictureBox1.Invalidate()
    5.     End Sub
    6.  
    7.     Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
    8.         Dim pn As New Pen(Color.Blue)
    9.  
    10.         Dim pt1 As New Point(30, 30)
    11.         Dim pt2 As New Point(110, 100)
    12.         Dim pts As Point() = {pt1, pt2}
    13.         'Stop
    14.         Project(pts, PictureBox1.Width, PictureBox1.Height)
    15.         e.Graphics.DrawLines(pn, pts)
    16.  
    17.         ' Create a pen with red color and width 3
    18.         Dim redPen As New Pen(Color.Red, 3)
    19.         ' Create an array of points
    20.         Dim ptsArray() As Point = {New Point(10, 20), New Point(50, 40), New Point(220, 120), New Point(120, 20)}
    21.  
    22.         Project(ptsArray, PictureBox1.Width, PictureBox1.Height)
    23.         e.Graphics.DrawLines(redPen, ptsArray)
    24.  
    25.     End Sub
    26.  
    27.     Public Sub Project(ByRef p() As Point, ByVal viewWidth As Integer, ByVal viewHeight As Integer)
    28.  
    29.         Dim MinX As Integer = p.Min(Function(pt) pt.X)
    30.         Dim MaxX As Integer = p.Max(Function(pt) pt.X)
    31.  
    32.         Dim MinY As Integer = p.Min(Function(pt) pt.Y)
    33.         Dim MaxY As Integer = p.Max(Function(pt) pt.Y)
    34.  
    35.         Dim leftOffset As Integer = (viewWidth - (MaxX - MinX)) \ 2
    36.         Dim topOffset As Integer = (viewHeight - (MaxY - MinY)) \ 2
    37.  
    38.         For x As Integer = 0 To p.GetUpperBound(0)
    39.             p(x) = New Point(p(x).X + leftOffset, p(x).Y + topOffset)
    40.         Next
    41.  
    42.     End Sub
    43.  
    44.     Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
    45.         PictureBox1.Invalidate()
    46.     End Sub
    47.  
    48. End Class
    woooooooooow, awesome, awesome & Awesome! Works Perfect, just what I needed (with the correction on the second post, ofc). Thank you sooooooooooooooooo muchhhhhhhhhh! Can finally present the project to my teacher, the cherry above the cake is finally put!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width