SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Y Daniel Lang
Chapter:
Functions Strings And Objects
Exercise:
Programming Excercises
Question:2 | ISBN:978013274719 | Edition: 6

Question

(Geometry: great circle distance) The great circle distance is the distance between two points on the surface of a sphere. Let (x1, y1) and (x2, y2) be the geographical latitude and longitude of two points. The great circle distance between the two points can be computed using the following formula:

d = radius * arccos(sin(x1) * sin(x2) + cos(x1) * cos(x2) * cos(y1 - y2))

Write a program that prompts the user to enter the latitude and longitude of two points on the earth in degrees and displays its great circle distance. The average earth radius is 6,371.01 km. Note that you need to convert the degrees into radians using the math.radians function since the Python trigonometric functions use radians. The latitude and longitude degrees in the formula are for north and west Use negative to indicate south and east degrees. Here is a sample run:
 

Enter point 1 (latitude and longitude) in degrees:39.55, -116.25
Enter point 2 (latitude and longitude) in degrees:41.5, 87.37
The distance between the two points is 10691.79183231593 km

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

#importing math module
import math
#Taking input as  latitude and longitude in degrees
print("Enter the side:")
print("Enter point 1 (latitude and longitude) in degrees: ")
x1 = math.radians(float(input()))
y1 = math.radians(float(input()))
print("Enter point 2 (latitude and longitude) in degrees: ")
x2 = math.radians(float(input()))
y2 = math.radians(float(input()))
#formula for the distance between two points
distance = 6371.01 * math.acos(math.sin(x1) * math.sin(x2)
    + math.cos(x1) * math.cos(x2) * math.cos(y1 - y2))
#Display the result
print("The distance between the two points is ",distance,"km")

Output:

Enter the side:
Enter point 1 (latitude and longitude) in degrees: 
39.55
-116.25
Enter point 2 (latitude and longitude) in degrees: 
41.5
87.37
The distance between the two points is  10691.79183231593 km

0 0

Discussions

Post the discussion to improve the above solution.