首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我的数学看起来很好,但是我的画线看起来很奇怪。[六角网格]

我的数学看起来很好,但是我的画线看起来很奇怪。[六角网格]
EN

Stack Overflow用户
提问于 2018-11-09 04:51:16
回答 1查看 97关注 0票数 2

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

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

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-10 00:16:46

我已经修改了你们的建议,并修正了我的错误。我没有使用正确的数据类型。此外,我使用e.Graphics.Drawlines和StockObject提示笔和处理它们。再次谢谢!

https://imgur.com/B37MDwB

如果我可以对我的代码进行任何其他改进,请随意评论它们。

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53220042

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档