In: Computer Science
(Great Circle Distance) Write a program great_circle.py that takes four floats x 1 , y 1 , x 2 , and y 2 (latitude and longitude in degrees of two points on Earth) as command-line arguments and writes the great-circle distance (in km) between them, given by the equation
d = 111 arccos(sin(x 1 ) sin(x 2 ) + cos(x 1 ) cos(x 2 ) cos(y 1 − y 2 )).
$python3 great_circle .py 48.87 -2.33 37.8 -122.4
8701.389543238289
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
#code
import sys
from math import *
#checking if extra 4 command line arguments are given (first
one is script name)
if len(sys.argv)!=5:
#printing usage and quitting
print("Usage: great_circle.py
<x1> <y1> <x2> <y2>")
exit(0)
#converting each argument to float and converting to radians by
multiplying with pi/180
x1=float(sys.argv[1])*(pi/180)
y1=float(sys.argv[2])*(pi/180)
x2=float(sys.argv[3])*(pi/180)
y2=float(sys.argv[4])*(pi/180)
'''
The equation you provided was not fully correct, or not getting
correct results, so I googled
and found the correct equation. instead of multiplying with 111,
you should multiply with
6371 (radius of earth in km), and each angle provided is in
radians
'''
distance=6371*acos(sin(x1)*sin(x2) +
cos(x1)*cos(x2)*cos(y1-y2))
#displaying the distance.
print(distance)
#for an input of 48.87 -2.33 37.8 -122.4, the distance is
8716.67001 and not 8701.389543238289
#as you have shown in the example output. If you are having doubts,
try some online great distance
#calculator and verify it yourself.
#input and output
> great_circle .py 48.87 -2.33 37.8 -122.4
> 8716.670017712719