mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-10-06 05:39:30 +00:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
25a03cf503
commit
5c9d6709ea
|
@ -1,18 +1,19 @@
|
|||
from math import asin, cos, radians, sin, sqrt
|
||||
|
||||
|
||||
def haversine_distance(lat1: float, lon1: float, lat2: float, lon2: float) -> float:
|
||||
"""
|
||||
Calculate the great-circle distance between two points
|
||||
on the Earth specified by latitude and longitude using
|
||||
the Haversine formula.
|
||||
|
||||
|
||||
Args:
|
||||
lat1, lon1: Latitude and longitude of point 1 in decimal degrees.
|
||||
lat2, lon2: Latitude and longitude of point 2 in decimal degrees.
|
||||
|
||||
|
||||
Returns:
|
||||
Distance between the two points in meters.
|
||||
|
||||
|
||||
>>> from collections import namedtuple
|
||||
>>> point_2d = namedtuple("point_2d", "lat lon")
|
||||
>>> SAN_FRANCISCO = point_2d(37.774856, -122.424227)
|
||||
|
@ -21,7 +22,7 @@ def haversine_distance(lat1: float, lon1: float, lat2: float, lon2: float) -> fl
|
|||
'254,033 meters'
|
||||
"""
|
||||
|
||||
R = 6378137 #earth radius (meters)
|
||||
R = 6378137 # earth radius (meters)
|
||||
|
||||
lat1_rad = radians(lat1)
|
||||
lat2_rad = radians(lat2)
|
||||
|
@ -29,12 +30,17 @@ def haversine_distance(lat1: float, lon1: float, lat2: float, lon2: float) -> fl
|
|||
delta_lon = radians(lon2 - lon1)
|
||||
|
||||
# Haversine formula
|
||||
a = sin(delta_lat / 2)**2 + cos(lat1_rad) * cos(lat2_rad) * sin(delta_lon / 2)**2
|
||||
a = (
|
||||
sin(delta_lat / 2) ** 2
|
||||
+ cos(lat1_rad) * cos(lat2_rad) * sin(delta_lon / 2) ** 2
|
||||
)
|
||||
c = 2 * asin(sqrt(a))
|
||||
|
||||
#Great-Circle Distance
|
||||
|
||||
# Great-Circle Distance
|
||||
return R * c
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import doctest
|
||||
|
||||
doctest.testmod()
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
|
||||
# if __name__ == "__main__":
|
||||
# import doctest
|
||||
# doctest.testmod()
|
||||
|
|
Loading…
Reference in New Issue
Block a user