Earth distance calculation: URI - 1721

Asked

Viewed 706 times

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()
"""
  • 2

    See: https://answall.com/a/17444

1 answer

4

First the radius of the earth’s circumference in KM is of 6371 and not of 6378 as you put it in the example.

To calculate the distance between two points using latitude and longitude you can use the following formula (based on the Haversine formula)

from math import cos, asin, sqrt
def distance(lat1, lon1, lat2, lon2):
    p = 0.017453292519943295     #Pi/180
    a = 0.5 - cos((lat2 - lat1) * p)/2 + cos(lat1 * p) * cos(lat2 * p) * (1 - cos((lon2 - lon1) * p)) / 2
    return 12742 * asin(sqrt(a)) #2*R*asin...

According to the question presented in the link of your question would be insert lat1 and lon1 the coordinates of Alice and lat2 and lon2 Bob’s coordinates... you can divide the result by 2 to find the average distance (the middle of the way) between Alice and Bob.

  • The radius is wrong in the description of the same problem. I didn’t understand how exactly you use the function to generate the output, which points to use.

  • 1

    lat1 and lon1 would equal p and q of Alice (latitude, longitude) while lat2 and lon2 would be Bob’s latitude and longitude. You don’t need to add r (radius) because it is already implicit in the return calculation as I left in the final comment # 2 * R * asin..., you can divide by 2 and round the return of this function to reach the distance of the "midway" between Alice and Bob.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.