Draw a hexagon from the starting point whose coordinates we have

user20 161 Reputation points
2022-06-01T05:53:59.16+00:00

WE HAVE COORDINATE OF A POINT AND MUST DRAW A HEXAGON FROM THIS POINT. LIKE BELOW PICTURE:
207350-2.jpg

please help me.

Developer technologies VB
{count} votes

Accepted answer
  1. LesHay 7,141 Reputation points
    2022-06-01T19:08:28.543+00:00

    Hi
    Try this as a new test project. Blank Form1. This example follows the mouse and can be slightly shortened if just static hexagon(s) required.

    Option Strict On
    Option Explicit On
    Imports System.Drawing.Drawing2D
    Public Class Form1
        Dim GP As New GraphicsPath
        Dim len As Integer = 40
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DoubleBuffered = True
        End Sub
        Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
            Using p As New Pen(Color.Red, 4)
                e.Graphics.DrawPath(p, GetHex(PointToClient(MousePosition).X - len, PointToClient(MousePosition).Y, len))
            End Using
        End Sub
        Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles MyBase.MouseMove
            Invalidate()
        End Sub
        Function GetHex(x As Integer, y As Integer, sz As Integer) As GraphicsPath
            Dim g As New GraphicsPath
    
            Dim dr As Double = Math.PI / 180
            Dim xo As Single = Convert.ToSingle(Math.Sin(30 * dr) * sz)
            Dim xl As Single = Convert.ToSingle(Math.Cos(30 * dr) * sz)
    
            Dim p(5) As PointF
            p(0) = New PointF(x + sz, y)
            p(0) = New PointF(x + sz + xl, y + xo)
            p(1) = New PointF(x + sz + xl, y + xo + sz)
            p(2) = New PointF(x + sz, y + xo + xo + sz)
            p(3) = New PointF(x + sz - xl, y + xo + sz)
            p(4) = New PointF(x + sz - xl, y - xo + sz)
            p(5) = New PointF(x + sz, y)
            g.AddPolygon(p)
            Return g
        End Function
    End Class
    
    0 comments No comments

0 additional answers

Sort by: Most helpful

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.