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