Imports System.Drawing.Drawing2D
Public Class Form4
Private NewSegment As Segment1
Private Segments As New List(Of Segment1)
Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
NewSegment = New Segment1(e.Location, e.Location)
Segments.Add(NewSegment)
PictureBox1.Invalidate()
End Sub
Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
If NewSegment Is Nothing Then Return
NewSegment.Point2 = e.Location
PictureBox1.Invalidate()
End Sub
Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
For Each segment As Segment1 In Segments
segment.Draw(e.Graphics)
Next
End Sub
Private Sub PictureBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseUp
NewSegment = Nothing
End Sub
Private Sub PictureBox1_SizeChanged(sender As Object, e As EventArgs) Handles PictureBox1.SizeChanged
PictureBox1.Invalidate()
End Sub
End Class
Class Segment1
Private pt1, pt2 As Point
Private angle As Double = 0
Private length As Double = 0
Private Shared SF As StringFormat
Private Const RADIUS As Integer = 20
Private Shared drawString As String = "90"
Private Shared pen1 As New Pen(Color.Black, 1.5)
Private Shared drawFontF As New Font("times new roman", 10)
Private Shared rc As New Rectangle(New Point(-RADIUS, -RADIUS), New Size(RADIUS * 2, RADIUS * 2))
Public Sub New(startPoint As Point, endPoint As Point)
Point1 = startPoint
Point2 = endPoint
End Sub
Public Property Point1 As Point
Get
Return pt1
End Get
Set(value As Point)
pt1 = value
UpdateAngleAndLength()
End Set
End Property
Public Property Point2 As Point
Get
Return pt2
End Get
Set(value As Point)
pt2 = value
UpdateAngleAndLength()
End Set
End Property
Private Sub UpdateAngleAndLength()
angle = Math.Atan2(Point2.Y - Point1.Y, Point2.X - Point1.X) * 180.0 / Math.PI
length = Math.Sqrt(Math.Pow(Point2.X - Point1.X, 2) + Math.Pow(Point2.Y - Point1.Y, 2))
End Sub
Public Sub Draw(gr As Graphics)
' save the current state of the graphics
Dim curState As GraphicsState = gr.Save()
' move the origin to the start point of the line
gr.TranslateTransform(Point1.X, Point1.Y)
' rotate the whole surface
gr.RotateTransform(angle)
' draw the line on the x-axis
gr.DrawLine(pen1, 0, 0, CInt(length), 0)
' move the origin along the x-axis to where the center of the circle should be
gr.TranslateTransform(length + RADIUS, 0)
' draw the hexagon
'******************************
Dim side As Integer = 25 '' the length of the side of a hex
Dim ShortSide As Single = Convert.ToSingle(System.Math.Sin(30 * System.Math.PI / 180) * side)
Dim LongSide As Single = Convert.ToSingle(System.Math.Cos(30 * System.Math.PI / 180) * side)
Dim Points(6) As Point
Points(0) = New Point(Point2.X, Point2.Y)
Points(1) = New Point(Point2.X + side, Point2.Y)
Points(2) = New Point(Point2.X + side + ShortSide, Point2.Y + LongSide)
Points(3) = New Point(Point2.X + side, Point2.Y + LongSide + LongSide)
Points(4) = New Point(Point2.X, Point2.Y + LongSide + LongSide)
Points(5) = New Point(Point2.X - ShortSide, Point2.Y + LongSide)
Points(6) = New Point(Point2.X, Point2.Y)
'**************************************************
gr.DrawPolygon(pen1, Points)
' put the graphics back to the way it was originally
gr.Restore(curState)
End Sub
end class