Check if two vectors are collinear or not

• Difficulty Level : Basic
• Last Updated : 22 Apr, 2021

Given six integers representing the x, y, and z coordinates of two vectors, the task is to check if the two given vectors are collinear or not.

Examples:

Input: x1 = 4, y1 = 8, z1 = 12, x2 = 8, y2 = 16, z2 = 24
Output: Yes
Explanation: The given vectors: 4i + 8j + 12k and 8i + 16j + 24k are collinear.

Input: x1 = 2, y1 = 8, z1 = -4, x2 = 4, y2 = 16, z2 = 8
Output: No
Explanation: The given vectors: 2i + 8j – 4k and 4i + 16j + 8k are not collinear.

Approach: The problem can be solved based on the idea that two vectors are collinear if any of the following conditions are satisfied:

• Two vectors A and B are collinear if there exists a number n, such that A = n · b.
• Two vectors are collinear if relations of their coordinates are equal, i.e. x1 / x2 = y1 / y2 = z1 / z2
Note: This condition is not valid if one of the components of the vector is zero.
• Two vectors are collinear if their cross product is equal to the NULL Vector.

Therefore, to solve the problem, the idea is to check if the cross-product of the two given vectors is equal to the NULL Vector or not. If found to be true, then print Yes. Otherwise, print No

Below is the implementation of the above approach:

C++14

 // C++ program for the above approach#include using namespace std; // Function to calculate cross// product of two vectorsvoid crossProduct(int vect_A[],                  int vect_B[],                  int cross_P[]){    // Update cross_P    cross_P        = vect_A * vect_B          - vect_A * vect_B;     // Update cross_P    cross_P        = vect_A * vect_B          - vect_A * vect_B;     // Update cross_P    cross_P        = vect_A * vect_B          - vect_A * vect_B;} // Function to check if two given// vectors are collinear or notvoid checkCollinearity(int x1, int y1,                       int z1, int x2,                       int y2, int z2){    // Store the first and second vectors    int A = { x1, y1, z1 };    int B = { x2, y2, z2 };     // Store their cross product    int cross_P;     // Calculate their cross product    crossProduct(A, B, cross_P);     // Check if their cross product    // is a NULL Vector or not    if (cross_P == 0 && cross_P == 0        && cross_P == 0)        cout << "Yes";    else        cout << "No";} // Driver Codeint main(){    // Given coordinates    // of the two vectors    int x1 = 4, y1 = 8, z1 = 12;    int x2 = 8, y2 = 16, z2 = 24;     checkCollinearity(x1, y1, z1,                      x2, y2, z2);     return 0;}

Java

 // Java program for the above approachclass GFG{     // Function to calculate cross// product of two vectorsstatic void crossProduct(int vect_A[],                         int vect_B[],                         int cross_P[]){         // Update cross_P    cross_P = vect_A * vect_B -                 vect_A * vect_B;     // Update cross_P    cross_P = vect_A * vect_B -                 vect_A * vect_B;     // Update cross_P    cross_P = vect_A * vect_B -                 vect_A * vect_B;} // Function to check if two given// vectors are collinear or notstatic void checkCollinearity(int x1, int y1,                              int z1, int x2,                              int y2, int z2){         // Store the first and second vectors    int A[] = { x1, y1, z1 };    int B[] = { x2, y2, z2 };     // Store their cross product    int cross_P[] = new int;     // Calculate their cross product    crossProduct(A, B, cross_P);     // Check if their cross product    // is a NULL Vector or not    if (cross_P == 0 && cross_P == 0 &&        cross_P == 0)        System.out.print("Yes");    else        System.out.print("No");} // Driver Codepublic static void main (String[] args){         // Given coordinates    // of the two vectors    int x1 = 4, y1 = 8, z1 = 12;    int x2 = 8, y2 = 16, z2 = 24;     checkCollinearity(x1, y1, z1,                      x2, y2, z2);}} // This code is contributed by AnkThon

Python3

 # Python3 program for the above approach # Function to calculate cross# product of two vectorsdef crossProduct(vect_A, vect_B, cross_P):    # Update cross_P    cross_P = (vect_A * vect_B -                  vect_A * vect_B)     # Update cross_P    cross_P = (vect_A * vect_B -                  vect_A * vect_B)     # Update cross_P    cross_P = (vect_A * vect_B -                  vect_A * vect_B) # Function to check if two given# vectors are collinear or notdef checkCollinearity(x1, y1, z1, x2, y2, z2):         # Store the first and second vectors    A = [x1, y1, z1]    B = [x2, y2, z2]     # Store their cross product    cross_P = [0 for i in range(3)]     # Calculate their cross product    crossProduct(A, B, cross_P)     # Check if their cross product    # is a NULL Vector or not    if (cross_P == 0 and        cross_P == 0 and        cross_P == 0):        print("Yes")    else:        print("No") # Driver Codeif __name__ == '__main__':         # Given coordinates    # of the two vectors    x1 = 4    y1 = 8    z1 = 12    x2 = 8    y2 = 16    z2 = 24     checkCollinearity(x1, y1, z1, x2, y2, z2) # This code is contributed by bgangwar59

C#

 // C# program for the above approachusing System; class GFG{     // Function to calculate cross// product of two vectorsstatic void crossProduct(int []vect_A,                         int []vect_B,                         int []cross_P){         // Update cross_P    cross_P = vect_A * vect_B -                 vect_A * vect_B;     // Update cross_P    cross_P = vect_A * vect_B -                 vect_A * vect_B;     // Update cross_P    cross_P = vect_A * vect_B -                 vect_A * vect_B;} // Function to check if two given// vectors are collinear or notstatic void checkCollinearity(int x1, int y1,                              int z1, int x2,                              int y2, int z2){         // Store the first and second vectors    int []A = { x1, y1, z1 };    int []B = { x2, y2, z2 };     // Store their cross product    int []cross_P = new int;     // Calculate their cross product    crossProduct(A, B, cross_P);     // Check if their cross product    // is a NULL Vector or not    if (cross_P == 0 && cross_P == 0 &&        cross_P == 0)        Console.Write("Yes");    else        Console.Write("No");} // Driver Codepublic static void Main (string[] args){         // Given coordinates    // of the two vectors    int x1 = 4, y1 = 8, z1 = 12;    int x2 = 8, y2 = 16, z2 = 24;     checkCollinearity(x1, y1, z1,                      x2, y2, z2);}} // This code is contributed by AnkThon

Javascript


Output:
Yes

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

My Personal Notes arrow_drop_up