Skip to content

Commit 48575ad

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 4c6132f commit 48575ad

1 file changed

Lines changed: 16 additions & 11 deletions

File tree

haversine_calculation/haversine.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
11
import math
22

3+
34
def haversine(lat1, lon1, lat2, lon2):
45
"""
5-
Calculate the great circle distance in kilometers between two points
6+
Calculate the great circle distance in kilometers between two points
67
on the earth (specified in decimal degrees)
78
"""
8-
# Convert decimal degrees to radians
9+
# Convert decimal degrees to radians
910
lat1, lon1, lat2, lon2 = map(math.radians, [lat1, lon1, lat2, lon2])
10-
11-
# Haversine formula
12-
dlat = lat2 - lat1
13-
dlon = lon2 - lon1
14-
a = math.sin(dlat/2)**2 + math.cos(lat1) * math.cos(lat2) * math.sin(dlon/2)**2
15-
c = 2 * math.asin(math.sqrt(a))
16-
r = 6371 # Radius of earth in kilometers. Use 3956 for miles.
11+
12+
# Haversine formula
13+
dlat = lat2 - lat1
14+
dlon = lon2 - lon1
15+
a = (
16+
math.sin(dlat / 2) ** 2
17+
+ math.cos(lat1) * math.cos(lat2) * math.sin(dlon / 2) ** 2
18+
)
19+
c = 2 * math.asin(math.sqrt(a))
20+
r = 6371 # Radius of earth in kilometers. Use 3956 for miles.
1721
return c * r
1822

23+
1924
# Example usage
2025
if __name__ == "__main__":
2126
# Coordinates of New York City and London
2227
lat1, lon1 = 40.7128, -74.0060 # New York City
23-
lat2, lon2 = 51.5074, -0.1278 # London
24-
28+
lat2, lon2 = 51.5074, -0.1278 # London
29+
2530
distance = haversine(lat1, lon1, lat2, lon2)
2631
print(f"Distance between New York City and London: {distance:.2f} km")

0 commit comments

Comments
 (0)