-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.go
89 lines (76 loc) · 1.49 KB
/
matrix.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
86
87
88
89
package vec2
import "math"
type Matrix [6]float64
var Identity Matrix = Matrix{
1, 0, 0,
0, 1, 0,
}
/*
Kaikki seuraavat
palauttavat matriisin, joka ...
*/
// kääntää vastapäivään
func Rotation(a float64) Matrix {
sini := math.Sin(a)
kosini := math.Cos(a)
return Matrix{
kosini, -sini, 0,
sini, kosini, 0,
}
}
// liikuttaa
func Translation(v Vector) Matrix {
return Matrix{
1, 0, v.X,
0, 1, v.Y,
}
}
func Scale(x, y float64) Matrix {
return Matrix{
x, 0, 0,
0, y, 0,
}
}
// multiplies m with v as if v was (x, y, 1)
func (m Matrix) Transform(v Vector) Vector {
return Vector{
m[0]*v.X + m[1]*v.Y + m[2],
m[3]*v.X + m[4]*v.Y + m[5],
}
}
// multiplies m with v as if v was (x, y, 0)
func (m Matrix) TransformDirection(v Vector) Vector {
return Vector{
m[0]*v.X + m[1]*v.Y,
m[3]*v.X + m[4]*v.Y,
}
}
// skalaari kertaa matriisi
func (m Matrix) Mulf(r float64) Matrix {
for i := range m {
m[i] *= r
}
return m
}
// matriisitulo
func (m Matrix) Mul(n Matrix) Matrix {
return Matrix{
m[0]*n[0] + m[1]*n[3], m[0]*n[1] + m[1]*n[4], m[0]*n[2] + m[1]*n[5] + m[2],
m[3]*n[0] + m[4]*n[3], m[3]*n[1] + m[4]*n[4], m[3]*n[2] + m[4]*n[5] + m[5],
}
}
// käänteismatriisi
func (m Matrix) Inverse() Matrix {
return Matrix{
m[4], -m[1], m[1]*m[5] - m[2]*m[4],
-m[3], m[0], m[2]*m[3] - m[0]*m[5],
}.Mulf(1 / (m[0]*m[4] - m[1]*m[3]))
}
// muunna 32-bittiseksi
func (m Matrix) To32() [6]float32 {
n := [6]float32{}
for i, f64 := range m {
n[i] = float32(f64)
}
return n
}