This repository has been archived by the owner on Jan 26, 2024. It is now read-only.
forked from rustyoz/Mtransform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mtransform.go
80 lines (68 loc) · 1.44 KB
/
mtransform.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
package mtransform
import "math"
type Transform [3][3]float64
func (t *Transform) Apply(x float64, y float64) (float64, float64) {
var X, Y float64
X = t[0][0]*x + t[0][1]*y + t[0][2]
Y = t[1][0]*x + t[1][1]*y + t[1][2]
return X, Y
}
func Identity() Transform {
var t Transform
t[0][0] = 1
t[1][1] = 1
t[2][2] = 1
return t
}
func NewTransform() *Transform {
var t Transform
t = Identity()
return &t
}
func MultiplyTransforms(a Transform, b Transform) Transform {
var t Transform
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
t[i][j] = t[i][j] + a[i][j]*b[j][i]
}
}
return t
}
func (a *Transform) MultiplyWith(b Transform) {
*a = MultiplyTransforms(*a, b)
}
func (t *Transform) Scale(x float64, y float64) {
a := Identity()
a[0][0] = x
a[1][1] = y
t.MultiplyWith(a)
}
func (t *Transform) Translate(x float64, y float64) {
a := Identity()
a[0][2] = x
a[1][2] = y
t.MultiplyWith(a)
}
func (t *Transform) RotateOrigin(angle float64) {
a := Identity()
a[0][0] = math.Cos(angle)
a[0][1] = -math.Sin(angle)
a[1][0] = math.Sin(angle)
a[1][1] = a[0][0]
t.MultiplyWith(a)
}
func (t *Transform) RotatePoint(angle float64, x float64, y float64) {
t.Translate(x, y)
t.RotateOrigin(angle)
t.Translate(-x, -x)
}
func (t *Transform) SkewX(angle float64) {
a := Identity()
a[0][1] = math.Tan(angle)
t.MultiplyWith(a)
}
func (t *Transform) SkewY(angle float64) {
a := Identity()
a[1][0] = math.Tan(angle)
t.MultiplyWith(a)
}