-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathTSMatrix2D.cpp
139 lines (107 loc) · 2.61 KB
/
TSMatrix2D.cpp
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
127
128
129
130
131
132
133
134
135
136
137
138
139
//
// This file is part of the Terathon Math Library, by Eric Lengyel.
// Copyright 1999-2024, Terathon Software LLC
//
// This software is distributed under the MIT License.
// Separate proprietary licenses are available from Terathon Software.
//
#include "TSMatrix2D.h"
using namespace Terathon;
const ConstMatrix2D Matrix2D::identity = {{{1.0F, 0.0F}, {0.0F, 1.0F}}};
Matrix2D::Matrix2D(float n00, float n01, float n10, float n11) : Mat2D<TypeMatrix2D>(n00, n01, n10, n11)
{
}
Matrix2D::Matrix2D(const Vector2D& a, const Vector2D& b)
{
col0 = a;
col1 = b;
}
Matrix2D& Matrix2D::Set(float n00, float n01, float n10, float n11)
{
matrix.Set(n00, n01, n10, n11);
return (*this);
}
Matrix2D& Matrix2D::Set(const Vector2D& a, const Vector2D& b)
{
col0 = a;
col1 = b;
return (*this);
}
Matrix2D& Matrix2D::operator *=(const Matrix2D& m)
{
float t = m00 * m(0,0) + m01 * m(1,0);
m01 = m00 * m(0,1) + m01 * m(1,1);
m00 = t;
t = m10 * m(0,0) + m11 * m(1,0);
m11 = m10 * m(0,1) + m11 * m(1,1);
m10 = t;
return (*this);
}
Matrix2D& Matrix2D::operator *=(float s)
{
matrix *= s;
return (*this);
}
Matrix2D& Matrix2D::operator /=(float s)
{
matrix /= s;
return (*this);
}
Matrix2D& Matrix2D::SetIdentity(void)
{
m00 = m11 = 1.0F;
m01 = m10 = 0.0F;
return (*this);
}
Matrix2D& Matrix2D::Orthogonalize(int32 column)
{
Vector2D& x = (*this)[column];
Vector2D& y = (*this)[column ^ 1];
x.Normalize();
y = Normalize(y - x * Dot(x, y));
return (*this);
}
Matrix2D Matrix2D::MakeRotation(float angle)
{
Vector2D v = CosSin(angle);
return (Matrix2D(v.x, -v.y, v.y, v.x));
}
Matrix2D Matrix2D::MakeScale(float scale)
{
return (Matrix2D(scale, 0.0F, 0.0F, scale));
}
Matrix2D Matrix2D::MakeScale(float sx, float sy)
{
return (Matrix2D(sx, 0.0F, 0.0F, sy));
}
Matrix2D Matrix2D::MakeScaleX(float sx)
{
return (Matrix2D(sx, 0.0F, 0.0F, 1.0F));
}
Matrix2D Matrix2D::MakeScaleY(float sy)
{
return (Matrix2D(1.0F, 0.0F, 0.0F, sy));
}
Matrix2D Terathon::operator *(const Matrix2D& m, float s)
{
return (Matrix2D(m(0,0) * s, m(0,1) * s, m(1,0) * s, m(1,1) * s));
}
Matrix2D Terathon::operator /(const Matrix2D& m, float s)
{
s = 1.0F / s;
return (Matrix2D(m(0,0) * s, m(0,1) * s, m(1,0) * s, m(1,1) * s));
}
float Terathon::Determinant(const Matrix2D& m)
{
return (m(0,0) * m(1,1) - m(0,1) * m(1,0));
}
Matrix2D Terathon::Inverse(const Matrix2D& m)
{
float invDet = 1.0F / (m(0,0) * m(1,1) - m(0,1) * m(1,0));
return (Matrix2D( m(1,1) * invDet, -m(0,1) * invDet,
-m(1,0) * invDet, m(0,0) * invDet));
}
Matrix2D Terathon::Adjugate(const Matrix2D& m)
{
return (Matrix2D(m(1,1), -m(0,1), -m(1,0), m(0,0)));
}