-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathmatrix_test.go
More file actions
277 lines (249 loc) · 7.45 KB
/
matrix_test.go
File metadata and controls
277 lines (249 loc) · 7.45 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Copyright 2010 The draw2d Authors. All rights reserved.
// created: 07/02/2026 by draw2d contributors
package draw2d
import (
"math"
"testing"
)
func TestNewIdentityMatrix(t *testing.T) {
m := NewIdentityMatrix()
if !m.IsIdentity() {
t.Error("NewIdentityMatrix should create an identity matrix")
}
expected := Matrix{1, 0, 0, 1, 0, 0}
if !m.Equals(expected) {
t.Errorf("NewIdentityMatrix = %v, want %v", m, expected)
}
}
func TestNewTranslationMatrix(t *testing.T) {
m := NewTranslationMatrix(5, 10)
x, y := m.GetTranslation()
if !fequals(x, 5) || !fequals(y, 10) {
t.Errorf("GetTranslation() = (%f, %f), want (5, 10)", x, y)
}
if !m.IsTranslation() {
t.Error("Translation matrix should return true for IsTranslation()")
}
}
func TestNewScaleMatrix(t *testing.T) {
m := NewScaleMatrix(2, 3)
sx, sy := m.GetScaling()
if !fequals(sx, 2) || !fequals(sy, 3) {
t.Errorf("GetScaling() = (%f, %f), want (2, 3)", sx, sy)
}
}
func TestNewRotationMatrix(t *testing.T) {
m := NewRotationMatrix(math.Pi / 2)
// Rotating (1,0) by π/2 should give (0,1)
x, y := m.TransformPoint(1, 0)
if !fequals(x, 0) || !fequals(y, 1) {
t.Errorf("Rotating (1,0) by π/2 = (%f, %f), want (0, 1)", x, y)
}
}
func TestMatrixDeterminant(t *testing.T) {
tests := []struct {
name string
m Matrix
want float64
}{
{"identity", NewIdentityMatrix(), 1},
{"scale(2,3)", NewScaleMatrix(2, 3), 6},
{"translation", NewTranslationMatrix(5, 10), 1},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.m.Determinant()
if !fequals(got, tt.want) {
t.Errorf("Determinant() = %f, want %f", got, tt.want)
}
})
}
}
func TestMatrixTransformPoint(t *testing.T) {
m := NewTranslationMatrix(5, 10)
x, y := m.TransformPoint(1, 2)
if !fequals(x, 6) || !fequals(y, 12) {
t.Errorf("TransformPoint(1, 2) = (%f, %f), want (6, 12)", x, y)
}
}
func TestMatrixTransform(t *testing.T) {
m := NewTranslationMatrix(5, 10)
points := []float64{1, 2, 3, 4}
m.Transform(points)
expected := []float64{6, 12, 8, 14}
for i := range points {
if !fequals(points[i], expected[i]) {
t.Errorf("Transform() point[%d] = %f, want %f", i, points[i], expected[i])
}
}
}
func TestMatrixInverseTransformPoint(t *testing.T) {
m := NewTranslationMatrix(5, 10)
x, y := m.InverseTransformPoint(6, 12)
if !fequals(x, 1) || !fequals(y, 2) {
t.Errorf("InverseTransformPoint(6, 12) = (%f, %f), want (1, 2)", x, y)
}
}
func TestMatrixInverseTransform(t *testing.T) {
m := NewTranslationMatrix(5, 10)
points := []float64{6, 12, 8, 14}
m.InverseTransform(points)
expected := []float64{1, 2, 3, 4}
for i := range points {
if !fequals(points[i], expected[i]) {
t.Errorf("InverseTransform() point[%d] = %f, want %f", i, points[i], expected[i])
}
}
}
func TestMatrixInverse(t *testing.T) {
m := NewTranslationMatrix(5, 10)
m.Inverse()
x, y := m.GetTranslation()
if !fequals(x, -5) || !fequals(y, -10) {
t.Errorf("Inverse translation = (%f, %f), want (-5, -10)", x, y)
}
}
func TestMatrixInverse_RoundTrip(t *testing.T) {
m := NewTranslationMatrix(5, 10)
origX, origY := 3.0, 7.0
// Transform
x, y := m.TransformPoint(origX, origY)
// Inverse transform
x2, y2 := m.InverseTransformPoint(x, y)
if !fequals(x2, origX) || !fequals(y2, origY) {
t.Errorf("Round trip: got (%f, %f), want (%f, %f)", x2, y2, origX, origY)
}
}
func TestMatrixCompose(t *testing.T) {
m := NewTranslationMatrix(5, 10)
scale := NewScaleMatrix(2, 3)
m.Compose(scale)
// Composed matrix should scale then translate
x, y := m.TransformPoint(1, 1)
expected_x := 1.0*2 + 5
expected_y := 1.0*3 + 10
if !fequals(x, expected_x) || !fequals(y, expected_y) {
t.Errorf("Compose transform = (%f, %f), want (%f, %f)", x, y, expected_x, expected_y)
}
}
func TestMatrixCopy(t *testing.T) {
m1 := NewTranslationMatrix(5, 10)
m2 := m1.Copy()
// Modify m2
m2.Translate(1, 1)
// m1 should be unchanged
x, y := m1.GetTranslation()
if !fequals(x, 5) || !fequals(y, 10) {
t.Error("Copy should be independent of original")
}
}
func TestMatrixTransformRectangle(t *testing.T) {
m := NewIdentityMatrix()
x0, y0, x2, y2 := m.TransformRectangle(10, 10, 20, 20)
if !fequals(x0, 10) || !fequals(y0, 10) || !fequals(x2, 20) || !fequals(y2, 20) {
t.Errorf("Identity transform rectangle = (%f, %f, %f, %f), want (10, 10, 20, 20)", x0, y0, x2, y2)
}
}
func TestMatrixTransformRectangle_WithScale(t *testing.T) {
m := NewScaleMatrix(2, 3)
x0, y0, x2, y2 := m.TransformRectangle(10, 10, 20, 20)
if !fequals(x0, 20) || !fequals(y0, 30) || !fequals(x2, 40) || !fequals(y2, 60) {
t.Errorf("Scale transform rectangle = (%f, %f, %f, %f), want (20, 30, 40, 60)", x0, y0, x2, y2)
}
}
func TestMatrixVectorTransform(t *testing.T) {
m := NewTranslationMatrix(5, 10)
points := []float64{1, 2}
m.VectorTransform(points)
// Translation should be ignored in VectorTransform
if !fequals(points[0], 1) || !fequals(points[1], 2) {
t.Errorf("VectorTransform should ignore translation: got (%f, %f), want (1, 2)", points[0], points[1])
}
}
func TestMatrixEquals(t *testing.T) {
m1 := NewIdentityMatrix()
m2 := NewIdentityMatrix()
if !m1.Equals(m2) {
t.Error("Two identity matrices should be equal")
}
m3 := NewTranslationMatrix(1, 1)
if m1.Equals(m3) {
t.Error("Identity and translation matrices should not be equal")
}
}
func TestMatrixIsIdentity(t *testing.T) {
m := NewIdentityMatrix()
if !m.IsIdentity() {
t.Error("Identity matrix should return true for IsIdentity()")
}
m.Translate(1, 1)
if m.IsIdentity() {
t.Error("Translated matrix should not be identity")
}
}
func TestMatrixIsTranslation(t *testing.T) {
m := NewTranslationMatrix(5, 10)
if !m.IsTranslation() {
t.Error("Translation matrix should return true for IsTranslation()")
}
m2 := NewScaleMatrix(2, 2)
if m2.IsTranslation() {
t.Error("Scale matrix should not be a translation")
}
}
func TestMatrixScale(t *testing.T) {
m := NewIdentityMatrix()
m.Scale(2, 3)
sx, sy := m.GetScaling()
if !fequals(sx, 2) || !fequals(sy, 3) {
t.Errorf("Scale() result GetScaling() = (%f, %f), want (2, 3)", sx, sy)
}
}
func TestMatrixTranslate(t *testing.T) {
m := NewIdentityMatrix()
m.Translate(5, 10)
x, y := m.GetTranslation()
if !fequals(x, 5) || !fequals(y, 10) {
t.Errorf("Translate() result GetTranslation() = (%f, %f), want (5, 10)", x, y)
}
}
func TestMatrixRotate(t *testing.T) {
m := NewIdentityMatrix()
m.Rotate(math.Pi)
// Rotating (1,0) by π should give (-1,0)
x, y := m.TransformPoint(1, 0)
if !fequals(x, -1) || !fequals(y, 0) {
t.Errorf("Rotate(π) on (1,0) = (%f, %f), want (-1, 0)", x, y)
}
}
func TestNewMatrixFromRects(t *testing.T) {
rect1 := [4]float64{0, 0, 10, 10}
rect2 := [4]float64{0, 0, 20, 20}
m := NewMatrixFromRects(rect1, rect2)
// Midpoint (5,5) of rect1 should map to midpoint (10,10) of rect2
x, y := m.TransformPoint(5, 5)
if !fequals(x, 10) || !fequals(y, 10) {
t.Errorf("NewMatrixFromRects transform midpoint = (%f, %f), want (10, 10)", x, y)
}
}
func TestMatrixGetScale(t *testing.T) {
m := NewIdentityMatrix()
scale := m.GetScale()
if !fequals(scale, 1.0) {
t.Errorf("GetScale() on identity = %f, want ~1.0", scale)
}
}
func TestMatrixGetTranslation(t *testing.T) {
m := NewTranslationMatrix(3, 7)
x, y := m.GetTranslation()
if !fequals(x, 3) || !fequals(y, 7) {
t.Errorf("GetTranslation() = (%f, %f), want (3, 7)", x, y)
}
}
func TestMatrixGetScaling(t *testing.T) {
m := NewScaleMatrix(4, 5)
sx, sy := m.GetScaling()
if !fequals(sx, 4) || !fequals(sy, 5) {
t.Errorf("GetScaling() = (%f, %f), want (4, 5)", sx, sy)
}
}