VB.NET计算两条直线的夹角,弧度转换

  • A+
所属分类:vb.net

vb.net做一个小程序,用到计算两条直线的角度,通常这个是画图才能用到的普遍一个算数题,vb.net本身貌似没有直接的这个函数,那我们自己造一个求两条直线的角度吧,首先我们分析,两条直线实际是两条线段,直线是无限长的,所以准确的说是两条线段,两条线段就是有4个端点。我们只取4点计算角度。直接上自定义计算两条直线的角度函数!

头部引入  Imports System.Math '三角函数

Function angle(ByVal PT1 As Point, ByVal PT2 As Point)
Dim JIAO As Single
Dim P As Point

P.X = PT2.X - PT1.X
P.Y = PT2.Y - PT1.Y

JIAO = Atan2(P.Y, P.X) * 180 / PI
Return JIAO '角度

' Return JIAO * PI / 180 ’弧度
End Function

复制

测试代码例子

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Dim point1 As New Point(0, 5) 'X0,Y5
Dim point2 As New Point(10, 0) 'X10 Y0

' MsgBox(angle(point1, point2))
RichTextBox2.Text = angle(point1, point2)

End Sub

复制

经测试角度26.565左右。在CAD上面画上面两条线段,测量角度,结果一样。赶快去试试吧!

 

最准确的计算方法参考 http://huojibk.com/2022/10/1198.html

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: