Open In App

Check if given vectors are Parallel or Perpendicular?

Last Updated : 10 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given two force vectors, find out whether they are parallel, perpendicular or neither. Let the first vector be A = a1 i +a2 j + a3 k and the second vector be B = b1 i + b2 j + b3 k.

A.B = a1 * b1 + a2 * b2 + a3 * b3
A x B = (a2 * b3 – a3 * b2) i – (a1 * b3 – b1* a3) j + (a1 * b2 – a2 * b1) k
|A|2 = a12 + a22 + a32
If A.B = 0, then A and B are perpendicular.
If |A X B|2 = 0, then A and B are parallel.

Return 1 if they are parallel to each other, 2 if they are perpendicular to each other or 0 otherwise.

Examples:

Input: A = {3, 2, 1}, B = {6, 4, 2}
Output: 1
Explanation: |A X B|2 = 0

Input: A = {4, 6, 1}, B = {1, -1, 2}
Output: 2
Explanation: A.B = 0

Approach: Follow the steps to solve this problem:

  • Find A.B and A X B 
  • If A.B = 0, then return 2.
  • Else if, |A X B| = 0, then  |A X B|2 is also 0, then return 1.
  • Else, return 0.

Below is the implementation of the above approach.

C++




// C++ code to implement the approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find out whether they
// are parallel, perpendicular or neither
int find(vector<int> A, vector<int> B)
{
    // Find A.B
    int per = A[0] * B[0] + A[1] * B[1] + A[2] * B[2];
 
    // Find A X B
    int par = (A[1] * B[2] - A[2] * B[1])
                  * (A[1] * B[2] - A[2] * B[1])
              + (A[0] * B[2] - B[0] * A[2])
                    * (A[0] * B[2] - B[0] * A[2])
              + (A[0] * B[1] - A[1] * B[0])
                    * (A[0] * B[1] - A[1] * B[0]);
 
    if (per == 0) {
        return 2;
    }
 
    else if (par == 0) {
        return 1;
    }
 
    else {
        return 0;
    }
}
 
// Driver Code
int main()
{
    vector<int> a = { 3, 2, 1 };
    vector<int> b = { 6, 4, 2 };
 
    // Function call
    cout << find(a, b) << endl;
    return 0;
}


Java




// Java code for the above approach
import java.io.*;
 
class GFG {
 
  // Function to find out whether they are parallel,
  // perpendicular or neither
  static int find(int[] A, int[] B)
  {
    // Find A.B
    int per = A[0] * B[0] + A[1] * B[1] + A[2] * B[2];
 
    // Find A X B
    int par = (A[1] * B[2] - A[2] * B[1])
      * (A[1] * B[2] - A[2] * B[1])
      + (A[0] * B[2] - B[0] * A[2])
      * (A[0] * B[2] - B[0] * A[2])
      + (A[0] * B[1] - A[1] * B[0])
      * (A[0] * B[1] - A[1] * B[0]);
 
    if (per == 0) {
      return 2;
    }
 
    else if (par == 0) {
      return 1;
    }
 
    else {
      return 0;
    }
  }
 
  public static void main(String[] args)
  {
    int[] a = { 3, 2, 1 };
    int[] b = { 6, 4, 2 };
 
    // Function call
    System.out.println(find(a, b));
  }
}
 
// This code is contributed by lokeshmvs21.


Python3




# Python3 code to implement the approach
 
# Function to find out whether they
# are parallel, perpendicular or neither
def find(A, B):
 
    # Find A.B
    per = A[0] * B[0] + A[1] * B[1] + A[2] * B[2]
 
    # Find A X B
    par = (A[1] * B[2] - A[2] * B[1]) * (A[1] * B[2] - A[2] * B[1]) + (A[0] * B[2] - B[0] * A[2]) * \
        (A[0] * B[2] - B[0] * A[2]) + (A[0] * B[1] -
                                       A[1] * B[0]) * (A[0] * B[1] - A[1] * B[0])
 
    if (per == 0):
        return 2
 
    elif (par == 0):
        return 1
 
    else:
        return 0
 
# Driver Code
if __name__ == "__main__":
 
    a = [3, 2, 1]
    b = [6, 4, 2]
 
    # Function call
    print(find(a, b))
 
    # This code is contributed by rakeshsahni


C#




using System;
 
public class GFG
{
 
  // Function to find out whether they
  // are parallel, perpendicular or neither
  public static int find(int[] A, int[] B)
  {
 
    // Find A.B
    int per = A[0] * B[0] + A[1] * B[1] + A[2] * B[2];
 
    // Find A X B
    int par = (A[1] * B[2] - A[2] * B[1])
      * (A[1] * B[2] - A[2] * B[1])
      + (A[0] * B[2] - B[0] * A[2])
      * (A[0] * B[2] - B[0] * A[2])
      + (A[0] * B[1] - A[1] * B[0])
      * (A[0] * B[1] - A[1] * B[0]);
 
    if (per == 0) {
      return 2;
    }
 
    else if (par == 0) {
      return 1;
    }
 
    else {
      return 0;
    }
  }
 
  static public void Main()
  {
 
    int[] a = { 3, 2, 1 };
    int[] b = { 6, 4, 2 };
 
    // Function call
    Console.WriteLine(find(a, b));
  }
}
 
// This code is contributed by akashish__


Javascript




<script>
        // JavaScript code for the above approach
 
        // Function to find out whether they
        // are parallel, perpendicular or neither
        function find(A, B)
        {
         
            // Find A.B
            let per = A[0] * B[0] + A[1] * B[1] + A[2] * B[2];
 
            // Find A X B
            let par = (A[1] * B[2] - A[2] * B[1])
                * (A[1] * B[2] - A[2] * B[1])
                + (A[0] * B[2] - B[0] * A[2])
                * (A[0] * B[2] - B[0] * A[2])
                + (A[0] * B[1] - A[1] * B[0])
                * (A[0] * B[1] - A[1] * B[0]);
 
            if (per == 0) {
                return 2;
            }
 
            else if (par == 0) {
                return 1;
            }
 
            else {
                return 0;
            }
        }
 
        // Driver Code
        let a = [3, 2, 1];
        let b = [6, 4, 2];
 
        // Function call
        document.write(find(a, b));
 
 // This code is contributed by Potta Lokesh
 
    </script>


Output

1

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads