我的数学似乎不错,但我的画线看起来很奇怪。 [六角格]

时间:2018-11-09 04:51:17

标签: vb.net

我想我只需要换一个新的眼睛即可。也许这是我的数字不够具体的问题。我是编程的初学者,唯一的直觉是递归。

Hexagonal grid

基本上,我的六边形在某些行中的位置不正确,但在其他行中却不正确,我希望它们都光滑。

Public Class Form1
    Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
        'Side Length
        Dim side As Integer = 25
        'Grid Width
        Dim width As Integer = 10
        'Grid Height
        Dim height As Integer = 10
        'Starting X
        Dim startX As Integer = 100
        'Starting Y
        Dim startY As Integer = 100
        'Hexagon Border
        Dim borderPen As New Pen(Brushes.Gray, 1)

        For i = 1 To width Step 1
            For j = 1 To height Step 1

                Dim apothem As Integer = (Math.Sqrt(3) * side / 2)
                Dim half As Integer = (side / 2)
                Dim centerX As Integer = (startX + (side * i * 1.5))
                Dim centerY As Integer = (startY + (apothem * j * 2))

                If i Mod 2 = 0 Then
                    centerY += apothem
                End If

                e.Graphics.DrawLine(borderPen, (centerX - half), (centerY + apothem), (centerX + half), (centerY + apothem))
                e.Graphics.DrawLine(borderPen, (centerX + half), (centerY + apothem), (centerX + side), (centerY))
                e.Graphics.DrawLine(borderPen, (centerX + side), (centerY), (centerX + half), (centerY - apothem))
                e.Graphics.DrawLine(borderPen, (centerX + half), (centerY - apothem), (centerX - half), (centerY - apothem))
                e.Graphics.DrawLine(borderPen, (centerX - half), (centerY - apothem), (centerX - side), (centerY))
                e.Graphics.DrawLine(borderPen, (centerX - side), (centerY), (centerX - half), (centerY + apothem))
            Next
        Next
    End Sub
End Class

1 个答案:

答案 0 :(得分:1)

我已经修正了你们提出的建议,并修复了我遇到的错误。我没有使用正确的数据类型。另外,我利用e.Graphics.Drawlines和StockObject笔尖进行笔的处理。再次谢谢你!

https://imgur.com/B37MDwB

如果有任何其他改进,我可以对代码进行注释。

Option Strict On
Imports System.Drawing.Drawing2D

Public Class Form1
    Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles 
Me.Paint
        'Side Length
        Dim side As Single = 30
        'Grid Width
        Dim width As Single = 10
        'Grid Height
        Dim height As Single = 5
        'Starting X
        Dim startX As Single = 0
        'Starting Y
        Dim startY As Single = 0

        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias
        e.Graphics.PixelOffsetMode = PixelOffsetMode.Half

        Dim apothem As Single = CSng(Math.Sqrt(3) * side / 2.0F)
        Dim half As Single = (side / 2)

        For i = 1 To width Step 1
            For j = 1 To height Step 1
                Dim centerX As Single = (startX + (side * i * 1.5F))
                Dim centerY As Single = (startY + (apothem * j * 2))

                If i Mod 2 = 0 Then
                    centerY += apothem
                End If

                Dim points As New List(Of PointF)

                points.Add(New PointF((centerX - half), (centerY + apothem)))
                points.Add(New PointF((centerX + half), (centerY + apothem)))
                points.Add(New PointF((centerX + side), (centerY)))
                points.Add(New PointF((centerX + half), (centerY - apothem)))
                points.Add(New PointF((centerX - half), (centerY - apothem)))
                points.Add(New PointF((centerX - side), (centerY)))
                points.Add(New PointF((centerX - half), (centerY + apothem)))

                e.Graphics.DrawLines(Pens.Gray, points.ToArray())
            Next
        Next
    End Sub
End Class