-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix.go
More file actions
59 lines (53 loc) · 1.23 KB
/
matrix.go
File metadata and controls
59 lines (53 loc) · 1.23 KB
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
package space
import "math"
// Matrix is a transformational matrix for 3D space (3 x 3)
type Matrix [][]float64
// NewRotationMatrixX produces a matrix which will rotate about X
func NewRotationMatrixX(theta float64) Matrix {
sin, cos := math.Sincos(theta)
return Matrix{
{1, 0, 0, 0},
{0, cos, -sin, 0},
{0, sin, cos, 0},
{0, 0, 0, 1},
}
}
// NewRotationMatrixY produces a matrix which will rotate about Y
func NewRotationMatrixY(theta float64) Matrix {
sin, cos := math.Sincos(theta)
return Matrix{
{cos, 0, sin, 0},
{0, 1, 0, 0},
{-sin, 0, cos, 0},
{0, 0, 0, 1},
}
}
// NewRotationMatrixZ produces a matrix which will rotate about Z
func NewRotationMatrixZ(theta float64) Matrix {
sin, cos := math.Sincos(theta)
return Matrix{
{cos, -sin, 0, 0},
{sin, cos, 0, 0},
{0, 0, 1, 0},
{0, 0, 0, 1},
}
}
// Multiply will return the result of m * n
func (m Matrix) Multiply(n Matrix) Matrix {
r := Matrix{
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 1},
}
for rowM := 0; rowM < 4; rowM++ {
for colN := 0; colN < 4; colN++ {
a := m[rowM][0] * n[0][colN]
b := m[rowM][1] * n[1][colN]
c := m[rowM][2] * n[2][colN]
d := m[rowM][3] * n[3][colN]
r[rowM][colN] = a + b + c + d
}
}
return r
}