Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 我的数学看起来不错,但我的抽绳看起来很奇怪。[六边形网格]_Vb.net - Fatal编程技术网

Vb.net 我的数学看起来不错,但我的抽绳看起来很奇怪。[六边形网格]

Vb.net 我的数学看起来不错,但我的抽绳看起来很奇怪。[六边形网格],vb.net,Vb.net,我想我需要一双新的眼睛来看看这个。或者可能是我的数字不够具体的问题。我是一个编程新手,我唯一的直觉是递归 基本上,我的六边形在某些行中的位置不正确,但在其他行中不正确,我希望它们都是平滑的 Public Class Form1 Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint 'Side Length Dim side As Integer =

我想我需要一双新的眼睛来看看这个。或者可能是我的数字不够具体的问题。我是一个编程新手,我唯一的直觉是递归

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

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

我已经修改了你们评论的建议,并修复了我的bug。我没有使用正确的数据类型。此外,我还使用了绘图线和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 Class

因此,编写一些测试代码。一次画一个六边形,看看哪里/什么时候出了问题,然后你就可以观察到当时使用的实际值。您可以通过调试和测试来修复bug,而不仅仅是阅读代码。此外,我倾向于查看对
DrawLine
的单个调用,而不是对
DrawLine
的多个调用。最后,您应该处理
,在处理过程中,您应该使用
语句创建
。唯一能让我的六边形光滑的是当边的长度是偶数时。奇数可能没有我想的那么准确。我会继续修补。我将研究这些改进!谢谢调试之前,请将Option Strict设置为On。使用所有浮点值(单个)更正赋值(和错误)。另外,请注意,有一个
e.Graphics.DrawLines()
方法(复数),但也有
e.Graphics.DrawPolygon()
。所有这些都接受浮点位置。您还可以浏览
GraphicsPath.AddPolygon()
。因为您使用的是尺寸为1的笔,所以可以使用不需要处理的StockObject(
Pens.Gray
)。或者您在最后(关闭表单/应用程序时)丢弃的预定义自定义笔()将
选项Strict On
放在
公共类表单1
之前的最顶端。VS将突出显示您的问题。将突出显示的
Integer
s替换为
Single
,它将看起来平滑。在循环之前添加
e.Graphics.SmoothingMode=SmoothingMode.antialas
e.Graphics.PixelOffsetMode=PixelOffsetMode.Half
。看到区别了吗?你应该使用单,而不是双。在循环中,您在
点中有从双到单的常量强制转换。Add()
+
Dim apothem
Dim half
应移到循环外。您只需计算一次。需要时转换为单个(例如,
CSng((Math.Sqrt(3)/2.0F)*面)
)。对所有其他变量使用单个值:侧边、宽度等。顺便说一句,
width
height
是坏名称(与控件属性相同)。使用类似于
GridWidth
GridHeight
等的东西。我做了2dGraphics建议,我喜欢它们,但在强制转换整数部分,你失去了我。简单。所有局部变量都应声明为
Single
。当您这样做时,您将在所有计算中保持一致的行为。您需要使用
列表(点F)
,然后添加
新点F()
,并且不要再次强制转换为
整数。那太糟糕了。使用
PointF
结构,它接受
单个数据类型。=>在此过程中,任何地方都不要使用
整数
。我在回答中编辑了我的代码以符合您的建议。常量强制转换修复和改进了图形。谢谢