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

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.

主站蜘蛛池模板: 盱眙县| 仁寿县| 巴东县| 海伦市| 兴隆县| 仁化县| 日喀则市| 赞皇县| 临漳县| 社会| 凤城市| 安义县| 冷水江市| 平原县| 庆元县| 锡林郭勒盟| 淳安县| 突泉县| 道真| 普安县| 新龙县| 邹平县| 巴楚县| 会理县| 新泰市| 芜湖县| 左贡县| 东乡县| 江华| 铜梁县| 梅河口市| 历史| 古田县| 大荔县| 边坝县| 鄂温| 肇源县| 沈丘县| 吉木乃县| 茶陵县| 城固县|