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

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.

主站蜘蛛池模板: 抚宁县| 武乡县| 寿光市| 淮安市| 葵青区| 宝应县| 琼结县| 泸水县| 永安市| 濮阳市| 桂林市| 内乡县| 汶上县| 东明县| 仙桃市| 鸡东县| 全南县| 乾安县| 阜平县| 云南省| 阿坝| 沾化县| 和平县| 林芝县| 乃东县| 达孜县| 双桥区| 广丰县| 平度市| 邓州市| 衢州市| 平舆县| 新田县| 武清区| 鲁山县| 启东市| 万宁市| 南阳市| 博湖县| 上林县| 房产|