Open In App

Program to calculate angle between two N-Dimensional vectors

Last Updated : 26 Apr, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Given an array arr[] consisting of magnitudes of two N-Dimensional vectors A and B, the task is to find the angle between the two vectors.

Examples:

Input: arr[] = {-0.5, -2, 1}, brr[] = {-1, -1, -0.3}  
Output: 0.845289
Explanation:
Placing the values in the formula cos\theta=\frac{\vec{a}.\vec{b}}{|\vec{a}||\vec{b}|}, the required result is obtained.

Input: arr[] = {1, -2, 3}, brr[] = {2, 3, -1} 
Output: -0.5

Approach: The idea is based on the mathematical formula of finding the dot product of two vectors and dividing it by the product of the magnitude of vectors A, B.

Formula:

Considering the two vectors to be separated by angle ?. the dot product of the two vectors is given by the equation:
\vec{a}.\vec{b} = |\vec{a}||\vec{b}|.cos\theta

Therefore, cos\theta=\frac{\vec{a}.\vec{b}}{|\vec{a}||\vec{b}|}

Below is the implementation of the above approach:

C++

// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the magnitude
// of the given vector
double magnitude(double arr[], int N)
{
    // Stores the final magnitude
    double magnitude = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++)
        magnitude += arr[i] * arr[i];
 
    // Return square root of magnitude
    return sqrt(magnitude);
}
 
// Function to find the dot
// product of two vectors
double dotProduct(double arr[],
                  double brr[], int N)
{
    // Stores dot product
    double product = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++)
        product = product + arr[i] * brr[i];
 
    // Return the product
    return product;
}
 
void angleBetweenVectors(double arr[],
                         double brr[], int N)
{
    // Stores dot product of two vectors
    double dotProductOfVectors
        = dotProduct(arr, brr, N);
 
    // Stores magnitude of vector A
    double magnitudeOfA
        = magnitude(arr, N);
 
    // Stores magnitude of vector B
    double magnitudeOfB
        = magnitude(brr, N);
 
    // Stores angle between given vectors
    double angle = dotProductOfVectors
                   / (magnitudeOfA * magnitudeOfB);
 
    // Print the angle
    cout << angle;
}
 
// Driver Code
int main()
{
    // Given magnitude arrays
    double arr[] = { -0.5, -2, 1 };
    double brr[] = { -1, -1, 0.3 };
 
    // Size of the array
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function call to find the
    // angle between two vectors
    angleBetweenVectors(arr, brr, N);
 
    return 0;
}

                    

Java

// Java program for the above approach
class GFG{
 
// Function to find the magnitude
// of the given vector
static double magnitude(double arr[], int N)
{
     
    // Stores the final magnitude
    double magnitude = 0;
 
    // Traverse the array
    for(int i = 0; i < N; i++)
        magnitude += arr[i] * arr[i];
 
    // Return square root of magnitude
    return Math.sqrt(magnitude);
}
 
// Function to find the dot
// product of two vectors
static double dotProduct(double[] arr,
                         double[] brr, int N)
{
     
    // Stores dot product
    double product = 0;
 
    // Traverse the array
    for(int i = 0; i < N; i++)
        product = product + arr[i] * brr[i];
 
    // Return the product
    return product;
}
 
static void angleBetweenVectors(double[] arr,
                                double[] brr, int N)
{
     
    // Stores dot product of two vectors
    double dotProductOfVectors = dotProduct(arr, brr, N);
 
    // Stores magnitude of vector A
    double magnitudeOfA = magnitude(arr, N);
 
    // Stores magnitude of vector B
    double magnitudeOfB = magnitude(brr, N);
 
    // Stores angle between given vectors
    double angle = dotProductOfVectors /
                   (magnitudeOfA * magnitudeOfB);
 
    // Print the angle
    System.out.println(angle);
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given magnitude arrays
    double[] arr = { -0.5, -2, 1 };
    double[] brr = { -1, -1, 0.3 };
 
    // Size of the array
    int N = arr.length;
 
    // Function call to find the
    // angle between two vectors
    angleBetweenVectors(arr, brr, N);
}
}
 
// This code is contributed by user_qa7r

                    

Python3

# Python3 program for the above approach
import math
 
# Function to find the magnitude
# of the given vector
def magnitude(arr, N):
 
    # Stores the final magnitude
    magnitude = 0
 
    # Traverse the array
    for i in range(N):
        magnitude += arr[i] * arr[i]
 
    # Return square root of magnitude
    return math.sqrt(magnitude)
 
