Click here to Skip to main content
15,400,390 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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! 
 code is:

<pre lang="VB">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 pen1 As New Pen(Color.Black, 1.5)
    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)

          
    End Sub

End Class


What I have tried:

I want draw a line that have a hexagon at the end
Posted
Updated 29-May-22 3:20am
Comments
Richard MacCutchan 29-May-22 3:40am
   
You have not explained what the problem is. Drawing a hexagon is a matter of drawing six line segments each of the same length. At the end of each segment you need to alter direction by 120 degrees in a clockwise direction. At the end of the sixth segment you should have a closed hexagon.
Greg Utas 29-May-22 8:25am
   
And should a projection of the line (a) bisect the hexagon through two vertices, (b) through two edges, (c) form one of the hexagon's sides, or (d) I don't care as long as it's a hexagon! Decisions, decisions. :)
Richard MacCutchan 29-May-22 8:32am
   
My thoughts exactly. :)
Although I have just spent the last half hour studying Hexagon[^].
moshaveran 29-May-22 9:16am
   
I know how draw a hexagon. with this codes:
Public Sub DrawHex(x As Integer, y As Integer)
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(5) As PointF
Points(0) = New PointF(x, y)
Points(1) = New PointF(x + side, y)
Points(2) = New PointF(x + side + ShortSide, y + LongSide)
Points(3) = New PointF(x + side, y + LongSide + LongSide)
Points(4) = New PointF(x, y + LongSide + LongSide)
Points(5) = New PointF(x - ShortSide, y + LongSide)
bmg.DrawPolygon(p, Points)
End Sub

but my problem is that I need draw a line that have a hexagon at the end.
In a new post, I will show a picture of what I want
Richard MacCutchan 29-May-22 9:28am
   
That is just a question of drawing a line and drawing a hexagon, and connecting them at some common point.

1 solution

https://s6.uupload.ir/files/1233_hd1t.gif[^]

It should be hexagonal at the end of the line instead of a circle.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900