- 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.
- ServiceNow Application Development
- JavaScript百煉成仙
- Java 9 Concurrency Cookbook(Second Edition)
- Rust實戰
- Java完全自學教程
- Oracle 12c中文版數據庫管理、應用與開發實踐教程 (清華電腦學堂)
- 基于Java技術的Web應用開發
- 深入淺出Android Jetpack
- Silverlight魔幻銀燈
- Python機器學習實戰
- Getting Started with Python
- 單片機原理及應用技術
- C編程技巧:117個問題解決方案示例
- 零基礎輕松學C++:青少年趣味編程(全彩版)
- 超簡單:用Python讓Excel飛起來(實戰150例)