# Function to find the dot
# product of two vectors
 
 
def dotProduct(arr, brr, N):
 
    # Stores dot product
    product = 0
 
    # Traverse the array
    for i in range(N):
        product = product + arr[i] * brr[i]
 
    # Return the product
    return product
 
 
def angleBetweenVectors(arr, brr, N):
 
    # Stores dot product of two vectors
    dotProductOfVectors = dotProduct(arr, brr, N)
 
    # Stores magnitude of vector A
    magnitudeOfA = magnitude(arr, N)
 
    # Stores magnitude of vector B
    magnitudeOfB = magnitude(brr, N)
 
    # Stores angle between given vectors
    angle = (dotProductOfVectors
             / (magnitudeOfA * magnitudeOfB))
 
    # Print the angle
    print('%.5f'%angle)
 
# Driver Code
if __name__ == "__main__":
 
    # Given magnitude arrays
    arr = [-0.5, -2, 1]
    brr = [-1, -1, 0.3]
 
    # Size of the array
    N = len(arr)
 
    # Function call to find the
    # angle between two vectors
    angleBetweenVectors(arr, brr, N)
 
    # This code is contributed by ukasp.

                    

C#

// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
  
// Function to find the magnitude
// of the given vector
static double magnitude(double []arr, int N)
{
     
    // Stores the final magnitude
    double magnitude = 0;
 
    // Traverse the array
    for(int i = 0; i < N; i++)
        magnitude += arr[i] * arr[i];
 
    // Return square root of magnitude
    return Math.Sqrt(magnitude);
}
 
// Function to find the dot
// product of two vectors
static double dotProduct(double []arr,
                         double []brr, int N)
{
     
    // Stores dot product
    double product = 0;
 
    // Traverse the array
    for(int i = 0; i < N; i++)
        product = product + arr[i] * brr[i];
 
    // Return the product
    return product;
}
 
static void angleBetweenVectors(double []arr,
                                double []brr, int N)
{
     
    // Stores dot product of two vectors
    double dotProductOfVectors = dotProduct(arr, brr, N);
 
    // Stores magnitude of vector A
    double magnitudeOfA = magnitude(arr, N);
 
    // Stores magnitude of vector B
    double magnitudeOfB = magnitude(brr, N);
 
    // Stores angle between given vectors
    double angle = dotProductOfVectors /
     (magnitudeOfA * magnitudeOfB);
 
    // Print the angle
    Console.Write(angle);
}
 
// Driver Code
public static void Main()
{
     
    // Given magnitude arrays
    double []arr = { -0.5, -2, 1 };
    double []brr = { -1, -1, 0.3 };
 
    // Size of the array
    int N = arr.Length;
 
    // Function call to find the
    // angle between two vectors
    angleBetweenVectors(arr, brr, N);
}
}
 
// This code is contributed by bgangwar59

                    

Javascript

<script>
 
// Javascript program for the above approach
 
// Function to find the magnitude
// of the given vector
function magnitude(arr, N)
{
    // Stores the final magnitude
    var magnitude = 0;
 
    // Traverse the array
    for (var i = 0; i < N; i++)
        magnitude += arr[i] * arr[i];
 
    // Return square root of magnitude
    return Math.sqrt(magnitude);
}
 
// Function to find the dot
// product of two vectors
function dotProduct(arr, brr,N)
{
    // Stores dot product
    var product = 0;
 
    // Traverse the array
    for (var i = 0; i < N; i++)
        product = product + arr[i] * brr[i];
 
    // Return the product
    return product;
}
 
function angleBetweenVectors(arr, brr, N)
{
    // Stores dot product of two vectors
    var dotProductOfVectors
        = dotProduct(arr, brr, N);
 
    // Stores magnitude of vector A
    var magnitudeOfA
        = magnitude(arr, N);
 
    // Stores magnitude of vector B
    var magnitudeOfB
        = magnitude(brr, N);
 
    // Stores angle between given vectors
    var angle = dotProductOfVectors
                   / (magnitudeOfA * magnitudeOfB);
 
    // Print the angle
    document.write( angle.toFixed(6));
}
 
// Driver Code
// Given magnitude arrays
var arr = [ -0.5, -2, 1 ];
var brr = [ -1, -1, 0.3 ];
// Size of the array
var N = arr.length;
// Function call to find the
// angle between two vectors
angleBetweenVectors(arr, brr, N);
 
