# Android QuestionAngle between two points

#### Abdou1283

##### Member
HI all, i want to calculate the angle betwwen two points with (Latitude/Longtitude) predefined

#### Peter Simpson

##### Expert
Angle = atan2(y2 - y1, x2 - x1) * 180 / PI

If so you can easily find out more about the formula and then implement it into your application. Just do a quick Google search on the formula above to learn how it works and implement your lat/Lon (x2).

But then again have you thoroughly searched forum??? Last edited:
• Erel and DonManfred

#### Abdou1283

##### Member
Ok that's it. Thank you Peter. #### Star-Dust

##### Expert
also
B4X:
``Angle = atan2D(y2 - y1, x2 - x1)``

#### klaus

##### Expert
Or, if you already use the GPS library:
B4X:
``````Private Loc1, Loc2 As Location
Loc1.Initialize2(Lat1, Lng1)
Loc2.Initialize2(Lat2, Lng2)
Angle = Loc1.BearingTo(Loc2)``````

#### Peter Simpson

##### Expert
Here you go @Abdou1283,
I've just found this on Google about 10 minutes ago (1st page result), so I quickly put this code together.

Give it a go, it's a bit messy but it can be approved upon, but I only had 10 minutes.

Angle (points 1 to 2) is at 321.2°
B4X:
``````'It's looks about right on Google maps from the 2 points that I used.
Dim r As Int = 6371 '6371000

Dim lat1 As Double = 52.419242
Dim lon1 As Double = -1.770847
Dim x1 As Double = r * Cos(lat1) * Cos(lon1)
Dim y1 As Double = r * Sin(lat1) * Cos(lon1)

Dim lat2 As Double = 52.539881
Dim lon2 As Double = -1.898292
Dim x2 As Double = r * Cos(lat2) * Cos(lon2)
Dim y2 As Double = r * Sin(lat2) * Cos(lon2)

Dim Angle As Double = ATan2(y2 - y1, x2 - x1) * 180 / cPI
If Angle < 0 Then Angle = 360 + Angle

Log(\$"Angle (points 1 to 2) is at \${NumberFormat(Angle, 1, 2)}°"\$)``````

Enjoy...

Last edited:
• Abdou1283, Erel and DonManfred

#### KMatle

##### Expert
With 2 points you can't calculate an angle. You need two lines. So at least you need a 3rd point (e.g. heading) which gives you a base line to calc the angle.

#### Peter Simpson

##### Expert
With 2 points you can't calculate an angle. You need two lines. So at least you need a 3rd point (e.g. heading) which gives you a base line to calc the angle.

Hiya @KMatle, so whats the solution then as in a formula?
I'm interested as I only tested it via a short distance and that was not a bad result, not perfect mind you. I'm going to presume that the further the distance the more of an error factor there will be with the angle being correct.

Cheers...

#### klaus

##### Expert
Hi Peter,
The code looks also strange to me.
Cos(Angle) expects an angle expressed in radians, but the angles in the code are in degrees !?
I'm afraid that by chance it gives a correct result with the lat and lon values you used.
I tried with lat1 = 0, lon1 = 0 and lat2 = 1 and lon2 = 1 which should give an angle of about 45°, the result of the code you posted gives 147°.
Then, I tried with lat1 = 0, lon1 = 0 and lat2 = 10 and lon2 = 10 which should also give an angle of about 45°, the result of the code you posted gives 123°.
That's why I am interested to see the original code.

Dim Angle As Double = ATan2(y2 - y1, x2 - x1) * 180 / cPI
you could use
Dim Angle As Double = ATan2D(y2 - y1, x2 - x1)
which returns directly degrees .

With 2 points you can't calculate an angle.
Here we speak of bearing, an angle on the compass, which is defined by two locations.

• Peter Simpson

#### emexes

##### Expert
Combination of code from various sources incl. above. Uses course heading angle formula from a guy that seriously knows his stuff. Give it a burl.
B4X:
``````Sub MainStartTest

Dim lat1 As Double = 52.419242
Dim lon1 As Double = -1.770847

Dim lat2 As Double = 52.539881
Dim lon2 As Double = -1.898292

Log(CourseFromLonLatDegrees(lat1, lon1, lat2, lon2))

End Sub

Sub CourseFromLonLatDegrees(Lat1 As Double, Lon1 As Double, Lat2 As Double, Lon2 As Double) As Double

Dim DegreesToRadians As Double = cPI / 180

End Sub

Sub CourseFromLonLatRadians(Lat1 As Double, Lon1 As Double, Lat2 As Double, Lon2 As Double) As Double

Dim tc1 As Double

'formula from: https://edwilliams.org/avform.htm#Crs

If (Cos(Lat1) < 0.0000001) Then    'within one metre of north or south pole
If (Lat1 > 0) Then
tc1 = cPI    'starting from north pole, thus heading south
Else
tc1= 0    'starting from south pole, thus heading north
End If
Else
'Lon1 and Lon2 reversed in subtraction due to formula convention of west = +ve
tc1 = ATan2(Sin(Lon2 - Lon1) * Cos(Lat2), Cos(Lat1) * Sin(Lat2) - Sin(Lat1) * Cos(Lat2) * Cos(Lon2 - Lon1))

'translate to compass angle ie 0 = north, positive through E, S, W
If tc1 < 0 Then
tc1 = tc1 + 2 * cPI
End If
End If

Return tc1

End Sub``````

Last edited:
• Peter Simpson

#### Peter Simpson

##### Expert
Hiya @klaus,
I'm out on my bike for the day now, I need to clock up over 28 miles today.
I searched Google for 'convert latitude and longitude to x and y coordinates formula', the formula that I used was found on the first page, there are lots of them, the result will read 'Dim x As Double = 6371 * Cos(latitude) * Cos(longitude)', 'Dim y As Double = 6371 * Sin(latitude) * Cos(longitude)', there was also a Dim r as Double. When I get back I'll try to find it again for you but I've already ran CCleaner this morning which I do every few days. I'll find it when I get back though.

Dim Angle As Double = ATan2D(y2 - y1, x2 - x1)
Thank you, I live and learn every single day.

Sorry about the newby text formatting, but I'm currently on my bike thus no inserting of code tags etc.

Pete...

#### klaus

##### Expert
That's why I am interested to see the original code.
My interest was not in getting equations for the calculation, I already have some, but to see the original code and find where the error does come from.
The equations in post #7 look a bit strange to me.

Replies
6
Views
526
Replies
18
Views
902
Replies
3
Views
580
Android Question Get an angle between 2 lines
Replies
32
Views
3K
Replies
13
Views
2K