2,892 questions
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