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

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.

主站蜘蛛池模板: 河东区| 富裕县| 贺州市| 临泉县| 青阳县| 佛坪县| 恩平市| 长武县| 东海县| 灵川县| 唐海县| 大竹县| 垣曲县| 耒阳市| 甘洛县| 屯留县| 湘潭市| 阿拉尔市| 资源县| 元阳县| 卓资县| 木兰县| 石林| 南靖县| 重庆市| 中西区| 萨迦县| 阿瓦提县| 察哈| 通江县| 香格里拉县| 九龙城区| 沙雅县| 乐至县| 常熟市| 仙居县| 资讯 | 衡阳县| 江门市| 鸡泽县| 福州市|