Page 1 of 1

Haversine formula

Posted: Tue Nov 11, 2025 5:16 am
by viktor_au
Hi
Wonder if anybody work with the gps distance calculation.
Any suggestions how to implement the Haversine formula?
Thanks.

Re: Haversine formula

Posted: Tue Nov 11, 2025 9:14 am
by chipfryer27
Hi Viktor

Whilst not actually doing it myself I did earmark this site as it gave many different ways to calculate, and also mentions a TRS-80 being used to calculate (my second computer)

https://www.movable-type.co.uk/scripts/latlong.html

Might be of some interest.

I found a lot of sites "scary" with their equations, but it isn't too difficult really and the above site also has some excel formulas.

Regards

Re: Haversine formula

Posted: Tue Nov 11, 2025 10:58 am
by medelec35
Hi Victor.
This is what I found:
Haversine Formula - Simple Explanation
The Haversine formula calculates the distance between two points on Earth using their latitude and longitude coordinates. It accounts for Earth being a sphere (roughly), not flat.
What it does: Given two GPS coordinates (lat1, lon1) and (lat2, lon2), it tells you the straight-line distance across Earth's surface.
The Formula Simplified
The formula works in steps:

Calculate the differences in latitude and longitude
Apply sine functions to account for Earth's curvature
Use Earth's radius (6371 km) to convert to actual distance

Implementation in Flowcode
Here's how to break it down into Flowcode calculation icons:
Step 1: Convert degrees to radians
lat1_rad = lat1 × π / 180
lon1_rad = lon1 × π / 180
lat2_rad = lat2 × π / 180
lon2_rad = lon2 × π / 180

Step 2: Calculate differences
dlat = lat2_rad - lat1_rad
dlon = lon2_rad - lon1_rad

Step 3: Haversine calculation
a = sin(dlat/2)² + cos(lat1_rad) × cos(lat2_rad) × sin(dlon/2)²
c = 2 × atan2(√a, √(1-a))
distance = 6371 × c // Result in kilometers

In Flowcode Calculation Icons:
Use separate calculation icons for each step
Store intermediate results in float variables
Use the built-in sin(), cos(), sqrt() and atan() functions

You can then use the link that chipfryer27 posted for testing the formula works.
If will be best if you use floats for the constants, e.g. instead of 2 use 2.0

Re: Haversine formula

Posted: Tue Nov 11, 2025 6:12 pm
by viktor_au
Thank you guys for the help.
Will try to implement.
I thought I have to find the difference between the (for example) lat2-lat1 before the radian conversion.

Re: Haversine formula

Posted: Fri Nov 14, 2025 8:34 pm
by viktor_au
Hi again.
Haversine
Did not work for me.

a = sin² (Δlat / 2) + cos(lat1) × cos(lat2) × sin²(Δlong / 2)

Flowcode:
a = (sin (((lat_diff / 2) * sin (lat_diff / 2))) + cos (Lat1) * cos (Lat2) * (sin (lon_diff / 2) * sin (lon_diff / 2)))
c = 2 * atan2 (sqrt (a),sqrt (1 - a))
d = 6371.0 * c

Example1 -Warsaw to Rome-1787 km
Result: 17.72km
LatDeg1 = 52.2296
LonDeg1 = 21.0122
LatDeg2 = 48.8919
LonDeg2 = 12.5113

Other examples:
0.42, instead of 20km
0.03 instead of 2km



Pythagorean theorem
https://smart.dhgate.com/a-simple-guide ... -location/
https://youtu.be/CWUr6Jo6tag

Flowcode

convert to radians, find the difference, etc.
c = sqrt (((lon2 - lon1) * (lon2 - lon1)) + ((lat2 - lat1) * (lat2 - lat1)))
d = 6371.0 * c

Examples
2.05km for 2km (as should be)
24.385km for 20km
1015km, instead of Warsaw to Rome-1787 km
4974km, instead of 3935.75km



Equirectangular method (used by Google maps)
convert to radians, find the difference, etc.
a = sqrt ((lon_diff * cos ((lat1 + lat2) / 2)) * (lon_diff * cos ((lat1 + lat2) / 2)) + (lat2 - lat1) * (lat2 - lat1))
c=a
d=6371.0 * c

Examples
2.05km for 2km (as it should be)
24.37km for 20km
1014km, instead of Warsaw to Rome-1787 km
4971km, instead of 3935.75km

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Looks like the Pythagorean method works OK for a small distances (<=20km) and should be OK for my boat. Note from internet: The Pythagorean formula can be used for GPS coordinates on a small, flat scale, but for accurate distances on Earth, a more complex formula like the Haversine formula is needed to account for the planet's curvature.

Re: Haversine formula

Posted: Fri Nov 14, 2025 11:43 pm
by chipfryer27
Hi Viktor

Your coordinates are a bit off :)
LatDeg2 = 48.8919
LonDeg2 = 12.5113
Puts you roughly at Straubing airport in Germany, 705Km from Warsaw.

My attached chart gets the same result as in that link I sent.

I made a constant of Pi/180 using 3.1415926 for Pi and more or less used Martin's calculation. You could probably compress the calculations a bit (and tidy things up).

Regards

Edit...km)
A bit bored so I entered details of Manhattan and London. Chart was within 20Km of the site in the link (5568Km)

Re: Haversine formula

Posted: Sat Nov 15, 2025 5:57 am
by viktor_au
Hi Iain.
You are a good man.
I see the difference between the:
-------------------------------------------------------------------------
a = (sin (((lat_diff / 2) * sin (lat_diff / 2))) + cos (Lat1) * cos (Lat2) * (sin (lon_diff / 2) * sin (lon_diff / 2)))
And:
a = ((sin(dlat / 2) * sin(dlat / 2))) + (cos(lat1_rad) * cos(lat2_rad)) * ((sin(dlon / 2) * sin(dlon / 2)))
-----------------------------------------------------
If London= 51.5072° N, 0.1276° W, OR:
London latitude = 51.5072,
If Manhattan latitude =40.7685° N, 73.9822° W
The distance between London and Manhattan should be approximately
5,5785 kilometers. The result is: 5564.8km
Thank you for the correction.
And 2km distance work OK as well.

Re: Haversine formula

Posted: Sun Nov 16, 2025 2:59 pm
by medelec35
Are you wanting additional help or are you happy with the help you ae given?

Re: Haversine formula

Posted: Mon Nov 17, 2025 12:03 am
by viktor_au
Hi
All good. Thank you.