Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Angle difference
#6
While writing test code, I very quickly came to insight that the ABS of the answer has to be <=180 degrees for degrees.

This led me to a more elegant and correct solution of:
Code: (Select All)
' 2023-01-31  Best! because in terms of +/- to angle1 a negative sign means a2 is clockwise to angle 1
Function DiffAngleD (A1Degrees, A2Degrees)
    ' This might be assuming both angles are between >= 0  and < 360
    If Abs(A1Degrees - A2Degrees) <= 180 Then
        DiffAngleD = A1Degrees - A2Degrees
    ElseIf Abs((A1Degrees + 360) - A2Degrees) <= 180 Then
        DiffAngleD = (A1Degrees + 360) - A2Degrees
    Else
        DiffAngleD = A1Degrees - (A2Degrees + 360)
    End If
End Function

EDIT: when I changed variable names I missed one

Test code:
Code: (Select All)
_Title "Angle Difference" ' b+ 2023-01-31 set up test code for AngleDifference Function tests
$Console:Only
' test code
For a1 = 0 To 360 Step 10
    For a2 = 0 To 360 Step 10
        Print a1, a2, DiffAngleD(a1, a2), dif_ang(a1, a2, 360)
        If DiffAngleD(a1, a2) <> dif_ang(a1, a2, 360) Then Beep: Sleep
    Next
Next

' 2023-01-31  Best! because in terms of +/- to angle1 a negative sign means a2 is clockwise to angle 1
Function DiffAngleD (A1Degrees, A2Degrees)
    ' This might be assuming both angles are between >= 0  and < 360
    If Abs(A1Degrees - A2Degrees) <= 180 Then
        DiffAngleD = A1Degrees - A2Degrees
    ElseIf Abs((A1Degrees + 360) - A2Degrees) <= 180 Then
        DiffAngleD = (A1Degrees + 360) - A2Degrees
    Else
        DiffAngleD = A1Degrees - (A2Degrees + 360)
    End If
End Function

'MasterGy version 2023-01-31   https://qb64phoenix.com/forum/showthread.php?tid=1434&pid=13123#pid13123
Function dif_ang (a, b, unit): a2 = a: b2 = b
    Do While Abs(a2 - b2) > Abs((a2 - unit) - b2): a2 = a2 - unit: Loop: Do While Abs(a2 - b2) > Abs((a2 + unit) - b2): a2 = a2 + unit: Loop: dif_ang = a2 - b2
End Function

EDIT: cleaned out stuff no longer useful

When I was trying to include OldMoses code I got stuck a bit on converting Spinner_Dot to an angle but I went ahead with my adjustment to +/- returns of Angle Degree measure.

If the AngleDiffD is negative it means the 2nd angle is Clockwise to the first, whereas if positive it means the 2nd angle is CCW to first.

PS MasterGy's looks more complicated but he is not assuming the angles input are between 0 and 360. +1 his code was very helpful in getting mine turned the right way.
b = b + ...
Reply


Messages In This Thread
Angle difference - by bplus - 01-31-2023, 05:04 PM
RE: Angle difference - by MasterGy - 01-31-2023, 05:50 PM
RE: Angle difference - by OldMoses - 01-31-2023, 06:15 PM
RE: Angle difference - by bplus - 01-31-2023, 06:44 PM
RE: Angle difference - by OldMoses - 01-31-2023, 07:33 PM
RE: Angle difference - by bplus - 01-31-2023, 08:12 PM
RE: Angle difference - by OldMoses - 01-31-2023, 10:32 PM
RE: Angle difference - by SMcNeill - 01-31-2023, 09:29 PM
RE: Angle difference - by bplus - 01-31-2023, 10:03 PM



Users browsing this thread: 2 Guest(s)