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

Vector subtraction

As with adding vectors, subtracting vectors is also a component-wise operation. You can think of subtracting vectors as adding the negative of the second vector to the first vector. When visualized as an arrow, subtraction points from the tip of the second vector to the tip of the first one.

To visually subtract vectors, place both vectors so they share the same origin. Draw a vector from the tip of the second arrow to the tip of the first one. The resulting arrow is the subtraction result vector:

Figure 2.3: Vector subtraction

Figure 2.3: Vector subtraction

To implement vector subtraction, subtract like components. Implement the subtraction function by overloading the - operator in vec3.cpp. Don't forget to add the function declaration to vec3.h:

vec3 operator-(const vec3 &l, const vec3 &r) {

    return vec3(l.x - r.x, l.y - r.y, l.z - r.z);

}

The steps and logic are very similar to vector addition. It might help to think of vector subtraction as adding a negative vector.

主站蜘蛛池模板: 龙岩市| 什邡市| 自治县| 彭水| 赤水市| 潮安县| 交口县| 丽江市| 灵璧县| 山丹县| 台州市| 元江| 武平县| 布拖县| 信宜市| 普兰店市| 邻水| 温泉县| 汾阳市| 长武县| 桦南县| 隆安县| 碌曲县| 柳河县| 灌阳县| 台山市| 琼海市| 保山市| 普格县| 宜宾县| 沅陵县| 清镇市| 翁源县| 大丰市| 炉霍县| 郁南县| 田阳县| 诸城市| 东台市| 杂多县| 巴中市|