Page 1 of 1
Calculate bearing
Posted: Sun Dec 15, 2013 6:18 pm
by Peter@multidesk.se
I wonder if there is anyone who knows how to calculate the bearing between two waypoints using LC, it would be great if someone has a code example or something like that I found on the forum, that described how to calculate the distance between two waypoints.
I have struggled with this for some time now and have come to the conclusion that I really need some help to resolve this.
/Peter
Re: Calculate bearing
Posted: Mon Dec 16, 2013 10:51 am
by jmburnod
Hi Peter,
how to calculate the distance between two waypoints
With a function like this
Code: Select all
function DistanceBetweenTwoPoints pLoc1,pLoc2
put abs(item 1 of pLoc1-item 1 of pLoc2) into difH1
put abs(item 2 of pLoc1-item 2 of pLoc2) into difV1
put sqrt(difH1^2 + difV1^2) into tL
put round(tL) into hypoLength
return hypoLength
end DistanceBetweenTwoPoints
I hope this help
Best regards
Jean-Marc
Re: Calculate bearing
Posted: Mon Dec 16, 2013 6:17 pm
by GoneToSail
Hi Peter,
I also need to calculate distance and bearing, and I found some time ago an interesting doc at the following web page:
http://www.movable-type.co.uk/scripts/latlong.html
But I've not used it yet...
Please keep me informed about any other documentation that you may find on this subject...
Re: Calculate bearing
Posted: Tue Dec 17, 2013 11:11 am
by Peter@multidesk.se
Jean-Marc
Just what I needed!
your code worked right away, without any problems
Thanks a lot
/Peter
Re: Calculate bearing
Posted: Tue Dec 17, 2013 3:07 pm
by jmburnod
Hi Peter,
Welcome
I was not sure I have understood completely "waypoints" and that is always a good point for me when I understand something in english
Best regards
Jean-Marc
Re: Calculate bearing
Posted: Tue Dec 17, 2013 6:52 pm
by Peter@multidesk.se
no! I was wrong.
the code works but does not give the answer I need. It answers with number close to zero, not with the range of 0 - 360 that I need.
I dont know if it's me that does something wrong, so I still need help with this problem.
I have two geographic points that my app needs to figure out the exact bearing of.
I have found some examples on the internet but can not figure out how it works and how I can do this in LC.
I use signed decimal degrees without compass direction, where negative indicates west/south (e.g. 40.7486, -73.9864)
but that should not make any difference, right?
Formula: θ = atan2( sin(Δλ).cos(φ2), cos(φ1).sin(φ2) − sin(φ1).cos(φ2).cos(Δλ) )
JavaScript: var y = Math.sin(dLon) * Math.cos(lat2);
var x = Math.cos(lat1)*Math.sin(lat2) -
Math.sin(lat1)*Math.cos(lat2)*Math.cos(dLon);
var brng = Math.atan2(y, x).toDeg();
/Peter
Re: Calculate bearing
Posted: Tue Dec 17, 2013 9:06 pm
by jacque
This should help:
http://lessons.runrev.com/s/lessons/m/4 ... al-compass
The heading is returned to you already calculated in the mobileSensorReading ("heading", true) command.
Re: Calculate bearing
Posted: Wed Dec 18, 2013 7:54 am
by endernafi
Peter hi,
Jean-Marc's script is for distance-calculation not for bearing and it doesn't take the earth's spherical - ellipsoidal shape into account.
That's why you get different results {some fine, some wrong} from it.
GoneToSail's
link is a valuable resource, I think.
And what I understand, it has everything you need, even more...
It doesn't seem that hard to convert those formulae into Livecode scripts.
I would give a shot, if I were you.
Peter@multidesk.se wrote:
I have found some examples on the internet but can not figure out how it works and how I can do this in LC.
Here is the first formula written in Javascript:
Code: Select all
var R = 6371; // km
var dLat = (lat2-lat1).toRad();
var dLon = (lon2-lon1).toRad();
var lat1 = lat1.toRad();
var lat2 = lat2.toRad();
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
And that code translated to revTalk:
Code: Select all
function haversineDistance pLat1, pLat2, pLon1, pLon2
local tRadiusOfEarth
local tDeltaLat, tDeltaLon
local tLat1_Rad, tLat2_Rad
local tValueA, tValueC, tValueD
put 6371 into tRadiusOfEarth
put degToRad(pLat2 - pLat1) into tDeltaLat
put degToRad(pLon2 - pLon1) into tDeltaLon
put degToRad(pLat1) into tLat1_Rad
put degToRad(pLat2) into tLat2_Rad
put sin(tDeltaLat / 2) * sin(tDeltaLat / 2) + sin(tDeltaLon / 2) * sin(tDeltaLon / 2) * cos(tLat1_Rad) * cos(tLat2_Rad) into tValueA
put 2 * atan2((tValueA * tValueA), ((1 - tValueA) * (1 - tValueA))) into tValueC
put tRadiusOfEarth * tValueC into tValueD
return tValueD
end haversineDistance
function degToRad pDeg
return ((pDeg * pi) / 180)
end degToRad
function radToDeg pRad
return ((pRad * 180) / pi)
end radToDeg
Notice that this is just a conversion of codes.
Probably it's not what you want exactly.
But there are many algorithms in that page.
Use whichever suits to your needs.
Hope it helps...
Best,
~ Ender Nafi
Re: Calculate bearing
Posted: Wed Dec 18, 2013 4:30 pm
by Peter@multidesk.se
When I try the code below, I get the answer 0.00 ... something, when in fact, I should get 222.059, which is the true direction between these waypoints.
how come?
/ Peter
Code: Select all
on mouseUp
put "59.562955" into plat1
put "17.046411" into plon1
put "58.02317100" into plat2
put "14.46755500" into plon2
local tRadiusOfEarth
local tDeltaLat, tDeltaLon
local tLat1_Rad, tLat2_Rad
local tValueA, tValueC, tValueD
put 6371 into tRadiusOfEarth
put degToRad(pLat2 - pLat1) into tDeltaLat
put degToRad(pLon2 - pLon1) into tDeltaLon
put degToRad(pLat1) into tLat1_Rad
put degToRad(pLat2) into tLat2_Rad
put sin(tDeltaLat / 2) * sin(tDeltaLat / 2) + sin(tDeltaLon / 2) * sin(tDeltaLon / 2) * cos(tLat1_Rad) * cos(tLat2_Rad) into tValueA
put 2 * atan2((tValueA * tValueA), ((1 - tValueA) * (1 - tValueA))) into tValueC
put tRadiusOfEarth * tValueC into tValueD
return tValueD
end mouseUp
function degToRad pDeg
return ((pDeg * pi) / 180)
end degToRad
function radToDeg pRad
return ((pRad * 180) / pi)
end radToDeg
Re: Calculate bearing
Posted: Wed Dec 18, 2013 6:51 pm
by endernafi
Of course you'll get
0.00126; because I, being a lousy coder, made a terrible mistake
{edit: my mistake, or typo if you will, was taking the
square instead of
square root }
Here is the correct one:
Code: Select all
on mouseUp
local tLat1, tLon1, tLat2, tLon2
put "59.562955" into tLat1
put "17.046411" into tLon1
put "58.023171" into tLat2
put "14.467555" into tLon2
answer haversineDistance(tLat1, tLat2, tLon1, tLon2)
end mouseUp
function haversineDistance pLat1, pLat2, pLon1, pLon2
local tRadiusOfEarth
local tDeltaLat, tDeltaLon
local tLat1_Rad, tLat2_Rad
local tValueA, tValueC, tValueD
put 6371 into tRadiusOfEarth
put degToRad(pLat2 - pLat1) into tDeltaLat
put degToRad(pLon2 - pLon1) into tDeltaLon
put degToRad(pLat1) into tLat1_Rad
put degToRad(pLat2) into tLat2_Rad
put sin(tDeltaLat / 2) * sin(tDeltaLat / 2) + sin(tDeltaLon / 2) * sin(tDeltaLon / 2) * cos(tLat1_Rad) * cos(tLat2_Rad) into tValueA
put 2 * atan2(sqrt(tValueA), sqrt(1 - tValueA)) into tValueC
put tRadiusOfEarth * tValueC into tValueD
return tValueD
end haversineDistance
function degToRad pDeg
return ((pDeg * pi) / 180)
end degToRad
function radToDeg pRad
return ((pRad * 180) / pi)
end radToDeg
This will give you the correct distance as
226.7km.
By the way, Peter; this was just an example which demonstrates translating Javascript code to Livecode script.
And I chose only the first algorithm on that page.
So, it won't give you the
direction or the
bearing, just the distance
I don't know which method you'll need for bearing that's why I didn't convert another code to Livecode.
Decide the algorithm, then I can happily translate that, too...
Best,
~ Ender Nafi
Re: Calculate bearing
Posted: Wed Dec 18, 2013 7:10 pm
by endernafi
Ok, I'm bored and had nothing to do for the past 10 minutes.
So, here's your
bearing code according to the linked page's algorithms.
Code: Select all
on mouseUp
local tLat1, tLon1, tLat2, tLon2
put "59.562955" into tLat1
put "17.046411" into tLon1
put "58.023171" into tLat2
put "14.467555" into tLon2
answer initialBearing(tLat1, tLat2, tLon1, tLon2)
end mouseUp
function initialBearing pLat1, pLat2, pLon1, pLon2
local tLat1, tLat2, tLon1, tLon2
local tDeltaLat, tDeltaLon
local tValueX, tValueY
local tBearing, tNormalizedBearing
put degToRad(pLat2 - pLat1) into tDeltaLat
put degToRad(pLon2 - pLon1) into tDeltaLon
put degToRad(pLat1) into tLat1
put degToRad(pLat2) into tLat2
put degToRad(pLon1) into tLon1
put degToRad(pLon2) into tLon2
put sin(tDeltaLon) * cos(tLat2) into tValueY
put cos(tLat1) * sin(tLat2) - sin(tLat1) * cos(tLat2) * cos(tDeltaLon) into tValueX
put radToDeg(atan2(tValueY, tValueX)) into tBearing
put ((tBearing + 360) mod 360) into tNormalizedBearing
return tNormalizedBearing
end initialBearing
function degToRad pDeg
return ((pDeg * pi) / 180)
end degToRad
function radToDeg pRad
return ((pRad * 180) / pi)
end radToDeg
Hope it helps,
~ Ender Nafi
Re: Calculate bearing
Posted: Thu Dec 19, 2013 10:19 am
by Peter@multidesk.se
Ender Nafi, you're a rock!
Now it works exactly as it is supposed.
Math is certainly not my forte, so I really needed to get some help with this.
Thanks!
/ Peter