Program To Calculate the Angle Between Two Vectors

Python Program To Calculate the Angle Between Two Vectors

In the previous article, we have discussed Python Program to Find the Sine Series for the Given range
Mathematical Way :

The angle between two vectors can be calculated using the formula, which states that the angle cos of two vectors is equal to the dot product of two vectors divided by the dot product of the mod of two vectors.

cosθ = X.Y/|X|.|Y|  =>θ =  cos^-1 X.Y/|X|.|Y|

Example:

Let  X={5,6} and y={4,3} are two vectors.

The angle between given two vectors = X.Y=  5*4+6*3 = 38

|X| = √ 5^2 +6^2 = 7.8102

|Y| = √ 4^2 +3^2 = 5

cosθ = 38/7.8102*5 = 0.973

θ= 13.324°

In this, we use the ‘math’ module to perform some complex calculations for us, such as square root, cos inverse, and degree, by calling the functions sqrt(), acos(), and degrees ().

Given two vectors, and the task is to calculate the angle between the given two vectors.

Examples:

Example1:

Input: 

Given x1, y1, x2, y2 = 5, 6, 4, 3

Output:

The Cos angle between given two vectors = 0.9730802874900094
The angle in degree between given two vectors =  13.324531261890783

Example 2:

Input:

Given x1, y1, x2, y2  =  7, 3, 2, 1

Output:

The Cos angle between given two vectors = 0.9982743731749958
The angle in degree between given two vectors = 3.36646066342994

Program To Calculate the Angle Between Two Vectors

Below are the ways to Calculate the Angle Between Two Vectors.

Method #1: Using Mathematical Formula (Static input)

Approach:

  • Import math module using the import keyword.
  • Give four variables as static input and store them in four different variables.
  • Calculate the dot product of the given two vectors using the above mathematical formula and store them in another variable.
  • Calculate the Mod of given two vectors using the above mathematical formula and store them in another variable.
  • Find the cosine angle between the given two vectors using the above mathematical formula and store them in another variable.
  • Print the cosine angle between the given two vectors.
  • Calculate the angle in degrees using built-in math.degrees(), math.acos() functions and store it in a variable .
  • Print the angle ‘θ’ between the given two vectors.
  • The Exit of the program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give four variables as static input and store them in four different variables.
x1, y1, x2, y2 = 5, 6, 4, 3


def angle_between_Gvnvectors(x1, y1, x2, y2):
    # Calculate the dot product of given two vectors using above mathematical formula
    # and store them in another variable.

    dot_Prodct = x1*x2 + y1*y2
 # Calculate the Mod of given two vectors using above mathematical formula and
# store them in another variable
    mod_Of_Vectr1 = math.sqrt(x1*x1 + y1*y1)*math.sqrt(x2*x2 + y2*y2)
 # Find the cosine angle between given two vectors  using above mathematical formula
# and store them in another variable.
    cosine_angle = dot_Prodct/mod_Of_Vectr1
 # Print the cosine angle between given two vectors.
    print(" The Cos angle between given two vectors =", cosine_angle)
# Calculate the angle in degrees using built-in math.degrees(), math.acos() functions
# and store it in a variable .
    angl_in_Degres = math.degrees(math.acos(cosine_angle))
# Print the angle 'θ' between given two vectors.
    print("The angle in degree between given two vectors = ", angl_in_Degres)


angle_between_Gvnvectors(x1, y1, x2, y2)

Output:

The Cos angle between given two vectors = 0.9730802874900094
The angle in degree between given two vectors =  13.324531261890783

Method #2: Using Mathematical Formula (User input)

Approach:

  • Import math module using the import keyword.
  • Give four variables as user input using map(),split() , int(input()) functions and store them in four different variables.
  • Calculate the dot product of the given two vectors using the above mathematical formula and store them in another variable.
  • Calculate the Mod of given two vectors using the above mathematical formula and store them in another variable.
  • Find the cosine angle between the given two vectors using the above mathematical formula and store them in another variable.
  • Print the cosine angle between the given two vectors.
  • Calculate the angle in degrees using built-in math.degrees(), math.acos() functions and store it in a variable .
  • Print the angle ‘θ’ between the given two vectors.
  • The Exit of the program.

Below is the implementation of the above approach:

# Import math module using the import keyword.
import math
# Give four variables as user input using map(),split() ,int(input())functions and 
#store them in four different variables.
x1, y1, x2, y2 = map(int,input("Enter four random numbers = ").split())
def angle_between_Gvnvectors(x1, y1, x2, y2):
    # Calculate the dot product of given two vectors using above mathematical formula
    # and store them in another variable.

    dot_Prodct = x1*x2 + y1*y2
 # Calculate the Mod of given two vectors using above mathematical formula and
# store them in another variable
    mod_Of_Vectr1 = math.sqrt(x1*x1 + y1*y1)*math.sqrt(x2*x2 + y2*y2)
 # Find the cosine angle between given two vectors  using above mathematical formula
# and store them in another variable.
    cosine_angle = dot_Prodct/mod_Of_Vectr1
 # Print the cosine angle between given two vectors.
    print(" The Cos angle between given two vectors =", cosine_angle)
# Calculate the angle in degrees using built-in math.degrees(), math.acos() functions
# and store it in a variable .
    angl_in_Degres = math.degrees(math.acos(cosine_angle))
# Print the angle 'θ' between given two vectors.
    print("The angle in degree between given two vectors = ", angl_in_Degres)

angle_between_Gvnvectors(x1, y1, x2, y2)

Output:

Enter four random numbers = 7 3 2 1
The Cos angle between given two vectors = 0.9982743731749958
The angle in degree between given two vectors = 3.36646066342994

Here we calculated the angle between the given vectors.

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.