HOW DRAW hexagon END OF LINE WITH MOUSE MOVE EVENT

user20 161 Reputation points
2022-05-30T06:56:50.387+00:00

I want to draw a line that has a hexagon at the end. with mouse move event. I can draw a circle or a square, but not a hexagon! I put my code, please help me. code is:

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 circle  
        gr.DrawEllipse(pen1, rc)  
                
        ' put the graphics back to the way it was originally  
        gr.Restore(curState)  
    End Sub  
end class  

206633-1233.gif

Developer technologies VB
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 34,221 Reputation points Microsoft External Staff
    2022-06-01T09:40:07.433+00:00

    Hi @user20 ,
    You have modified the origin of the coordinates, so the hexagon coordinates are offset in the graph.
    I modified lines 93 and 102 to 108 in the code you provided.
    You can refer to the code below.

            gr.TranslateTransform(length, 0)  
      
            Points(0) = New Point(0, 0)  
            Points(1) = New Point(0 + side, 0)  
            Points(2) = New Point(0 + side + ShortSide, 0 + LongSide)  
            Points(3) = New Point(0 + side, 0 + LongSide + LongSide)  
            Points(4) = New Point(0, 0 + LongSide + LongSide)  
            Points(5) = New Point(0 - ShortSide, 0 + LongSide)  
            Points(6) = New Point(0, 0)  
    

    Best Regards.
    Jiachen Li

    ----------

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


2 additional answers

Sort by: Most helpful
  1. Jiachen Li-MSFT 34,221 Reputation points Microsoft External Staff
    2022-05-31T03:15:12.96+00:00

    Hi @user20 ,
    You can use Graphics.DrawPolygon Method to draw hexagons.
    Calculate your desired hexagon vertices based on Point2.
    Best Regards.
    Jiachen Li

    ----------

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. user20 161 Reputation points
    2022-06-01T08:43:59.943+00:00
     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
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.