Android Question Turn to degree

tufanv

Expert
Licensed User
Longtime User
Hello

For the past 2 hours i am trying mod , abs and many other things and when you think of something too much it goes harder and harder so i need a simple help of math and and android.

Lets say a line is traveling on 150 degress heading . I will turn this line to heading 340. If i turn by right turn , it need 190 degree of turn but if i turn by left turn it need 170 degree of turn so i need to select left turn. BUT :)

I can't write a simple 2 lines codes for this and at the end My head is totally mixed up. if initial heading is h1 and heading to be turned is h2 , how can i determine easily if it is a right turn or left turn .

Thanks
 

wonder

Expert
Licensed User
Longtime User
Not tested, drafted in Notepad++.

B4X:
'-------------------------------------------------------------------------------------------------------
Dim Const LEFT = -1, RIGHT = 1 As Short
'-------------------------------------------------------------------------------------------------------
Dim turningDirection As Short
Dim turningAngle, smallestAngle As Double
Dim angleDifferenceA, angleDifferenceB, absAngDiffA, absAngDiffB As Double
'-------------------------------------------------------------------------------------------------------
angleDifferenceA = h2 - h1
angleDifferenceB = angleDifferenceA - 360
absAngDiffA      = Abs(angleDifferenceA)
absAngDiffB      = Abs(angleDifferenceB)
smallestAngle    = Min(absAngDiffA, absAngDiffB)
'-------------------------------------------------------------------------------------------------------
If smallestAngle = absAngDiffA Then turningAngle = angleDifferenceA Else turningAngle = angleDifferenceB
'-------------------------------------------------------------------------------------------------------
If turningAngle < 0 Then
    turningDirection = LEFT
Else _
If turningAngle > 0 Then
    turningDirection = RIGHT   
End If
'-------------------------------------------------------------------------------------------------------
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
I will try this thanks !

After some more thinking i also found a formula but not tested too much
if h1 is smaller than h2 then
h1+360-h2 = left turn
h2-h1 = right turn
Chosse the small value

if h1 is grater than h2
h2-h1 i= left turn
h2+360-h1 =righ turn
choose small value.
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
Not tested, drafted in Notepad++.

B4X:
'-------------------------------------------------------------------------------------------------------
Dim Const LEFT = -1, RIGHT = 1 As Short
'-------------------------------------------------------------------------------------------------------
Dim turningDirection As Short
Dim turningAngle, smallestAngle As Double
Dim angleDifferenceA, angleDifferenceB, absAngDiffA, absAngDiffB As Double
'-------------------------------------------------------------------------------------------------------
angleDifferenceA = h2 - h1
angleDifferenceB = angleDifferenceA - 360
absAngDiffA      = Abs(angleDifferenceA)
absAngDiffB      = Abs(angleDifferenceB)
smallestAngle    = Min(absAngDiffA, absAngDiffB)
'-------------------------------------------------------------------------------------------------------
If smallestAngle = absAngDiffA Then turningAngle = angleDifferenceA Else turningAngle = angleDifferenceB
'-------------------------------------------------------------------------------------------------------
If turningAngle < 0 Then
    turningDirection = LEFT
Else _
If turningAngle > 0 Then
    turningDirection = RIGHT  
End If
'-------------------------------------------------------------------------------------------------------

I think it is not wrong. I am testing it now and it works perfect. Are you sure it has a problem =?
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
@wonder : I also thought it was smart to pick the right variable type for certain situations. like short or byte for tiny values.

But I noticed everything gets A LOT slower when you get away from the int type. not sure why tho.

In this case it doesn't matter but you'll see your timing multipled several times on heavier routines.
 
Upvote 0

wonder

Expert
Licensed User
Longtime User
B4X:
Sub GetTurningAngle(initialHeading As Double, finalHeading As Double) As Double
    '----------------------------------------------------------
    Dim angleDifferenceA, angleDifferenceB,     _
        absAngDiffA, absAngDiffB, smallestAngle _
    As Double
    '----------------------------------------------------------
    angleDifferenceA = finalHeading - initialHeading
    angleDifferenceB = angleDifferenceA - 360
    absAngDiffA      = Abs(angleDifferenceA)
    absAngDiffB      = Abs(angleDifferenceB)
    smallestAngle    = Min(absAngDiffA, absAngDiffB)
    '----------------------------------------------------------
    If smallestAngle = absAngDiffA Then Return angleDifferenceA
    '----------------------------------------------------------
    Return angleDifferenceB
    '----------------------------------------------------------
End Sub
 
Last edited:
Upvote 0

wonder

Expert
Licensed User
Longtime User
@wonder : I also thought it was smart to pick the right variable type for certain situations. like short or byte for tiny values.

But I noticed everything gets A LOT slower when you get away from the int type. not sure why tho.

In this case it doesn't matter but you'll see your timing multipled several times on heavier routines.
Wow, I didn't know that... I try to run some tests later on this week... Thanks for the tip! :)
 
Upvote 0
Top