</script>

                    

Output: 
0.845289

 

Time Complexity: O(1)
Auxiliary Space: O(1)


 



Similar Reads

Nth angle of a Polygon whose initial angle and per angle increment is given
Given four integers N, A, K, n where N represents the number of sides the polygon, A represents the initial angle of the polygon, K represents the per angle increase, the task is to find the nth angle of the polygon having N sides. If it is not possible then print -1. Examples: Input: N = 3, A = 30, K = 30, n = 3Output: 90Explanation:The 3rd angle
6 min read
Program to calculate angle on circumference subtended by the chord when the central angle subtended by the chord is given
Given a circle having a chord and an angle subtended by chord on center of the circle. The task here is to find the measure of the angle subtended by given chord on the circumference. Examples: Input: [Tex]\( \theta \)[/Tex] = 90Output: ABC = 45.00 degreesInput: [Tex]\( \theta \)[/Tex] = 65Output: ABC = 32.50 degrees Recommended: Please try your ap
7 min read
Difference Between one-dimensional and two-dimensional array
Array is a data structure that is used to store variables that are of similar data types at contiguous locations. The main advantage of the array is random access and cache friendliness. There are mainly three types of the array: One Dimensional (1D) ArrayTwo Dimension (2D) ArrayMultidimensional Array One Dimensional Array: It is a list of the vari
3 min read
Area of Triangle using Side-Angle-Side (length of two sides and the included angle)
Given two integers A, B representing the length of two sides of a triangle and an integer K representing the angle between them in radian, the task is to calculate the area of the triangle from the given information.Examples: Input: a = 9, b = 12, K = 2 Output: 49.1 Explanation: Area of triangle = 1 / 2 * (9 * 12 * Sin 2) = 35.12Input: A = 2, B = 4
4 min read
Angle between a chord and a tangent when angle in the alternate segment is given
Given a circle whose chord and tangent meet at a particular point. The angle in the alternate segment is given. The task here is to find the angle between the chord and the tangent.Examples: Input: z = 48 Output: 48 degrees Input: z = 64 Output: 64 degrees Approach: Let, angle BAC is the given angle in the alternate segment.let, the angle between t
3 min read
Angle subtended by the chord when the angle subtended by another chord of same length is given
Given a circle having two chords of equal length. The angle subtended by one of the chords to the centre is given. The task here is to find the measure of the angle subtended by another chord at the centre.Examples: Input: 48 Output: 48 degrees Input: 82 Output: 82 degrees Approach: Let AC &amp; BD are the two equal chords of the circle having cent
3 min read
Exterior angle of a cyclic quadrilateral when the opposite interior angle is given
Given cyclic quadrilateral inside a circle, the task is to find the exterior angle of the cyclic quadrilateral when the opposite interior angle is given.Examples: Input: 48 Output: 48 degrees Input: 83 Output: 83 degrees Approach: Let, the exterior angle, angle CDE = xand, it's opposite interior angle is angle ABCas, ADE is a straight lineso, angle
3 min read
Angle subtended by the chord to center of the circle when the angle subtended by the another equal chord of a congruent circle is given
Given are two congruent circles with two equal chords. The angle subtended to the center from the chord of one of the circles is given. The task is to find the angle subtended by the chord to the center of another circle.Examples: Input: z = 48 Output: 48 degrees Input: z = 93 Output: 93 degrees Approach: In triangle AOB and PXQ AO = PX(radii of co
3 min read
Calculate the angle between hour hand and minute hand
This problem is known as Clock angle problem where we need to find angle between hands of an analog clock at a given time.Examples: Input: h = 12:00 m = 30.00 Output: 165 degree Input: h = 3.00 m = 30.00 Output: 75 degreeRecommended PracticeAngle between hour and minute handTry It! The idea is to take 12:00 (h = 12, m = 0) as a reference. Following
7 min read
Find area of triangle if two vectors of two adjacent sides are given
Given two vectors in form of (xi+yj+zk) of two adjacent sides of a triangle. The task is to find out the area of a triangle.Examples: Input: x1 = -2, y1 = 0, z1 = -5 x2 = 1, y2 = -2, z2 = -1 Output: Area = 6.422616289332565Input: x1 = -2, y1 = 1, z1 = 5 x2 = 1, y2 = 3, z2 = -1 Output: Area = 8.860022573334675 Approach: Suppose we have two vectors a
5 min read