-
Notifications
You must be signed in to change notification settings - Fork 11
/
matrix.go
191 lines (165 loc) · 4.13 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Implements SVG style matrix transformations.
// https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/transform
// Copyright 2018 All rights reserved.
package rasterx
import (
"math"
"golang.org/x/image/math/fixed"
)
// Matrix2D represents an SVG style matrix
type Matrix2D struct {
A, B, C, D, E, F float64
}
// matrix3 is a full 3x3 float64 matrix
// used for inverting
type matrix3 [9]float64
func otherPair(i int) (a, b int) {
switch i {
case 0:
a, b = 1, 2
case 1:
a, b = 0, 2
case 2:
a, b = 0, 1
}
return
}
func (m *matrix3) coFact(i, j int) float64 {
ai, bi := otherPair(i)
aj, bj := otherPair(j)
a, b, c, d := m[ai+aj*3], m[bi+bj*3], m[ai+bj*3], m[bi+aj*3]
return a*b - c*d
}
func (m *matrix3) Invert() *matrix3 {
var cofact matrix3
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
sign := float64(1 - (i+j%2)%2*2) // "checkerboard of minuses" grid
cofact[i+j*3] = m.coFact(i, j) * sign
}
}
deteriminate := m[0]*cofact[0] + m[1]*cofact[1] + m[2]*cofact[2]
// transpose cofact
for i := 0; i < 2; i++ {
for j := i + 1; j < 3; j++ {
cofact[i+j*3], cofact[j+i*3] = cofact[j+i*3], cofact[i+j*3]
}
}
for i := 0; i < 9; i++ {
cofact[i] /= deteriminate
}
return &cofact
}
// Invert returns the inverse matrix
func (a Matrix2D) Invert() Matrix2D {
n := &matrix3{a.A, a.C, a.E, a.B, a.D, a.F, 0, 0, 1}
n = n.Invert()
return Matrix2D{A: n[0], C: n[1], E: n[2], B: n[3], D: n[4], F: n[5]}
}
// Mult returns a*b
func (a Matrix2D) Mult(b Matrix2D) Matrix2D {
return Matrix2D{
A: a.A*b.A + a.C*b.B,
B: a.B*b.A + a.D*b.B,
C: a.A*b.C + a.C*b.D,
D: a.B*b.C + a.D*b.D,
E: a.A*b.E + a.C*b.F + a.E,
F: a.B*b.E + a.D*b.F + a.F}
}
// Identity is the identity matrix
var Identity = Matrix2D{1, 0, 0, 1, 0, 0}
// TFixed transforms a fixed.Point26_6 by the matrix
func (a Matrix2D) TFixed(x fixed.Point26_6) (y fixed.Point26_6) {
y.X = fixed.Int26_6((float64(x.X)*a.A + float64(x.Y)*a.C) + a.E*64)
y.Y = fixed.Int26_6((float64(x.X)*a.B + float64(x.Y)*a.D) + a.F*64)
return
}
// Transform multiples the input vector by matrix m and outputs the results vector
// components.
func (a Matrix2D) Transform(x1, y1 float64) (x2, y2 float64) {
x2 = x1*a.A + y1*a.C + a.E
y2 = x1*a.B + y1*a.D + a.F
return
}
// TransformVector is a modidifed version of Transform that ignores the
// translation components.
func (a Matrix2D) TransformVector(x1, y1 float64) (x2, y2 float64) {
x2 = x1*a.A + y1*a.C
y2 = x1*a.B + y1*a.D
return
}
//Scale matrix in x and y dimensions
func (a Matrix2D) Scale(x, y float64) Matrix2D {
return a.Mult(Matrix2D{
A: x,
B: 0,
C: 0,
D: y,
E: 0,
F: 0})
}
//SkewY skews the matrix in the Y dimension
func (a Matrix2D) SkewY(theta float64) Matrix2D {
return a.Mult(Matrix2D{
A: 1,
B: math.Tan(theta),
C: 0,
D: 1,
E: 0,
F: 0})
}
//SkewX skews the matrix in the X dimension
func (a Matrix2D) SkewX(theta float64) Matrix2D {
return a.Mult(Matrix2D{
A: 1,
B: 0,
C: math.Tan(theta),
D: 1,
E: 0,
F: 0})
}
//Translate translates the matrix to the x , y point
func (a Matrix2D) Translate(x, y float64) Matrix2D {
return a.Mult(Matrix2D{
A: 1,
B: 0,
C: 0,
D: 1,
E: x,
F: y})
}
//Rotate rotate the matrix by theta
func (a Matrix2D) Rotate(theta float64) Matrix2D {
return a.Mult(Matrix2D{
A: math.Cos(theta),
B: math.Sin(theta),
C: -math.Sin(theta),
D: math.Cos(theta),
E: 0,
F: 0})
}
// MatrixAdder is an adder that applies matrix M to all points
type MatrixAdder struct {
Adder
M Matrix2D
}
// Reset sets the matrix M to identity
func (t *MatrixAdder) Reset() {
t.M = Identity
}
// Start starts a new path
func (t *MatrixAdder) Start(a fixed.Point26_6) {
t.Adder.Start(t.M.TFixed(a))
}
// Line adds a linear segment to the current curve.
func (t *MatrixAdder) Line(b fixed.Point26_6) {
t.Adder.Line(t.M.TFixed(b))
}
// QuadBezier adds a quadratic segment to the current curve.
func (t *MatrixAdder) QuadBezier(b, c fixed.Point26_6) {
t.Adder.QuadBezier(t.M.TFixed(b), t.M.TFixed(c))
}
// CubeBezier adds a cubic segment to the current curve.
func (t *MatrixAdder) CubeBezier(b, c, d fixed.Point26_6) {
t.Adder.CubeBezier(t.M.TFixed(b), t.M.TFixed(c), t.M.TFixed(d))
}