ABCDE ABCDE - 19 days ago 11
Vb.net Question

Drawing hexagon in vb.net

I want to build a hexagon-shaped button. Here's the code I've got.

Dim p(5) As Point
Dim v As Integer = CInt(Me.Width / 2 * Math.Sin(30 * Math.PI / 180))
p(0) = New Point(Me.Width \ 2, 0)
p(1) = New Point(Me.Width, v)
p(2) = New Point(Me.Width, Me.Height - v)
p(3) = New Point(Me.Width \ 2, Me.Height)
p(4) = New Point(0, Me.Height - v)
p(5) = New Point(0, v)


Unfortunately, it appears hexagon with the point at the top. What I want is a hexagon with horizontal line at the top.

Thanks!

Answer

You can just swap x and y coordinates, which effectively mirrors the shape at the system's diagonal. And adapt the scaling to Width and Height accordingly:

Dim v As Integer = CInt(Me.Height/ 2 * Math.Sin(30 * Math.PI / 180))
p(0) = New Point(0, Me.Height\ 2)
p(1) = New Point(v, Me.Height)
p(2) = New Point(Me.Width- v, Me.Height)
p(3) = New Point(Me.Width, Me.Height\ 2)
p(4) = New Point(Me.Width - v, 0)
p(5) = New Point(v, 0)

Be aware that this reverses the point order. If your processing method relies on that, you may need to re-order the points.

Comments