我想我只是需要一双全新的眼睛来看这个。或者是因为我的数字不够具体。我是编程初学者,我唯一的预感是递归。

本质上,我的六边形在某些行中的位置不正确,但其他行不正确,我希望它们都是光滑的。
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发布于 2018-11-10 00:16:46
我已经修改了你们的建议,并修正了我的错误。我没有使用正确的数据类型。此外,我使用e.Graphics.Drawlines和StockObject提示笔和处理它们。再次谢谢!
如果我可以对我的代码进行任何其他改进,请随意评论它们。
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 Classhttps://stackoverflow.com/questions/53220042
复制相似问题