The Cross-Product Route
2. Using the Cross Product to Detect Alignment
Here’s where things get a bit geometrical, but don’t worry, we’ll keep it simple. The cross product is a powerful tool for determining if two vectors are collinear. The magnitude of the cross product is equal to the area of the parallelogram formed by the two vectors. If the vectors are collinear, the area of this parallelogram is zero!
In 2D space, the cross product is a bit of a misnomer, as it technically only exists in 3D. However, we can extend our 2D vectors to 3D by adding a z-component of 0. This allows us to calculate the cross product. If the resulting z-component of the cross product is zero, the vectors are collinear. A little hack, if you will.
Mathematically, if we have two vectors a = (a, a) and b = (b, b), extending them to 3D gives us a = (a, a, 0) and b = (b, b, 0). The cross product a x b will result in (0, 0, ab – ab). So, if (ab – ab) = 0, the vectors are collinear. See? Not as scary as it sounds!
Lets look at an example. Vector a = (2, 4) and vector b = (1, 2). Calculate: (2 2) – (4 1) = 4 – 4 = 0. So, these vectors are collinear. Easy peasy! This method is especially useful when you have a good visual intuition for the vectors and want a relatively quick confirmation.