# Draw a hexagon from the starting point whose coordinates we have

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:

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,693 questions

1. 7,126 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
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)