-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.go
142 lines (118 loc) · 2.77 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
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
140
141
142
// port of https://github.com/fontello/svgpath
package svgpath
import (
"math"
)
// combine 2 matrixes
// m1, m2 - [a, b, c, d, e, g]
//
func combine(m1, m2 []float64) []float64 {
return []float64{
m1[0]*m2[0] + m1[2]*m2[1],
m1[1]*m2[0] + m1[3]*m2[1],
m1[0]*m2[2] + m1[2]*m2[3],
m1[1]*m2[2] + m1[3]*m2[3],
m1[0]*m2[4] + m1[2]*m2[5] + m1[4],
m1[1]*m2[4] + m1[3]*m2[5] + m1[5],
}
}
type Matrix struct {
queue [][]float64
cache []float64
}
func NewMatrix() *Matrix {
return &Matrix{
// list of matrixes to apply
queue: [][]float64{},
cache: nil,
}
}
func (mx *Matrix) Matrix(m []float64) {
if m[0] == 1.0 && m[1] == 0.0 && m[2] == 0.0 && m[3] == 1.0 && m[4] == 0.0 && m[5] == 0.0 {
return
}
mx.queue = append(mx.queue, m)
mx.cache = nil
}
func (mx *Matrix) Translate(tx, ty float64) {
if tx != 0.0 || ty != 0.0 {
mx.queue = append(mx.queue, []float64{1.0, 0.0, 0.0, 1.0, tx, ty})
mx.cache = nil
}
}
func (mx *Matrix) Scale(sx, sy float64) {
if sx != 1.0 || sy != 1.0 {
mx.queue = append(mx.queue, []float64{sx, 0, 0, sy, 0, 0})
mx.cache = nil
}
}
func (mx *Matrix) Rotate(angle, rx, ry float64) {
if angle != 0.0 {
mx.Translate(rx, ry)
rad := angle * math.Pi / 180.0
cos := math.Cos(rad)
sin := math.Sin(rad)
mx.queue = append(mx.queue, []float64{cos, sin, -sin, cos, 0.0, 0.0})
mx.cache = nil
mx.Translate(-rx, -ry)
}
}
func (mx *Matrix) SkewX(angle float64) {
if angle != 0.0 {
mx.queue = append(mx.queue, []float64{1.0, 0.0, math.Tan(angle * math.Pi / 180.0), 1.0, 0.0, 0.0})
mx.cache = nil
}
}
func (mx *Matrix) SkewY(angle float64) {
if angle != 0.0 {
mx.queue = append(mx.queue, []float64{1.0, math.Tan(angle * math.Pi / 180.0), 0.0, 1.0, 0.0, 0.0})
mx.cache = nil
}
}
// Flatten queue
//
func (mx *Matrix) ToArray() []float64 {
if mx.cache != nil {
return mx.cache
}
if len(mx.queue) == 0 {
mx.cache = []float64{1.0, 0.0, 0.0, 1.0, 0.0, 0.0}
return mx.cache
}
mx.cache = mx.queue[0]
if len(mx.queue) == 1 {
return mx.cache
}
for i := 1; i < len(mx.queue); i++ {
mx.cache = combine(mx.cache, mx.queue[i])
}
return mx.cache
}
// Apply list of matrixes to (x,y) point.
// If `isRelative` set, `translate` component of matrix will be skipped
//
func (mx *Matrix) Calc(x float64, y float64, isRelative bool) []float64 {
// Don't change point on empty transforms queue
if len(mx.queue) == 0 {
return []float64{x, y}
}
// Calculate final matrix, if not exists
//
// NB. if you deside to apply transforms to point one-by-one,
// they should be taken in reverse order
if mx.cache == nil {
mx.cache = mx.ToArray()
}
m := mx.cache
// Apply matrix to point
if isRelative {
return []float64{
x*m[0] + y*m[2],
x*m[1] + y*m[3],
}
}
return []float64{
x*m[0] + y*m[2] + m[4],
x*m[1] + y*m[3] + m[5],
}
}