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

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.

主站蜘蛛池模板: 循化| 特克斯县| 阳泉市| 涿州市| 于都县| 安岳县| 巨野县| 湘乡市| 瑞安市| 鄂温| 义乌市| 泉州市| 佛坪县| 沁源县| 新安县| 张家界市| 凤台县| 临桂县| 吴江市| 大兴区| 新丰县| 营山县| 许昌县| 林周县| 和田市| 本溪| 海安县| 高邮市| 香格里拉县| 仁布县| 涟源市| 吴桥县| 南木林县| 垫江县| 繁昌县| 保康县| 聊城市| 临夏市| 绥芬河市| 罗江县| 墨脱县|