1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
#include "MathLibrary.h"
#include <iostream>
#include <cmath>
namespace MathLib
{
// Vector3 实现
Vector3::Vector3() : x(0), y(0), z(0) {}
Vector3::Vector3(float x, float y, float z) : x(x), y(y), z(z) {}
Vector3 Vector3::operator+(const Vector3& other) const
{
return Vector3(x + other.x, y + other.y, z + other.z);
}
Vector3 Vector3::operator-(const Vector3& other) const
{
return Vector3(x - other.x, y - other.y, z - other.z);
}
Vector3 Vector3::operator*(float scalar) const
{
return Vector3(x * scalar, y * scalar, z * scalar);
}
float Vector3::Magnitude() const
{
return std::sqrt(x * x + y * y + z * z);
}
Vector3 Vector3::Normalize() const
{
float mag = Magnitude();
if (mag > 0)
return Vector3(x / mag, y / mag, z / mag);
return Vector3();
}
float Vector3::Dot(const Vector3& other) const
{
return x * other.x + y * other.y + z * other.z;
}
Vector3 Vector3::Cross(const Vector3& other) const
{
return Vector3(
y * other.z - z * other.y,
z * other.x - x * other.z,
x * other.y - y * other.x
);
}
void Vector3::Print() const
{
std::cout << "(" << x << ", " << y << ", " << z << ")" << std::endl;
}
// Matrix4 实现
Matrix4::Matrix4()
{
for (int i = 0; i < 16; ++i)
m_Data[i] = 0.0f;
}
Matrix4::Matrix4(float diagonal)
{
for (int i = 0; i < 16; ++i)
m_Data[i] = 0.0f;
m_Data[0] = diagonal; // [0,0]
m_Data[5] = diagonal; // [1,1]
m_Data[10] = diagonal; // [2,2]
m_Data[15] = diagonal; // [3,3]
}
Matrix4 Matrix4::Identity()
{
return Matrix4(1.0f);
}
float& Matrix4::operator[](int index)
{
return m_Data[index];
}
const float& Matrix4::operator[](int index) const
{
return m_Data[index];
}
void Matrix4::Print() const
{
for (int row = 0; row < 4; ++row)
{
for (int col = 0; col < 4; ++col)
{
std::cout << m_Data[row * 4 + col] << "\t";
}
std::cout << std::endl;
}
}
// 工具函数实现
float ToRadians(float degrees)
{
return degrees * 3.14159f / 180.0f;
}
float ToDegrees(float radians)
{
return radians * 180.0f / 3.14159f;
}
float Clamp(float value, float min, float max)
{
if (value < min) return min;
if (value > max) return max;
return value;
}
float Lerp(float a, float b, float t)
{
return a + (b - a) * Clamp(t, 0.0f, 1.0f);
}
}
|