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

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.

主站蜘蛛池模板: 定兴县| 扶绥县| 县级市| 五莲县| 白朗县| 汝州市| 武定县| 磐安县| 亳州市| 青龙| 鱼台县| 青浦区| 海伦市| 探索| 万州区| 东乡族自治县| 永济市| 九江市| 郴州市| 隆子县| 灵寿县| 瑞昌市| 肥城市| 重庆市| 金乡县| 上高县| 息烽县| 六盘水市| 安平县| 南涧| 郯城县| 三江| 浦江县| 丰宁| 蓬溪县| 本溪市| 宁城县| 佛山市| 印江| 乐昌市| 梓潼县|