tungSfer

haversine

Jan 16th, 2022 (edited)
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.55 KB | None | 0 0
  1. from math import radians, cos, sin, asin, sqrt
  2.  
  3. def haversine(lat1, lon1, lat2, lon2):
  4.  
  5.       R = 6371
  6.  
  7.       dLat = (lat2 - lat1)
  8.       dLon = (lon2 - lon1)
  9.       lat1 = (lat1)
  10.       lat2 = (lat2)
  11.  
  12.       a = sin(dLat/2)**2 + cos(lat1)*cos(lat2)*sin(dLon/2)**2
  13.       c = 2*asin(sqrt(a))
  14.  
  15.       return R * c
  16.  
  17. # Usage
  18. lon1, lat1 = [float(i) for i in input().split()] # 105.96 10.21
  19. lon2, lat2 = [float(i) for i in input().split()] # 107.17 16.79
  20.  
  21. print("{:.2f}".format(haversine(lat1, lon1, lat2, lon2)))
  22.  
  23. '''
  24. 105.96 10.21
  25. 107.17 16.79
  26. '''
Add Comment
Please, Sign In to add comment