官术网_书友最值得收藏!

Projection

Sometimes it's useful to decompose a vector into parallel and perpendicular components with respect to another vector. Projecting Projection onto Projection will give us the length of Projection in the direction of Projection. This projection decomposes Projection into its parallel component with respect to Projection. Once we know the parallel component of Projection, we can use it to get the perpendicular component. The formula for projecting Projection onto Projection is as follows:

Projection

The perpendicular component of Projection with respect to Projection is defined as follows:

Projection

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 Getting ready, and direction is Getting ready.

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:

  1. 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);
  2. 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);
    }
  3. 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 How it works… onto How it works…, to find How it works…. Having a ' character next to a vector means prime; it's a transformed version of the vector; How it works… is pronounced A-Prime:

How it works…

From the preceding figure we see that How it works… can be found by subtracting some unknown vector from How it works…. This unknown vector is the perpendicular component of How it works… with respect to How it works…, let's call it How it works…:

How it works…

We can get the perpendicular component How it works… by subtracting the projection of How it works… onto How it works… fromHow it works…. The projection at this point is still unknown, that's what we are trying to find:

How it works…

Because How it works… points in the same direction as How it works…, we can express How it works… as scaling How it works… by some unknown scalar s, How it works…. Knowing this, the problem becomes, how do we find s?:

How it works…

The dot product of two perpendicular vectors is 0. Because of this, the dot product of How it works… and How it works… is going to be 0:

How it works…

Substitute the value of How it works… with the equation we use to find its value, How it works…:

How it works…

Finally, let's substitute How it works… with the equation we use to find its value, How it works…:

How it works…

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…:

How it works…

Let's start to isolate s, first we add How it works… to both sides of the equation:

How it works…

Now we can isolate s if we divide both sides of the equation by How it works…. Remember, the dot product of a vector with itself yields the square magnitude of that vector:

How it works…

Now we can solve How it works… by substituting s with the preceding formula. The final equation becomes:

How it works…
主站蜘蛛池模板: 祁门县| 晋州市| 常山县| 略阳县| 友谊县| 梁平县| 西宁市| 五原县| 和龙市| 廉江市| 金阳县| 沙洋县| 邹城市| 浏阳市| 永仁县| 九龙坡区| 宁德市| 台安县| 泽普县| 鱼台县| 张掖市| 郴州市| 明水县| 卢湾区| 晴隆县| 承德市| 佛冈县| 泾源县| 日照市| 基隆市| 余江县| 休宁县| 阆中市| 祁连县| 天津市| 宿州市| 丹凤县| 阿克陶县| 二手房| 东乌珠穆沁旗| 武安市|