 # Interesting decision based on cross product of two vectors. How do one decide the direction?

I was solving the question Problem - A - Codeforces.
If the cross product is zero then obviously they are collinear since sin(x)=0 if x=2nPI.
However, how do they decide if it is Left or Right based on the value of cross-product i.e; x1y2-x2y1.
Please demystify this for me. Am I missing any fundamentals?

#include<iostream>

using namespace std;

long long xa,xb,ya,yb,xc,yc,t;

long long cp(long long x1,long long x2,long long y1,long long y2)
{
return x1*y2-x2*y1;
}
int main()
{
cin>>xa>>ya>>xb>>yb>>xc>>yc;
t=cp(xc-xa,xb-xa,yc-ya,yb-ya);
if (t==0)
{
cout<<"TOWARDS"<<endl;
}
if (t<0)
{
cout<<"LEFT"<<endl;
}
if (t>0)
{
cout<<"RIGHT"<<endl;
}
return 0;
}



The Cross Product of two vectors is a vector that is perpendicular to both vectors. The direction of the Cross Product is given by the right-hand rule. You can understand it better by taking cross products of the unit vectors \^i, \^j and \^j.

\^i\times \^j = \^k
\^j\times \^i = -\^k
\^i\times \^k = -\^j
\^k\times \^i = \^j
\^j\times \^k = \^i
\^k\times \^j = -\^i

Edit: We can see that the result is positive when the direction is anti-clockwise.

1 Like