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

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.

主站蜘蛛池模板: 新乐市| 麻阳| 楚雄市| 多伦县| 历史| 日土县| 马尔康县| 佛冈县| 三原县| 宜川县| 西峡县| 香格里拉县| 静海县| 西吉县| 罗田县| 屏东市| 大埔县| 法库县| 乡城县| 盘山县| 廉江市| 巨鹿县| 米易县| 巨鹿县| 天镇县| 新野县| 永善县| 新宾| 高雄市| 阜南县| 涿鹿县| 凤庆县| 旬阳县| 敦煌市| 尼勒克县| 手游| 武安市| 威宁| 城步| 开阳县| 清流县|