Haversine formula that includes an altitude parameter?

3.1k Views Asked by At

I have this Python function that computes the great-circle distance between two points, but I want to modify it so that a third parameter, altitude, can be incorporated into the Haversine formula.

from math import cos, sin, atan2, radians, sqrt

def findDistance(p, p2):
  R = 3959
  lat1 = radians(p[0])
  lon1 = radians(p[1])
  lat2 = radians(p2[0])
  lon2 = radians(p2[1])
  dlat = lat2 - lat1
  dlon = lon2 - lon1
  a = (sin(dlat/2))**2 + cos(lat1) * cos(lat2) * (sin(dlon/2))**2
  c = 2 * atan2(sqrt(a), sqrt(1-a))
  return R * c

p and p2 are of the form (latitude, longitude, altitude)

Is there a way to modify the Haversine formula to take into account an altitude difference between two points?