0

我似乎很难创建一个画笔线功能,用户单击一个按钮来创建一条线,然后在表单上选择一个点并在文本框中输入一个长度,这也将成为线的长度,它的自己。

有人可以帮忙吗?

4

1 回答 1

1

您还必须考虑线的方向。这里有一些代码可以帮助您入门。您提供起点、线的长度以及以度为单位的角度,其中 0 度朝向正 x 轴。该方法将返回直线终点的 x、y 坐标:

Sub Main()

    Dim start As New PointF(0, 0)
    Dim length As Double = 10
    Dim degrees As Double = 45

    Dim endPoint As PointF = getEndPoint(start, length, degrees)

    Console.WriteLine("Start point: {0}", start)
    Console.WriteLine("Length: {0}", length)
    Console.WriteLine("Angle: {0}", degrees)
    Console.WriteLine("End point: {0}", endPoint)


    Console.WriteLine("Press ENTER to exit...")
    Console.ReadLine()
End Sub

Private Function getEndPoint(ByVal startPoint As PointF, ByVal length As Double, ByVal angleInDegrees As Double) As PointF
    Dim x As Double = startPoint.X + length * Math.Cos(degreesToRadians(angleInDegrees))
    Dim y As Double = startPoint.Y + length * Math.Sin(degreesToRadians(angleInDegrees))

    Return New PointF(CType(x, Single), CType(y, Single))
End Function

Private Function degreesToRadians(ByVal degrees As Double) As Double
    Return (Math.PI / 180.0) * degrees
End Function

此代码假定标准笛卡尔平面。由于 Windows 窗体的原点位于窗体的左上角,并且正 Y 轴在窗体的下方,因此如果您打算使用它在窗体上绘制线条,则必须调整此代码以使其正常工作。

在 Windows 窗体上,角度按顺时针方向增加,因此这里是 getEndPoint 方法的更新版本,它对此进行了调整(未完全测试):

Private Function getEndPoint(ByVal startPoint As PointF, ByVal length As Double, ByVal angleInDegrees As Double) As PointF
    Dim x As Double = startPoint.X + length * Math.Cos(degreesToRadians(360.0 - angleInDegrees))
    Dim y As Double = startPoint.Y + length * Math.Sin(degreesToRadians(360.0 - angleInDegrees))
于 2011-12-16T15:00:09.877 回答