Debian Codegraph @

drawing/matrix.go

raw symbols SCIP
1 package drawing
2
3 import (
4 "math"
5 )
6
7 // Matrix represents an affine transformation
8 type Matrix [6]float64
9
10 const (
11 epsilon = 1e-6
12 )
13
14 // Determinant compute the determinant of the matrix
15 func (tr Matrix) Determinant() float64 {
16 return tr[0]*tr[3] - tr[1]*tr[2]
17 }
18
19 // Transform applies the transformation matrix to points. It modify the points passed in parameter.
20 func (tr Matrix) Transform(points []float64) {
21 for i, j := 0, 1; j < len(points); i, j = i+2, j+2 {
22 x := points[i]
23 y := points[j]
24 points[i] = x*tr[0] + y*tr[2] + tr[4]
25 points[j] = x*tr[1] + y*tr[3] + tr[5]
26 }
27 }
28
29 // TransformPoint applies the transformation matrix to point. It returns the point the transformed point.
30 func (tr Matrix) TransformPoint(x, y float64) (xres, yres float64) {
31 xres = x*tr[0] + y*tr[2] + tr[4]
32 yres = x*tr[1] + y*tr[3] + tr[5]
33 return xres, yres
34 }
35
36 func minMax(x, y float64) (min, max float64) {
37 if x > y {
38 return y, x
39 }
40 return x, y
41 }
42
43 // TransformRectangle applies the transformation matrix to the rectangle represented by the min and the max point of the rectangle
44 func (tr Matrix) TransformRectangle(x0, y0, x2, y2 float64) (nx0, ny0, nx2, ny2 float64) {
45 points := []float64{x0, y0, x2, y0, x2, y2, x0, y2}
46 tr.Transform(points)
47 points[0], points[2] = minMax(points[0], points[2])
48 points[4], points[6] = minMax(points[4], points[6])
49 points[1], points[3] = minMax(points[1], points[3])
50 points[5], points[7] = minMax(points[5], points[7])
51
52 nx0 = math.Min(points[0], points[4])
53 ny0 = math.Min(points[1], points[5])
54 nx2 = math.Max(points[2], points[6])
55 ny2 = math.Max(points[3], points[7])
56 return nx0, ny0, nx2, ny2
57 }
58
59 // InverseTransform applies the transformation inverse matrix to the rectangle represented by the min and the max point of the rectangle
60 func (tr Matrix) InverseTransform(points []float64) {
61 d := tr.Determinant() // matrix determinant
62 for i, j := 0, 1; j < len(points); i, j = i+2, j+2 {
63 x := points[i]
64 y := points[j]
65 points[i] = ((x-tr[4])*tr[3] - (y-tr[5])*tr[2]) / d
66 points[j] = ((y-tr[5])*tr[0] - (x-tr[4])*tr[1]) / d
67 }
68 }
69
70 // InverseTransformPoint applies the transformation inverse matrix to point. It returns the point the transformed point.
71 func (tr Matrix) InverseTransformPoint(x, y float64) (xres, yres float64) {
72 d := tr.Determinant() // matrix determinant
73 xres = ((x-tr[4])*tr[3] - (y-tr[5])*tr[2]) / d
74 yres = ((y-tr[5])*tr[0] - (x-tr[4])*tr[1]) / d
75 return xres, yres
76 }
77
78 // VectorTransform applies the transformation matrix to points without using the translation parameter of the affine matrix.
79 // It modify the points passed in parameter.
80 func (tr Matrix) VectorTransform(points []float64) {
81 for i, j := 0, 1; j < len(points); i, j = i+2, j+2 {
82 x := points[i]
83 y := points[j]
84 points[i] = x*tr[0] + y*tr[2]
85 points[j] = x*tr[1] + y*tr[3]
86 }
87 }
88
89 // NewIdentityMatrix creates an identity transformation matrix.
90 func NewIdentityMatrix() Matrix {
91 return Matrix{1, 0, 0, 1, 0, 0}
92 }
93
94 // NewTranslationMatrix creates a transformation matrix with a translation tx and ty translation parameter
95 func NewTranslationMatrix(tx, ty float64) Matrix {
96 return Matrix{1, 0, 0, 1, tx, ty}
97 }
98
99 // NewScaleMatrix creates a transformation matrix with a sx, sy scale factor
100 func NewScaleMatrix(sx, sy float64) Matrix {
101 return Matrix{sx, 0, 0, sy, 0, 0}
102 }
103
104 // NewRotationMatrix creates a rotation transformation matrix. angle is in radian
105 func NewRotationMatrix(angle float64) Matrix {
106 c := math.Cos(angle)
107 s := math.Sin(angle)
108 return Matrix{c, s, -s, c, 0, 0}
109 }
110
111 // NewMatrixFromRects creates a transformation matrix, combining a scale and a translation, that transform rectangle1 into rectangle2.
112 func NewMatrixFromRects(rectangle1, rectangle2 [4]float64) Matrix {
113 xScale := (rectangle2[2] - rectangle2[0]) / (rectangle1[2] - rectangle1[0])
114 yScale := (rectangle2[3] - rectangle2[1]) / (rectangle1[3] - rectangle1[1])
115 xOffset := rectangle2[0] - (rectangle1[0] * xScale)
116 yOffset := rectangle2[1] - (rectangle1[1] * yScale)
117 return Matrix{xScale, 0, 0, yScale, xOffset, yOffset}
118 }
119
120 // Inverse computes the inverse matrix
121 func (tr *Matrix) Inverse() {
122 d := tr.Determinant() // matrix determinant
123 tr0, tr1, tr2, tr3, tr4, tr5 := tr[0], tr[1], tr[2], tr[3], tr[4], tr[5]
124 tr[0] = tr3 / d
125 tr[1] = -tr1 / d
126 tr[2] = -tr2 / d
127 tr[3] = tr0 / d
128 tr[4] = (tr2*tr5 - tr3*tr4) / d
129 tr[5] = (tr1*tr4 - tr0*tr5) / d
130 }
131
132 // Copy copies the matrix.
133 func (tr Matrix) Copy() Matrix {
134 var result Matrix
135 copy(result[:], tr[:])
136 return result
137 }
138
139 // Compose multiplies trToConcat x tr
140 func (tr *Matrix) Compose(trToCompose Matrix) {
141 tr0, tr1, tr2, tr3, tr4, tr5 := tr[0], tr[1], tr[2], tr[3], tr[4], tr[5]
142 tr[0] = trToCompose[0]*tr0 + trToCompose[1]*tr2
143 tr[1] = trToCompose[1]*tr3 + trToCompose[0]*tr1
144 tr[2] = trToCompose[2]*tr0 + trToCompose[3]*tr2
145 tr[3] = trToCompose[3]*tr3 + trToCompose[2]*tr1
146 tr[4] = trToCompose[4]*tr0 + trToCompose[5]*tr2 + tr4
147 tr[5] = trToCompose[5]*tr3 + trToCompose[4]*tr1 + tr5
148 }
149
150 // Scale adds a scale to the matrix
151 func (tr *Matrix) Scale(sx, sy float64) {
152 tr[0] = sx * tr[0]
153 tr[1] = sx * tr[1]
154 tr[2] = sy * tr[2]
155 tr[3] = sy * tr[3]
156 }
157
158 // Translate adds a translation to the matrix
159 func (tr *Matrix) Translate(tx, ty float64) {
160 tr[4] = tx*tr[0] + ty*tr[2] + tr[4]
161 tr[5] = ty*tr[3] + tx*tr[1] + tr[5]
162 }
163
164 // Rotate adds a rotation to the matrix.
165 func (tr *Matrix) Rotate(radians float64) {
166 c := math.Cos(radians)
167 s := math.Sin(radians)
168 t0 := c*tr[0] + s*tr[2]
169 t1 := s*tr[3] + c*tr[1]
170 t2 := c*tr[2] - s*tr[0]
171 t3 := c*tr[3] - s*tr[1]
172 tr[0] = t0
173 tr[1] = t1
174 tr[2] = t2
175 tr[3] = t3
176 }
177
178 // GetTranslation gets the matrix traslation.
179 func (tr Matrix) GetTranslation() (x, y float64) {
180 return tr[4], tr[5]
181 }
182
183 // GetScaling gets the matrix scaling.
184 func (tr Matrix) GetScaling() (x, y float64) {
185 return tr[0], tr[3]
186 }
187
188 // GetScale computes a scale for the matrix
189 func (tr Matrix) GetScale() float64 {
190 x := 0.707106781*tr[0] + 0.707106781*tr[1]
191 y := 0.707106781*tr[2] + 0.707106781*tr[3]
192 return math.Sqrt(x*x + y*y)
193 }
194
195 // ******************** Testing ********************
196
197 // Equals tests if a two transformation are equal. A tolerance is applied when comparing matrix elements.
198 func (tr Matrix) Equals(tr2 Matrix) bool {
199 for i := 0; i < 6; i = i + 1 {
200 if !fequals(tr[i], tr2[i]) {
201 return false
202 }
203 }
204 return true
205 }
206
207 // IsIdentity tests if a transformation is the identity transformation. A tolerance is applied when comparing matrix elements.
208 func (tr Matrix) IsIdentity() bool {
209 return fequals(tr[4], 0) && fequals(tr[5], 0) && tr.IsTranslation()
210 }
211
212 // IsTranslation tests if a transformation is is a pure translation. A tolerance is applied when comparing matrix elements.
213 func (tr Matrix) IsTranslation() bool {
214 return fequals(tr[0], 1) && fequals(tr[1], 0) && fequals(tr[2], 0) && fequals(tr[3], 1)
215 }
216
217 // fequals compares two floats. return true if the distance between the two floats is less than epsilon, false otherwise
218 func fequals(float1, float2 float64) bool {
219 return math.Abs(float1-float2) <= epsilon
220 }