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

Comparing matrices

Comparing matrices is a component-wise operation. Two matrices are the same only if all their components are the same. To compare two matrices, loop through and compare all of their components. Since you are comparing floating point numbers, an epsilon should be used.

Create a new file, mat4.cpp. Implement the matrix equality and inequality operators in this file. The equality operator should check whether two matrices are the same; the inequality operator returns the opposite of the equality operator. Don't forget to add the function declarations to mat4.h:

bool operator==(const mat4& a, const mat4& b) {

    for (int i = 0; i < 16; ++i) {

        if (fabsf(a.v[i] - b.v[i]) > MAT4_EPSILON) {

            return false;

        }

    }

    return true;

}

bool operator!=(const mat4& a, const mat4& b) {

    return !(a == b);

}

Important note

The MAT4_EPSILON constant should be defined in mat4.h. 0.000001f is a good default value to use.

When comparing matrices by component, you are checking for literal equality. There are other ways to define matrix equality; for example, regardless of shape, the volume of two matrices can be compared using their determinants. Matrix determinants will be covered later in this chapter.

In the next section, you will learn how to add matrices together.

主站蜘蛛池模板: 灵石县| 靖江市| 清丰县| 丹棱县| 富川| 尼玛县| 遵义县| 荆州市| 芜湖市| 曲沃县| 彭阳县| 涟水县| 大石桥市| 德化县| 邳州市| 凤翔县| 丹东市| 达州市| 英德市| 镇远县| 盐山县| 罗田县| 洱源县| 芮城县| 英山县| 金门县| 泰来县| 长沙市| 克东县| 绥芬河市| 左云县| 娄烦县| 永宁县| 吉木萨尔县| 泰顺县| 池州市| 林西县| 延庆县| 晋中市| 错那县| 始兴县|