-
Notifications
You must be signed in to change notification settings - Fork 0
/
mat2.go
85 lines (69 loc) · 1.59 KB
/
mat2.go
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
package mathg
import "math"
/*
m11 m12
m21 m22
*/
type Mat2 struct {
M11 float64
M21 float64
M12 float64
M22 float64
}
func (m *Mat2) Zero() *Mat2 {
return &Mat2{0., 0., 0., 0.}
}
func (m *Mat2) Identity() *Mat2 {
return &Mat2{1., 0., 0., 1.}
}
func (m *Mat2) Determinant() float64 {
return m.M11*m.M22 - m.M12*m.M21
}
func (m *Mat2) Negative() *Mat2 {
return &Mat2{-m.M11, -m.M21, -m.M12, -m.M22}
}
func (m *Mat2) Transpose() *Mat2 {
return &Mat2{m.M11, m.M12, m.M21, m.M22}
}
func (m *Mat2) Cofactor() *Mat2 {
return &Mat2{m.M22, -m.M12, -m.M21, m.M11}
}
func (m *Mat2) Adjugate() *Mat2 {
return &Mat2{m.M22, -m.M21, -m.M12, m.M11}
}
func (m *Mat2) Multiply(m1 *Mat2) *Mat2 {
return &Mat2{
m.M11*m1.M11 + m.M12*m1.M21,
m.M21*m1.M11 + m.M22*m1.M21,
m.M11*m1.M12 + m.M12*m1.M22,
m.M21*m1.M12 + m.M22*m1.M22,
}
}
func (m *Mat2) MultiplyScalar(scalar float64) *Mat2 {
return &Mat2{m.M11 * scalar, m.M21 * scalar, m.M12 * scalar, m.M22 * scalar}
}
func (m *Mat2) Inverse() *Mat2 {
det := m.Determinant()
inverse := m.Cofactor()
inverse = inverse.MultiplyScalar(1. / det)
return inverse
}
func (m *Mat2) Scaling(v *Vec2) *Mat2 {
return &Mat2{v.X, m.M21, m.M12, v.Y}
}
func (m *Mat2) Scale(v *Vec2) *Mat2 {
return &Mat2{m.M11 * v.X, m.M21, m.M12, m.M22 * v.Y}
}
func (m *Mat2) RotationZ(angle float64) *Mat2 {
c := math.Cos(angle)
s := math.Sin(angle)
return &Mat2{c, s, -s, c}
}
func (m *Mat2) Lerp(m1 *Mat2, percent float64) *Mat2 {
return &Mat2{
m.M11 + (m1.M11-m.M11)*percent,
m.M21 + (m1.M21-m.M21)*percent,
m.M12 + (m1.M12-m.M12)*percent,
m.M22 + (m1.M22-m.M22)*percent,
}
}