Calculating Distance Between Two Geodetic Points

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
HeXaDeCiMaToR
Posts: 155
Joined: 08 Feb 2021, 12:42

Calculating Distance Between Two Geodetic Points

11 May 2021, 01:55

So, to be honest, I Jerry Rigged this from a post in JavaScript. I'm not even sure it will work in AHK.

With that being said, I'm getting an Error Message in the line that holds "c := 2 * ATan(Sqrt(a), Sqrt(1-a))" saying "==> Too many parameters passed to function."

Anybody know how to reconcile this error, and still have it work?

Website Reference: https://stackoverflow.com/questions/365826/calculate-distance-between-2-gps-coordinates

Code: Select all

degreesToRadians(degrees)
{
	return degrees * pi/180
}

distanceInMilesBetweenEarthCoordinates(lat1, lon1, lat2, lon2)
{
	earthRadiusMiles=3959
	
	dLat := degreesToRadians(lat2-lat1)
	dLon := degreesToRadians(lon2-lon1)
	
	lat1 := degreesToRadians(lat1)
	lat2 := degreesToRadians(lat2)
	
	a := Sin(dLat/2) * Sin(dLat/2) + Sin(dLon2) * Sin(dLon2) * Cos(lat1) * Cos(lat2)
	c := 2 * ATan(Sqrt(a), Sqrt(1-a))
	return earthRadiusMiles * c
}
HeXaDeCiMaToR
Posts: 155
Joined: 08 Feb 2021, 12:42

Re: Calculating Distance Between Two Geodetic Points

11 May 2021, 02:01

So...... I just moved what was in ATan() into a var "b" and then put "b" in Atan()

Like so...

Code: Select all

degreesToRadians(degrees)
{
	return degrees * pi/180
}

distanceInMilesBetweenEarthCoordinates(lat1, lon1, lat2, lon2)
{
	earthRadiusMiles=3959
	
	dLat := degreesToRadians(lat2-lat1)
	dLon := degreesToRadians(lon2-lon1)
	
	lat1 := degreesToRadians(lat1)
	lat2 := degreesToRadians(lat2)
	
	a := Sin(dLat/2) * Sin(dLat/2) + Sin(dLon2) * Sin(dLon2) * Cos(lat1) * Cos(lat2)
	b := Sqrt(a), Sqrt(1-a)
	c := 2 * ATan(b)
	return earthRadiusMiles * c
}
HeXaDeCiMaToR
Posts: 155
Joined: 08 Feb 2021, 12:42

Re: Calculating Distance Between Two Geodetic Points

11 May 2021, 02:37

Update: This is not working exactly right...

This is a "As A Crow" calculation and it's calculating Houston to Beverly Hills at 584 miles. I'll update this post when it's working correctly.
Rohwedder
Posts: 7623
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Calculating Distance Between Two Geodetic Points

11 May 2021, 03:30

Hallo,
pi is not defined in Autohotkey.
Try:

Code: Select all

degreesToRadians(degrees)
{
	return degrees * atan(1)/45
}
and

Code: Select all

ATan2(y, x)
{ ;MSVCRT.DLL is the C standard library for the Visual C++ (MSVC) compiler
	return DllCall("msvcrt\atan2", "Double", y, "Double", x, "Cdecl Double")
}
HeXaDeCiMaToR
Posts: 155
Joined: 08 Feb 2021, 12:42

Re: Calculating Distance Between Two Geodetic Points

11 May 2021, 04:24

@Rohwedder I like where your head is at. I think I should add in my vars...

I simplified my exponents and I had already added in an Atan formula for PI, but I like your method better as it's shown in the function and not hard coded global.

Latitude: 29.863
Longitude: -95.198
City: Houston
State: Texas


Latitude: 34.09
Longitude: -118.406
City: Beverly Hills
State: California


Code: Select all

degreesToRadians(degrees)
{
	return degrees * atan(1)/45
}

distanceInMilesBetweenEarthCoordinates(lat1, lon1, lat2, lon2)
{
	earthRadiusMiles=3959
	
	dLat := degreesToRadians(lat2-lat1)
	dLon := degreesToRadians(lon2-lon1)
	
	lat1 := degreesToRadians(lat1)
	lat2 := degreesToRadians(lat2)

	a := Sin(dLat/2)**2 + Cos(lat1)  * Cos(lat2) * Sin(dLon2)**2
	;~ a := Sin(dLat/2) * Sin(dLat/2) + Sin(dLon2) * Sin(dLon2) * Cos(lat1) * Cos(lat2)
	;~ b := 
	;~ MsgBox % a
	c := 2 * ATan2(Sqrt(a), Sqrt(1-a))
	;~ MsgBox % c
	return earthRadiusMiles * c
}

ATan2(vNumY, vNumX)
{
	local
	return DllCall("msvcrt\atan2", "Double",vNumY, "Double",vNumX, "Cdecl Double")
}
Last edited by HeXaDeCiMaToR on 11 May 2021, 04:29, edited 2 times in total.
HeXaDeCiMaToR
Posts: 155
Joined: 08 Feb 2021, 12:42

Re: Calculating Distance Between Two Geodetic Points

11 May 2021, 04:26

Something is DEFINITELY wrong! lol
image.png
image.png (10.58 KiB) Viewed 424 times
Rohwedder
Posts: 7623
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Calculating Distance Between Two Geodetic Points

11 May 2021, 04:41

Hallo,
I use the source and km:

Code: Select all

MsgBox,% distanceInKmBetweenEarthCoordinates(51.5, 0, 38.8, -77.1)
; From London to Arlington 5918.185064088764


degreesToRadians(degrees)
{
	return degrees * atan(1)/45
}

distanceInKmBetweenEarthCoordinates(lat1, lon1, lat2, lon2)
{
  earthRadiusKm = 6371

  dLat := degreesToRadians(lat2-lat1)
  dLon := degreesToRadians(lon2-lon1)

  lat1 := degreesToRadians(lat1)
  lat2 := degreesToRadians(lat2)

  a := sin(dLat/2) * sin(dLat/2)
		+ sin(dLon/2) * sin(dLon/2) * cos(lat1) * cos(lat2)
  c := 2 * atan2(sqrt(a), sqrt(1-a)) 
  return earthRadiusKm * c
}


ATan2(vNumY, vNumX)
{
	local
	return DllCall("msvcrt\atan2", "Double",vNumY, "Double",vNumX, "Cdecl Double")
}
I get 5918.185064 km
resp. (matching your account name) 0x171E.2F605B km

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: doodles333 and 387 guests