# PureBasic Forum

 It is currently Fri Jul 19, 2019 4:45 am

 All times are UTC + 1 hour

 Page 2 of 2 [ 27 posts ] Go to page Previous  1, 2
 Print view Previous topic | Next topic
Author Message
 Post subject: Re: Has anyone done the 'Haversine' formula?Posted: Fri Feb 08, 2019 2:02 pm
 PureBasic Team

Joined: Fri Apr 25, 2003 6:14 pm
Posts: 1692
Location: Germany (Saxony, Deutscheinsiedel)
Thanks for all this wonderful codes, which I'm successfully using to calculate the distance between two geographic points!

But can you show me the other way around, meaning I have one point (latitude,longitude) and a specific distance (in km) around this point. How can I calculate the most-northern/southern latitude and most-western/eastern longitude matching this distance from the given point?

(I've seen a lot formulas on internet, but don't get it... latitude seems easy, but longitude... )

_________________
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)

Top

 Post subject: Re: Has anyone done the 'Haversine' formula?Posted: Fri Feb 08, 2019 3:44 pm
 PureBasic Expert

Joined: Fri Apr 25, 2003 4:34 pm
Posts: 894
Code:
Macro DistFromPoint (lat1,lon1,Distance,Bearing)
EndMacro

DistFromPoint(49,-94,20,0) ;<-- Most northern point 20km from lat/lon
Debug lat2
Debug lon2

DistFromPoint(49,-94,20,180) ;<-- Most southern point 20km from lat/lon
DistFromPoint(49,-94,20,90) ;<-- Most eastern point 20km from lat/lon
DistFromPoint(49,-94,20,270) ;<-- Most western point 20km from lat/lon

_________________

Top

 Post subject: Re: Has anyone done the 'Haversine' formula?Posted: Fri Feb 08, 2019 5:27 pm
 PureBasic Team

Joined: Fri Apr 25, 2003 6:14 pm
Posts: 1692
Location: Germany (Saxony, Deutscheinsiedel)
Thank you very much, Paul!

