- Hands-On C++ Game Animation Programming
- Gabor Szauer
- 236字
- 2021-06-30 14:45:57
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.
- Visual C++程序設計教程
- C# 從入門到項目實踐(超值版)
- C語言程序設計
- Bootstrap Essentials
- 深度強化學習算法與實踐:基于PyTorch的實現
- 51單片機C語言開發教程
- 智能搜索和推薦系統:原理、算法與應用
- Java EE企業級應用開發教程(Spring+Spring MVC+MyBatis)
- 微信小程序開發實戰:設計·運營·變現(圖解案例版)
- Serverless Web Applications with React and Firebase
- FPGA嵌入式項目開發實戰
- Mockito Essentials
- Mastering HTML5 Forms
- Access數據庫應用教程(2010版)
- 產品架構評估原理與方法