2

我正在使用一些简单的代码来绘制六边形,我得到了意想不到的结果。

代码如下:

Public Class Form1

Dim bm As New Bitmap(640, 480)
Dim bmg As Graphics = Graphics.FromImage(bm)
Dim p As Pen = New Pen(Color.Black)
Dim sb As SolidBrush = New SolidBrush(Color.Black)


Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    DrawHex(80, 50)
End Sub

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(6) 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

Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint

    e.Graphics.DrawImage(bm, New Point(10, 10))

End Sub

结束类

六边形的五个点都可以。这是最后一点没有正确绘制,我不明白为什么:

在此处输入图像描述

“ShortSide”和“LongSide”代表直角三角形在六边形之外的线。我很确定数学是正确的,我觉得我错过了一些明显的东西。

谢谢!

4

1 回答 1

2

数组是从零开始的。将其更改为:

Dim Points(5) As PointF

会给你六分。您的最后一点 (#7) 默认为 (0, 0)。

于 2012-08-20T23:30:09.960 回答