通过给定 x 和 y 坐标通过循环创建重复线

Creating a repeating line through looping by giving the x and y coordinates

我正在试验 VB.Net 的绘画事件,对于这个试验,我想创建一个重复的水平或垂直(取决于我输入的参数)线并循环直到它遇到对应的终点x和y.

像这样:

给定 x 和 y 起点x 和 y 终点 函数应创建的目标从给定起点开始直到到达给定终点的垂直线或水平线。

我可以使用 paintevent 创建曲线和直线,但现在我不知道如何在给定的 x 和 y 起点和终点执行循环。

你有没有尝试过类似的东西:

For x = xstart to xend Step Spacing

Next

其中:

  • xstart = 你的起点
  • xend = 你的终点
  • 间距 = 行间距

你只需要使用一个For循环来迭代x/y坐标。这是一个例子:

Public Class Form1

    Private Enum Orientation
        Vertical
        Horizontal
    End Enum

    Protected Overrides Sub OnPaint(e As PaintEventArgs)

        Dim orient As Orientation = Orientation.Vertical
        Dim x As Integer = 100          'X Coord
        Dim y As Integer = 100          'Y Coord
        Dim count As Integer = 10       'Number of Lines to draw
        Dim spacing As Integer = 5      'Spacing between lines in pixels
        Dim length As Integer = 20      'Length of each line in pixels
        Dim thickness As Integer = 3    'Thickness of each line in pixels

        drawLines(x, y, orient, count, spacing, length, thickness, e.Graphics)
    End Sub

    Private Sub drawLines(x As Integer, y As Integer, orient As Orientation, count As Integer, spacing As Integer, length As Integer, thickness As Integer, g As Graphics)

        'Create the Pen in a using block so it will be disposed.  
        'The code uses a red pen, you can use whatever color you want
        Using p As New Pen(Brushes.Red, CSng(thickness))

            'Here we iterate either the x or y coordinate to draw each
            'small segment.
            For i As Integer = 0 To count - 1
                If orient = Orientation.Horizontal Then
                    g.DrawLine(p, x + ((thickness + spacing) * i), y, x + ((thickness + spacing) * i), y + length)
                Else
                    g.DrawLine(p, x, y + ((thickness + spacing) * i), x + length, y + ((thickness + spacing) * i))
                End If
            Next

        End Using

    End Sub
End Class