In the previous article, we have discussed Python Program to Find Armstrong Number in an Interval
Euclidean distance :
The Euclidean distance between any two points in two-dimensional or three-dimensional space is used to calculate the length of a segment connecting the two points. It is also known simply as representing the distance between two points.
Formula to Compute Euclidean distance :
Let A(x1,y1) ,B(x2, y2) are the two points in 2-dimensional plane
Euclidean distance = √[ (x – x)2 + (y – y)2]
Math Module :
Python’s math module is a built-in module. By importing this module, we can perform mathematical computations.
Numerous mathematical operations like ceil( ), floor( ), factorial( ), sqrt(), mod( ),value of pi ,…..etc .can be computed with the help of math module.
sqrt() function :
To compute the square root of any expression in Python, use the sqrt() function, which is built into the language.
split() function :
The split() function in Python is used to accept multiple inputs in the same line.
Examples:
Example1:
Input:
Given First point values P(x1,y1) : 6 , 7 Given second point values Q(x2,y2) : 5, 4
Output:
The Euclidean Distance between the above given two points 'PQ' = 3.1622776601683795
Example 2:
Input:
Given First point values P(x1,y1) : 4, 5 Given second point values Q(x2,y2) : 5, 6
Output:
The Euclidean Distance is between the above given two points PQ = 1.4142135623730951
Program to Compute Euclidean Distance
Below are the ways to Compute Euclidean Distance.
Method #1: Using math Module (Static input)
Approach:
- Import math module using the import keyword.
- Give the first point values as static input say P(x1,y1), and Store it in a variable.
- Give the second point values as static input say Q(x2,y2), and Store it in another variable.
- Calculate the Euclidean distance between the given two points with reference to the mathematical formula using built-in math.sqrt() function.
- And Store it in another variable.
- Print the Euclidean distance between the above given two points ‘PQ’.
- The Exit of the program.
Below is the implementation:
# Import math module using the import keyword. import math # Give the first point values as static input say P(x1,y1) and Store it in a variable. x_1, y_1 = 6, 7 # Give the second point values as static input say Q(x2,y2) and Store it in another #variable. x_2, y_2 = 5, 4 # Calculate the Euclidean distance between given two points with reference to the # mathematical formula using built-in math.sqrt() function. # And Store it in another variable. Eucli_dist = math.sqrt((x_2-x_1)**2 + (y_2-y_1)**2) # Print the Euclidean distance between the above given two points 'PQ'. print("The Euclidean Distance between the above given two points 'PQ' = " + str(Eucli_dist))
Output:
The Euclidean Distance between the above given two points 'PQ' = 3.1622776601683795
Method #2: Using math Module (User input)
Approach:
- Import math module using the import keyword.
- Give the first point values as User input say P(x1,y1) on a single line using built-in map(), split() functions, and Store it in a variable.
- Give the second point values as User input say Q(x2,y2) on a single line using built-in map(), split() functions, and Store it in a variable.
- Calculate the Euclidean distance between the given two points with reference to the mathematical formula using built-in math.sqrt() function.
- And Store it in another variable.
- Print the Euclidean distance between the above given two points ‘PQ’.
- The Exit of the program.
Below is the implementation:
# Import math module using the import keyword. import math # Give the first point values as User input say P(x1,y1) on a single line using built-in # map(),split() functions and Store it in a variable. print("Given First point values P(x1,y1) : ") x_1, y_1 = map(int, input().split()) # Give the second point values as User input say Q(x2,y2) on a single line using #built-in map(),split() functions and Store it in another variable. print("Given second point values Q(x2,y2) : ") x_2, y_2 = map(int, input().split()) # Calculate the Euclidean distance between given two points with reference to the # mathematical formula using built-in math.sqrt() function. # And Store it in another variable. Eucli_dist = math.sqrt((x_2-x_1)**2 + (y_2-y_1)**2) # Print the Euclidean distance between the above given two points 'PQ'. print("The Euclidean Distance is between the above given two points PQ = " + str(Eucli_dist))
Output:
Given First point values P(x1,y1) : 4 5 Given second point values Q(x2,y2) : 5 6 The Euclidean Distance is between the above given two points PQ = 1.4142135623730951
Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.