I tried to encapsulate the complete function and variables a bit more, and extended the examples.
Hopefully all is done the right way and the results are true
(e.g I'm not sure, why the latitude changes a bit in the 3rd example..., or if the +/- of the resulting lat/long are always handled correctly...)

My own test inside my project will take a bit...

[Update: 9th Feb. 2019]
Code:
Define.d Distance, Bearing

Structure geopoint
Latitude.d
Longitude.d
EndStructure
Define pos.geopoint

#MostNorthern = 0
#MostSouthern = 180
#MostEastern  = 90
#MostWestern  = 270

Procedure DistanceFromPoint(*pos.geopoint, Distance, Bearing)
; This function calculates the new geographic point in the 'Distance' (of xxx km) from the
; given current geographic point '@pos'.
; 'Bearing' determines the direction, where the new position should be calculated:
;   #MostNorthern and #MostSouthern will give a new latitude value
;   #MostEastern and #MostWestern will give a new longitude value
Protected Radius.d = 6372.795477598  ;  a fixed value
Protected OldLatitude.d = *pos\Latitude, OldLongitude.d = *pos\Longitude
Protected NewLatitude.d, NewLongitude.d

; Now we use the given values (old Lat/Long position, Distance and Bearing/Direction) for calculating the new position:

; Return the new position by saving it in the structured variable (which can be accessed in the main program):
*pos\Latitude  = NewLatitude
*pos\Longitude = NewLongitude
EndProcedure

; 1st example - going to the North:
pos\Latitude = 49
pos\Longitude = -94
Distance = 200
Debug "Old latitude/longitude: " + pos\Latitude + " / " + pos\Longitude
DistanceFromPoint(@pos, Distance, #MostNorthern) ;<-- Most northern point 200 km from lat/lon
Debug "..new latitude/longitude " + Distance + " km into North: " + pos\Latitude + " / " + pos\Longitude
Debug "----------------"

; 2nd example - going to the South:
pos\Latitude = 49
pos\Longitude = -94
Distance = 20
Debug "Old latitude/longitude: " + pos\Latitude + " / " + pos\Longitude
DistanceFromPoint(@pos, Distance, #MostSouthern) ;<-- Most southern point 20 km from lat/lon
Debug "..new latitude/longitude " + Distance + " km into South: " + pos\Latitude + " / " + pos\Longitude
Debug "----------------"

; 3rd extended example - going from Northpole (latitude = +90) to Southpole (latitude = -90) and calculate the new longitude for going 200 km to the East:
Debug "Here a more extended example: starting point moves from North to South (latitude decreases 10 degress everytime), but with constant longitude (20):"
For a = 85 To -85 Step -10
pos\Latitude = a
pos\Longitude = 20
Distance = 200
Debug "Old latitude/longitude: " + pos\Latitude + " / " + pos\Longitude
DistanceFromPoint(@pos, Distance, #MostEastern) ;<-- Most eastern point 200 km from lat/lon
Debug "..new latitude/longitude " + Distance + " km into East: " + pos\Latitude + " / " + pos\Longitude
Next
Debug "----------------"

; 4th example - going to the West:
pos\Latitude = 49
pos\Longitude = -94
Distance = 20
Debug "Old latitude/longitude: " + pos\Latitude + " / " + pos\Longitude
DistanceFromPoint(@pos, Distance, #MostWestern) ;<-- Most western point 20 km from lat/lon
Debug "..new latitude/longitude " + Distance + " km into West: " + pos\Latitude + " / " + pos\Longitude

_________________
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)

Top

 Post subject: Re: Has anyone done the 'Haversine' formula?Posted: Fri Feb 08, 2019 6:01 pm

Joined: Sat Feb 13, 2010 3:45 pm
Posts: 861
Andre wrote:
... latitude seems easy, but longitude...

If you take a closer look, you will find that calculating the latitude is probably the bigger problem.

For example, if you want to drive 2000 km to the west, you have to follow the great circle to really reach the farthest point to the west. To follow the great circle means that on the northern hemisphere you have to go north first and then south again after half of your distance. In the southern hemisphere the opposite is true, at the equator it doesn't matter.

At GPS starting time I wrote a simple navigation program (shit, I'm old). I always calculated the globe to be in the middle of the circle. So all calculations were much easier. I haven't thought it over for your case right now, but maybe this can help.

_________________
sorry for my bad english

Top

 Post subject: Re: Has anyone done the 'Haversine' formula?Posted: Fri Feb 08, 2019 8:09 pm

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 3598
Location: Berlin, Germany
A riddle:
You are somewhere on earth. You go 5 km south, then 5 km west and then 5 km north. Then you have arrived back at the place where you started.
You see a bear. What color is it?

_________________
Please excuse my flawed English. My native language is PureBasic.
Search
RSBasic's backups

Top

 Post subject: Re: Has anyone done the 'Haversine' formula?Posted: Fri Feb 08, 2019 8:42 pm

Joined: Sat Feb 13, 2010 3:45 pm
Posts: 861
Little John wrote:
You see a bear. What color is it?

I have never seen a polar bear so far north

_________________
sorry for my bad english

Top

 Post subject: Re: Has anyone done the 'Haversine' formula?Posted: Fri Feb 08, 2019 9:08 pm
 PureBasic Expert

Joined: Fri Apr 25, 2003 4:34 pm
Posts: 894
Josh wrote:
Little John wrote:
You see a bear. What color is it?

I have never seen a polar bear so far north

What about Santa ?

_________________

Top

 Post subject: Re: Has anyone done the 'Haversine' formula?Posted: Sat Feb 09, 2019 7:52 am

Joined: Thu Feb 09, 2006 11:27 pm
Posts: 2430
Tried to do a quick check, don't think, the results are okay (maybe I am mixing bears and penguins here)...

Here's the output...
Old latitude/longitude: 49 / -94
New latitude/longitude 20 km into West: 48.9996754155 / -94.4177635481
30.568590932620445

...and here's the code:
Code:
:
pos\Latitude = 49
pos\Longitude = -94
new.geopoint=pos
Distance = 20
DistanceFromPoint(@new, Distance, #MostWestern) ;<-- Most western point 20 km from lat/lon
Debug "Old latitude/longitude: " + pos\Latitude + " / " + pos\Longitude
Debug "New latitude/longitude " + Distance + " km into West: " + new\Latitude + " / " + new\Longitude
Debug distVincenty2(pos\Latitude,pos\Longitude,new\Latitude,new\Longitude)

Top

 Post subject: Re: Has anyone done the 'Haversine' formula?Posted: Sat Feb 09, 2019 8:21 am
 PureBasic Expert

Joined: Fri Apr 25, 2003 4:34 pm
Posts: 894
When Andre converted my snippet, he missed something...

Code:
^^^

He renamed all the 'lat1' to 'OldLongitude' except this one

_________________

Top

 Post subject: Re: Has anyone done the 'Haversine' formula?Posted: Sat Feb 09, 2019 2:04 pm
 PureBasic Team

Joined: Fri Apr 25, 2003 6:14 pm
Posts: 1692
Location: Germany (Saxony, Deutscheinsiedel)
Thanks Paul, for pointing this out

I've updated my code snippet above, including a slightly modified 3rd example.

But I still think too, that moving to the West (4th example) shouldn't change the latitude, when it doesn't for moving to the East (2nd example)!?

_________________
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)

Top

 Post subject: Re: Has anyone done the 'Haversine' formula?Posted: Sat Feb 09, 2019 4:55 pm
 PureBasic Expert

Joined: Fri Apr 25, 2003 4:34 pm
Posts: 894
This is a good read...
https://www.movable-type.co.uk/scripts/latlong.html

Explains why final bearing differs from the initial heading by varying degrees according to distance and latitude.

(calculations would probably be much easier if everyone would just accept the 'flat earth' theory )

_________________

Top

 Post subject: Re: Has anyone done the 'Haversine' formula?Posted: Sun Feb 10, 2019 10:50 pm
 PureBasic Team

Joined: Fri Apr 25, 2003 6:14 pm
Posts: 1692
Location: Germany (Saxony, Deutscheinsiedel)
Paul wrote:
This is a good read...
https://www.movable-type.co.uk/scripts/latlong.html

...

(calculations would probably be much easier if everyone would just accept the 'flat earth' theory )

Thanks for the link, Paul.

But like other links too, this math section is too high for me....
So I'm someone for the 'flat earth'....

I will post again, when I can report more about testing the code in my project (will take a while).

_________________
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 2 of 2 [ 27 posts ] Go to page Previous  1, 2

 All times are UTC + 1 hour

#### Who is online

Users browsing this forum: No registered users and 20 guests

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forum

Search for:
 Jump to:  Select a forum ------------------ PureBasic    Coding Questions    Game Programming    3D Programming    Assembly Programming    The PureBasic Editor    The PureBasic Form Designer    General Discussion    Feature Requests and Wishlists    Tricks 'n' Tips Bug Reports    Bugs - Windows    Bugs - Linux    Bugs - Mac OSX    Bugs - Documentation OS Specific    AmigaOS    Linux    Windows    Mac OSX Miscellaneous    Announcement    Off Topic Showcase    Applications - Feedback and Discussion    PureFORM & JaPBe    TailBite