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

Comparing vectors

The last operation that needs to be implemented is vector comparison. Comparison is a component-wise operation; each element must be compared using an epsilon. Another way to measure whether two vectors are the same is to subtract them. If they were equal, subtracting them would yield a vector with no length.

Overload the == and != operators in vec3.cpp. Don't forget to add the function declarations to vec3.h:

bool operator==(const vec3 &l, const vec3 &r) {

    vec3 diff(l - r);

    return lenSq(diff) < VEC3_EPSILON;

}

bool operator!=(const vec3 &l, const vec3 &r) {

    return !(l == r);

}

Important note:

Finding the right epsilon value to use for comparison operations is difficult. In this chapter, you declared 0.000001f as the epsilon. This value is the result of some trial and error. To learn more about comparing floating point values, check out https://bitbashing.io/comparing-floats.html.

In the next section, you will implement vectors with two and four components. These vectors will only be used as a convenient way to store data; they won't actually need any math operations implemented on them.

主站蜘蛛池模板: 通化县| 镇赉县| 报价| 萝北县| 水富县| 清丰县| 怀远县| 金堂县| 富源县| 慈溪市| 山东| 岑溪市| 米泉市| 贵港市| 泽普县| 株洲县| 永平县| 辽源市| 扶沟县| 建平县| 崇左市| 怀仁县| 贞丰县| 沽源县| 原阳县| 叙永县| 礼泉县| 凉城县| 亳州市| 北川| 乌鲁木齐县| 广东省| 咸阳市| 庄浪县| 斗六市| 山东省| 商水县| 保靖县| 谷城县| 凤阳县| 浮山县|