[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.
Re: Use Lines to draw Geometric Figures(Polygons)?
using that link for the example, try this:
vb Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
PictureBox1.Invalidate()
End Sub
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
Dim pn As New Pen(Color.Blue)
Dim pt1 As New Point(30, 30)
Dim pt2 As New Point(110, 100)
Dim pts As Point() = {pt1, pt2}
'Stop
Project(pts, PictureBox1.Width, PictureBox1.Height)
e.Graphics.DrawLines(pn, pts)
' Create a pen with red color and width 3
Dim redPen As New Pen(Color.Red, 3)
' Create an array of points
Dim ptsArray() As Point = {New Point(10, 20), New Point(50, 40), New Point(220, 120), New Point(120, 20)}
Project(ptsArray, PictureBox1.Width, PictureBox1.Height)
e.Graphics.DrawLines(redPen, ptsArray)
End Sub
Public Sub Project(ByRef p() As Point, ByVal viewWidth As Integer, ByVal viewHeight As Integer)
Dim MinX As Integer = p.Min(Function(pt) pt.X)
Dim MaxX As Integer = p.Max(Function(pt) pt.X)
Dim MinY As Integer = p.Min(Function(pt) pt.Y)
Dim MaxY As Integer = p.Max(Function(pt) pt.Y)
Dim leftOffset As Integer = (viewWidth - (MaxX - MinX)) \ 2
Dim topOffset As Integer = (viewHeight - (MaxY - MinY)) \ 2
For x As Integer = 0 To p.GetUpperBound(0)
p(x) = New Point(p(x).X + leftOffset, p(x).Y + topOffset)
Next
End Sub
Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
PictureBox1.Invalidate()
End Sub
End Class
Re: Use Lines to draw Geometric Figures(Polygons)?
i just noticed. these 2 lines:
vb Code:
Dim leftOffset As Integer = (viewWidth - (MaxX - MinX)) \ 2
Dim topOffset As Integer = (viewHeight - (MaxY - MinY)) \ 2
should be:
vb Code:
Dim leftOffset As Integer = ((viewWidth - (MaxX - MinX)) \ 2) - MinX
Dim topOffset As Integer = ((viewHeight - (MaxY - MinY)) \ 2) - MinY
Re: Use Lines to draw Geometric Figures(Polygons)?
Quote:
Originally Posted by
.paul.
using that link for the example, try this:
vb Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
PictureBox1.Invalidate()
End Sub
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
Dim pn As New Pen(Color.Blue)
Dim pt1 As New Point(30, 30)
Dim pt2 As New Point(110, 100)
Dim pts As Point() = {pt1, pt2}
'Stop
Project(pts, PictureBox1.Width, PictureBox1.Height)
e.Graphics.DrawLines(pn, pts)
' Create a pen with red color and width 3
Dim redPen As New Pen(Color.Red, 3)
' Create an array of points
Dim ptsArray() As Point = {New Point(10, 20), New Point(50, 40), New Point(220, 120), New Point(120, 20)}
Project(ptsArray, PictureBox1.Width, PictureBox1.Height)
e.Graphics.DrawLines(redPen, ptsArray)
End Sub
Public Sub Project(ByRef p() As Point, ByVal viewWidth As Integer, ByVal viewHeight As Integer)
Dim MinX As Integer = p.Min(Function(pt) pt.X)
Dim MaxX As Integer = p.Max(Function(pt) pt.X)
Dim MinY As Integer = p.Min(Function(pt) pt.Y)
Dim MaxY As Integer = p.Max(Function(pt) pt.Y)
Dim leftOffset As Integer = (viewWidth - (MaxX - MinX)) \ 2
Dim topOffset As Integer = (viewHeight - (MaxY - MinY)) \ 2
For x As Integer = 0 To p.GetUpperBound(0)
p(x) = New Point(p(x).X + leftOffset, p(x).Y + topOffset)
Next
End Sub
Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
PictureBox1.Invalidate()
End Sub
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!