Check if given vectors are Parallel or Perpendicular?
Last Updated :
10 Oct, 2022
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++
#include <bits/stdc++.h>
using namespace std;
int find(vector< int > A, vector< int > B)
{
int per = A[0] * B[0] + A[1] * B[1] + A[2] * B[2];
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;
}
}
int main()
{
vector< int > a = { 3, 2, 1 };
vector< int > b = { 6, 4, 2 };
cout << find(a, b) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int find( int [] A, int [] B)
{
int per = A[ 0 ] * B[ 0 ] + A[ 1 ] * B[ 1 ] + A[ 2 ] * B[ 2 ];
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 };
System.out.println(find(a, b));
}
}
|
Python3
def find(A, B):
per = A[ 0 ] * B[ 0 ] + A[ 1 ] * B[ 1 ] + A[ 2 ] * B[ 2 ]
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
if __name__ = = "__main__" :
a = [ 3 , 2 , 1 ]
b = [ 6 , 4 , 2 ]
print (find(a, b))
|
C#
using System;
public class GFG
{
public static int find( int [] A, int [] B)
{
int per = A[0] * B[0] + A[1] * B[1] + A[2] * B[2];
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 };
Console.WriteLine(find(a, b));
}
}
|
Javascript
<script>
function find(A, B)
{
let per = A[0] * B[0] + A[1] * B[1] + A[2] * B[2];
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;
}
}
let a = [3, 2, 1];
let b = [6, 4, 2];
document.write(find(a, b));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...