2
I would like to know how to calculate the distance between points of the Earth given latitude and longitude. What formulas to use to convert spherical coordinates to cartesian and vice versa.
I need to help solve an URI problem: https://www.urionlinejudge.com.br/judge/pt/problems/view/1721
#URI - 1721 - Equidistance
import math
def sphericalDistence(p, q, r):
#p e q sao pares de coordenadas esfericas (latitude, longitude)
#r eh o raio da esfera
return math.acos(math.sin(p[0]) * math.sin(q[0])+ math.cos(p[0]) * math.cos(q[0]) * math.cos(p[1]-q[1]))*r
r = 6378
p = {}
S, La, Lo = input().split()
while S!='#':
try:
p[S] = (math.radians(float(La)), math.radians(float(Lo)))#remember of the convert to radians
#print('S=', S, ' La Lo = ', p[S])
S, La, Lo = input().split()
except:
break
#URI
A, B, M = input().split()
while A!='#':
if not(A in p.keys() and B in p.keys() and M in p.keys()):
d = '?'
else:
p1 = (math.radians(p[M][0]),math.radians(p[M][1]))
p2 = (math.radians((p[A][0]+p[B][0])/2),math.radians((p[A][1]+p[B][1])/2))
d = str(sphericalDistence(p1, p2, r))
#print('d A M = ',(sphericalDistence(p[A], p[M],r)))
#print('d B M = ',(sphericalDistence(p[B], p[M],r)))
print('%s is %s km off %s/%s equidistance' %(M, d, A, B))
A, B, M = input().split()
"""
#UVA
A, B= input().split()
while A!='#':
print((sphericalDistence(p[A], p[B],r)))
A, B = input().split()
"""
See: https://answall.com/a/17444
– bfavaretto