已知条件求圆心角
在一整圆上有两个任意点,已知起点坐标(X1,Y1),终点坐标(X2,Y2),圆心坐标(XC,YC),半径(R),起点终点间的直线距离(L),起点和终点将圆打断为两段弧长不相等的圆弧,已知弧长(C1,C2)
求如果起点到终点是顺时针方向的话,起点到终点间圆弧的圆心角是大于180度还是小于180度
求如果起点到终点是逆时针方向的话,起点到终点间圆弧的圆心角是大于180度还是小于180度
求一高效的算法,谢谢
[解决办法]
Const PI = 3.1415927
Private Function Angle(x1 As Single, y1 As Single, x2 As Single, y2 As Single, xc As Single, yc As Single, bisClockwise As Boolean)
Dim x3 As Single, y3 As Single
Dim x4 As Single, y4 As Single
'//transform your coordinate
x3 = x1 - xc: y3 = y1 - yc: x4 = x2 - xc: y4 = y2 - yc
'//From the first point to the 2nd poinit, use cross product to check the angle
Dim z As Single, bisLessThanPi As Boolean
If bisClockwise Then
z = x3 * y4 - y3 * x4
Else
z = y3 * x4 - x3 * y4
End If
If z > 0 Then
bisLessThanPi = True
Else
bisLessThanPi = False
End If
'//Get the angle between
Dim cos As Double
cos = (x3 * x4 + y3 * y4) / (x3 ^ 2 + y3 ^ 2)
'//check for 0 or 180 degrees
If Round(Abs(cos), 3) < 0.001 Then
Angle = PI / 2
ElseIf Round(Abs(Abs(cos) - 1), 3) < 0.001 And cos < 0 Then
Angle = PI
ElseIf Round(Abs(Abs(cos) - 1), 3) < 0.001 And cos > 0 Then
Angle = 0
Else
Angle = Arccos(cos)
End If
If bisLessThanPi = False Then
Angle = 2 * PI - Angle
End If
'//Convert to degree
Angle = Angle * 180 / PI
End Function
Private Function Arccos(x As Double) As Double
Arccos = PI / 2 - Atn(x / Sqr(-x * x + 1))
End Function
Option ExplicitPrivate Sub Rotate(X As Single, Y As Single, ByVal sinT As Single, ByVal cosT As Single) Dim dX As Single Dim dY As Single dX = X dY = Y X = dX * cosT + dY * sinT Y = -dX * sinT + dY * cosTEnd SubPrivate Sub Command1_Click() Dim X1 As Single Dim X2 As Single Dim Y1 As Single Dim Y2 As Single Dim XC As Single Dim YC As Single Dim R As Single Dim sinT As Single Dim cosT As Single Me.Cls Me.Line (-100, 0)-(100, 0) Me.Line (0, -100)-(0, 100) X1 = 61.132 Y1 = -44.549 X2 = 54.107 Y2 = -52.519 XC = 81.698 YC = -69.759 '平移' X2 = X2 - X1 Y2 = Y2 - Y1 XC = XC - X1 YC = YC - Y1 X1 = 0 Y1 = 0 R = Sqr(XC ^ 2 + YC ^ 2) '旋转' sinT = YC / R cosT = XC / R Rotate X2, Y2, sinT, cosT Rotate XC, YC, sinT, cosT Me.Circle (X1, Y1), 1, vbBlue Me.Circle (X2, Y2), 1, vbGreen Me.Circle (XC, YC), 1, vbRed Me.Circle (XC, YC), R '判断' Me.CurrentX = 20 Me.CurrentY = 20 If Y2 > 0 Then Me.Print "顺时针 小弧" If Y2 < 0 Then Me.Print "顺时针 大弧"End SubPrivate Sub Form_Load() Me.AutoRedraw = True Me.Scale (-100, 100)-(100, -100)End Sub