Projection
Sometimes it's useful to decompose a vector into parallel and perpendicular components with respect to another vector. Projecting onto
will give us the length of
in the direction of
. This projection decomposes
into its parallel component with respect to
. Once we know the parallel component of
, we can use it to get the perpendicular component. The formula for projecting
onto
is as follows:
![Projection](https://static.packt-cdn.com/products/9781787123663/graphics/graphics/B05887_1_072.jpg)
The perpendicular component of with respect to
is defined as follows:
![Projection](https://static.packt-cdn.com/products/9781787123663/graphics/graphics/B05887_1_073.jpg)
Getting ready
Implementing the projection is fairly straightforward as we already have both the dot product and magnitude squared defined. In the following function, the vector being projected is represented by the variable length
, and the vector it is being projected onto is represented by the variable direction
. If we compare it to the preceding formula, length
is , and
direction
is .
How to do it…
Follow these steps to implement projection functions for two and three dimensional vectors. A function to get the perpendicular component of the projection is also described:
- Declare the projection and perpendicular functions in
vectors.h
:vec2 Project(const vec2& length, const vec2& direction); vec3 Project(const vec3& length, const vec3& direction); vec2 Perpendicular(const vec2& len, const vec2& dir); vec3 Perpendicular(const vec3& len, const vec3& dir);
- Add the implementation of projection to
vectors.cpp
:vec2 Project(const vec2& length, const vec2& direction) { float dot = Dot(length, direction); float magSq = MagnitudeSq(direction); return direction * (dot / magSq); } vec3 Project(const vec3& length, const vec3& direction) { float dot = Dot(length, direction); float magSq = MagnitudeSq(direction); return direction * (dot / magSq); }
- Add the implementation of perpendicular to
vectors.cpp
:vec2 Perpendicular(const vec2& len, const vec2& dir) { return len - Project(len, dir); } vec3 Perpendicular(const vec3& len, const vec3& dir) { return len - Project(len, dir); }
How it works…
Let's explore how projection works. Say we want to project onto
, to find
. Having a ' character next to a vector means prime; it's a transformed version of the vector;
is pronounced A-Prime:
![How it works…](https://static.packt-cdn.com/products/9781787123663/graphics/graphics/B05887_1_075.jpg)
From the preceding figure we see that can be found by subtracting some unknown vector from
. This unknown vector is the perpendicular component of
with respect to
, let's call it
:
![How it works…](https://static.packt-cdn.com/products/9781787123663/graphics/graphics/B05887_1_076.jpg)
We can get the perpendicular component by subtracting the projection of
onto
from
. The projection at this point is still unknown, that's what we are trying to find:
![How it works…](https://static.packt-cdn.com/products/9781787123663/graphics/graphics/B05887_1_077.jpg)
Because points in the same direction as
, we can express
as scaling
by some unknown scalar s,
. Knowing this, the problem becomes, how do we find s?:
![How it works…](https://static.packt-cdn.com/products/9781787123663/graphics/graphics/B05887_1_079.jpg)
The dot product of two perpendicular vectors is 0. Because of this, the dot product of and
is going to be 0:
![How it works…](https://static.packt-cdn.com/products/9781787123663/graphics/graphics/B05887_1_080.jpg)
Substitute the value of with the equation we use to find its value,
:
![How it works…](https://static.packt-cdn.com/products/9781787123663/graphics/graphics/B05887_1_082.jpg)
Finally, let's substitute with the equation we use to find its value,
:
![How it works…](https://static.packt-cdn.com/products/9781787123663/graphics/graphics/B05887_1_083.jpg)
Now the only unknown in the formula is s, let's try to find it. The dot product exhibits the distributive property, let's distribute :
![How it works…](https://static.packt-cdn.com/products/9781787123663/graphics/graphics/B05887_1_084.jpg)
Let's start to isolate s, first we add to both sides of the equation:
![How it works…](https://static.packt-cdn.com/products/9781787123663/graphics/graphics/B05887_1_086.jpg)
Now we can isolate s if we divide both sides of the equation by . Remember, the dot product of a vector with itself yields the square magnitude of that vector:
![How it works…](https://static.packt-cdn.com/products/9781787123663/graphics/graphics/B05887_1_088.jpg)
Now we can solve by substituting s with the preceding formula. The final equation becomes:
![How it works…](https://static.packt-cdn.com/products/9781787123663/graphics/graphics/B05887_1_089.jpg)