-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix_2d_extensions_spec.rb
399 lines (365 loc) · 15.5 KB
/
matrix_2d_extensions_spec.rb
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
require './matrix_2d_extensions'
EPS = 0.000001
RSpec::Matchers.define :be_like_really_close_to do |expected|
match do |actual|
actual.row_count == expected.row_count &&
actual.column_count == expected.column_count &&
(actual-expected).each.reduce(&:+).abs < EPS
end
end
points = [
p(0,0),
p(1,2),
p(-4234,-223423),
p(231,-123),
p(-213,345),
p(234.789,68.4456),
]
scalars = [0, 1, -1, -1324, 5768, 0.5, 0.001, 345.6745, -245.4352]
describe 'p' do
it 'creates matrices like [[x, y, 0]]' do
expect(p(0,0)).to(be_like_really_close_to(Matrix[[0], [0], [1]]))
expect(p(1,0)).to(be_like_really_close_to(Matrix[[1], [0], [1]]))
expect(p(0,1)).to(be_like_really_close_to(Matrix[[0], [1], [1]]))
expect(p(1,1)).to(be_like_really_close_to(Matrix[[1], [1], [1]]))
expect(p(-1,-1)).to(be_like_really_close_to(Matrix[[-1], [-1], [1]]))
expect(p(0.7226,1.5234)).to(be_like_really_close_to(Matrix[[0.7226], [1.5234], [1]]))
end
it 'fails our custom matcher when called with different values' do
expect(p(0,0)).to_not(be_like_really_close_to(Matrix[[2], [0], [1]]))
expect(p(0,0)).to_not(be_like_really_close_to(Matrix[[-2], [0], [1]]))
end
end
describe 'Matrix' do
describe '#to2d_s' do
it 'prints points with 3 decimal digits precision' do
expect(p(0,0).to2d_s).to(eq('0 0'))
expect(p(1,2).to2d_s).to(eq('1 2'))
expect(p(2.5,3.4).to2d_s).to(eq('2.5 3.4'))
expect(p(2.5324,3.42352).to2d_s).to(eq('2.532 3.424'))
expect(p(1,-2).to2d_s).to(eq('1 -2'))
end
end
describe '#angle2d' do
it 'returns the angle in the direction of the other point' do
expect(p(0,0).angle2d(p(1,0))).to(eq(0))
expect(p(0,0).angle2d(p(4,0))).to(eq(0))
expect(p(3,0).angle2d(p(4,0))).to(eq(0))
expect(p(3.45,10.45).angle2d(p(4.45,10.45))).to(eq(0))
expect(p(0,0).angle2d(p(-1,0))).to(eq(Math::PI))
expect(p(0,0).angle2d(p(-4,0))).to(eq(Math::PI))
expect(p(3,0).angle2d(p(-4,0))).to(eq(Math::PI))
expect(p(3.45,10.45).angle2d(p(-4.45,10.45))).to(eq(Math::PI))
expect(p(0,0).angle2d(p(0,1))).to(eq(Math::PI/2))
expect(p(0,0).angle2d(p(0,4))).to(eq(Math::PI/2))
expect(p(0,3).angle2d(p(0,4))).to(eq(Math::PI/2))
expect(p(10.45,3.45).angle2d(p(10.45,4.45))).to(eq(Math::PI/2))
expect(p(0,0).angle2d(p(0,-1))).to(eq(-Math::PI/2))
expect(p(0,0).angle2d(p(0,-4))).to(eq(-Math::PI/2))
expect(p(0,3).angle2d(p(0,-4))).to(eq(-Math::PI/2))
expect(p(10.45,3.45).angle2d(p(10.45,-4.45))).to(eq(-Math::PI/2))
expect(p(0,0).angle2d(p(1,1))).to(be_within(EPS).of(Math::PI/4))
expect(p(0,0).angle2d(p(4,4))).to(be_within(EPS).of(Math::PI/4))
expect(p(0,3).angle2d(p(4,7))).to(be_within(EPS).of(Math::PI/4))
expect(p(10.45,3.45).angle2d(p(11.45,4.45))).to(be_within(EPS).of(Math::PI/4))
expect(p(0,0).angle2d(p(1,-1))).to(be_within(EPS).of(-Math::PI/4))
expect(p(0,0).angle2d(p(4,-4))).to(be_within(EPS).of(-Math::PI/4))
expect(p(0,-3).angle2d(p(4,-7))).to(be_within(EPS).of(-Math::PI/4))
expect(p(0,3).angle2d(p(4,-1))).to(be_within(EPS).of(-Math::PI/4))
expect(p(10.45,3.45).angle2d(p(11.45,2.45))).to(be_within(EPS).of(-Math::PI/4))
end
end
describe '.rotation2d' do
it 'returns the identity matrix for multiples of 2π' do
expect(Matrix.rotation2d(0)).to(be_like_really_close_to(Matrix.identity(3)))
expect(Matrix.rotation2d(2*Math::PI)).to(be_like_really_close_to(Matrix.identity(3)))
expect(Matrix.rotation2d(-2*Math::PI)).to(be_like_really_close_to(Matrix.identity(3)))
expect(Matrix.rotation2d(4*Math::PI)).to(be_like_really_close_to(Matrix.identity(3)))
end
it 'returns a good rotation matrix for π' do
expect(Matrix.rotation2d(Math::PI)).to(be_like_really_close_to(Matrix[[-1, 0, 0],[0, -1, 0],[0, 0, 1]]))
end
end
describe '.tranlsation2d' do
it 'returns the indentity matrix for zeros' do
expect(Matrix.tranlsation2d(0 , 0)).to(be_like_really_close_to(Matrix.identity(3)))
end
it 'returns a translation matrix' do
expect(Matrix.tranlsation2d(3, 4)).to(be_like_really_close_to(Matrix[[1, 0, 0],[0, 1, 0],[3, 4, 1]]))
end
end
describe '.reflection2d' do
[0, Math::PI, -Math::PI, 2*Math::PI].each do |r|
context "along the X axis (r=#{r/Math::PI}π)" do
it 'correctly reflects' do
reflection = Matrix.reflection2d(r)
expect(reflection*p(0,0)).to(be_like_really_close_to(p(0,0)))
expect(reflection*p(1,0)).to(be_like_really_close_to(p(1,0)))
expect(reflection*p(-1,0)).to(be_like_really_close_to(p(-1,0)))
expect(reflection*p(0,1)).to(be_like_really_close_to(p(0,-1)))
expect(reflection*p(0,-1)).to(be_like_really_close_to(p(0,1)))
expect(reflection*p(1,1)).to(be_like_really_close_to(p(1,-1)))
expect(reflection*p(732.645,83.94)).to(be_like_really_close_to(p(732.645,-83.94)))
end
end
end
[Math::PI/2, -Math::PI/2, 3*Math::PI/2, -3*Math::PI/2].each do |r|
context "along the Y axis (r=#{r/Math::PI}π)" do
it 'correctly reflects' do
reflection = Matrix.reflection2d(r)
expect(reflection*p(0,0)).to(be_like_really_close_to(p(0,0)))
expect(reflection*p(1,0)).to(be_like_really_close_to(p(-1,0)))
expect(reflection*p(-1,0)).to(be_like_really_close_to(p(1,0)))
expect(reflection*p(0,1)).to(be_like_really_close_to(p(0,1)))
expect(reflection*p(0,-1)).to(be_like_really_close_to(p(0,-1)))
expect(reflection*p(1,1)).to(be_like_really_close_to(p(-1,1)))
expect(reflection*p(732.645,83.94)).to(be_like_really_close_to(p(-732.645,83.94)))
end
end
end
[Math::PI/4, 5*Math::PI/4, -3*Math::PI/4].each do |r|
context "along the diagonal (r=#{r/Math::PI}π)" do
it 'correctly reflects' do
reflection = Matrix.reflection2d(r)
expect(reflection*p(0,0)).to(be_like_really_close_to(p(0,0)))
expect(reflection*p(1,0)).to(be_like_really_close_to(p(0,1)))
expect(reflection*p(-1,0)).to(be_like_really_close_to(p(0,-1)))
expect(reflection*p(0,1)).to(be_like_really_close_to(p(1,0)))
expect(reflection*p(0,-1)).to(be_like_really_close_to(p(-1,0)))
expect(reflection*p(1,1)).to(be_like_really_close_to(p(1,1)))
expect(reflection*p(732.645,83.94)).to(be_like_really_close_to(p(83.94,732.645)))
end
end
end
[3*Math::PI/4, 7*Math::PI/4, -Math::PI/4].each do |r|
context "along the other diagonal (r=#{r/Math::PI}π)" do
it 'correctly reflects' do
reflection = Matrix.reflection2d(r)
expect(reflection*p(0,0)).to(be_like_really_close_to(p(0,0)))
expect(reflection*p(1,0)).to(be_like_really_close_to(p(-1,0)))
expect(reflection*p(-1,0)).to(be_like_really_close_to(p(0,1)))
expect(reflection*p(0,1)).to(be_like_really_close_to(p(-1,0)))
expect(reflection*p(0,-1)).to(be_like_really_close_to(p(1,0)))
expect(reflection*p(1,1)).to(be_like_really_close_to(p(-1,-1)))
expect(reflection*p(732.645,83.94)).to(be_like_really_close_to(p(-83.94,-732.645)))
end
end
end
end
describe '.reflection2dp' do
it 'enjoys watching a little rummy-tummy on the Discovery Channel' do
reflection = Matrix.reflection2dp(p(3,1), p(3,5))
expect(reflection*p(1,3)).to(be_like_really_close_to(p(5,3)))
expect(reflection*p(3,1)).to(be_like_really_close_to(p(3,1)))
expect(reflection*p(3,5)).to(be_like_really_close_to(p(3,5)))
end
it 'peels the banana from the other side' do
reflection = Matrix.reflection2dp(p(1,3), p(5,3))
expect(reflection*p(3,1)).to(be_like_really_close_to(p(3,5)))
expect(reflection*p(1,3)).to(be_like_really_close_to(p(1,3)))
expect(reflection*p(5,3)).to(be_like_really_close_to(p(5,3)))
end
it 'opens a can of beer in the middle of the night' do
reflection = Matrix.reflection2dp(p(2,2), p(3,3))
expect(reflection*p(2,2)).to(be_like_really_close_to(p(2,2)))
expect(reflection*p(5,5)).to(be_like_really_close_to(p(5,5)))
expect(reflection*p(4,3)).to(be_like_really_close_to(p(3,4)))
expect(reflection*p(12,19)).to(be_like_really_close_to(p(19,12)))
end
it 'does what it has to do to supply for its children' do
reflection = Matrix.reflection2dp(p(2,2), p(3,1))
expect(reflection*p(2,2)).to(be_like_really_close_to(p(2,2)))
expect(reflection*p(5,-1)).to(be_like_really_close_to(p(5,-1)))
expect(reflection*p(4,3)).to(be_like_really_close_to(p(1,0)))
expect(reflection*p(-1,2)).to(be_like_really_close_to(p(2,5)))
expect(reflection*p(5,5)).to(be_like_really_close_to(p(-1,-1)))
end
end
describe '.scale2d' do
context "returns a scale matrix" do
scalars.each do |factor|
it "for factor #{factor}" do
expect(Matrix.scale2d(factor)).to(be_like_really_close_to(Matrix[[factor, 0, 0],[0, factor, 0],[0, 0, 1]]))
end
end
end
it "returns a indentity matrix for factor 1" do
expect(Matrix.scale2d(1)).to(be_like_really_close_to(Matrix.identity(3)))
end
end
describe '.point2d' do
it 'creates matrices like [[x, y, 0]]' do
expect(Matrix.point2d(0,0)).to(be_like_really_close_to(Matrix[[0], [0], [1]]))
expect(Matrix.point2d(1,0)).to(be_like_really_close_to(Matrix[[1], [0], [1]]))
expect(Matrix.point2d(0,1)).to(be_like_really_close_to(Matrix[[0], [1], [1]]))
expect(Matrix.point2d(1,1)).to(be_like_really_close_to(Matrix[[1], [1], [1]]))
expect(Matrix.point2d(-1,-1)).to(be_like_really_close_to(Matrix[[-1], [-1], [1]]))
expect(Matrix.point2d(0.7226,1.5234)).to(be_like_really_close_to(Matrix[[0.7226], [1.5234], [1]]))
end
end
describe '#x' do
it 'returns back the first argument to p' do
scalars.each do |x|
scalars.each do |y|
expect(p(x,y).x).to(be_within(EPS).of(x))
end
end
end
end
describe '#y' do
it 'returns back the second argument to p' do
scalars.each do |x|
scalars.each do |y|
expect(p(x,y).y).to(be_within(EPS).of(y))
end
end
end
end
describe '#to_tranlsation2d' do
it 'returns the indentity matrix for p(0, 0)' do
expect(p(0, 0).to_tranlsation2d).to(be_like_really_close_to(Matrix.identity(3)))
end
it 'returns a translation matrix' do
expect(p(3, 4).to_tranlsation2d).to(be_like_really_close_to(Matrix[[1, 0, 0],[0, 1, 0],[3, 4, 1]]))
end
end
describe '#translate2d' do
it 'basically sums up points' do
expect(p(0, 0).translate2d(p(0, 0))).to(be_like_really_close_to(p(0, 0)))
expect(p(3, 2).translate2d(p(7, 11))).to(be_like_really_close_to(p(10, 13)))
expect(p(-435.905, 543.43).translate2d(p(345.43, -487.456))).to(be_like_really_close_to(p(-90.475, 55.974)))
end
end
describe '#rotate2d' do
context "without center" do
context 'rotation by 0 is identity' do
points.each do |point|
it "for #{point}" do
expect(point.rotate2d(0)).to(be_like_really_close_to(point))
end
end
end
context 'p(0,0) is not changed' do
scalars.each do |angle|
it "for #{angle}" do
expect(p(0, 0).rotate2d(angle)).to(be_like_really_close_to(p(0, 0)))
end
end
end
it 'rotates correctly' do
expect(p(3, 4).rotate2d(Math::PI)).to(be_like_really_close_to(p(-3, -4)))
end
end
context "with center" do
points.each do |center|
context "set to #{center}" do
context 'rotation by 0 is identity' do
points.each do |point|
it "for #{point}" do
expect(point.rotate2d(0, center)).to(be_like_really_close_to(point))
end
end
end
context 'rotation around itself is identity' do
points.each do |point|
context "for point #{point}" do
scalars.each do |angle|
it "for #{angle}" do
expect(point.rotate2d(angle, point)).to(be_like_really_close_to(point))
end
end
end
end
end
end
end
end
it 'rotates correctly' do
expect(p(3, 4).rotate2d(Math::PI, p(7, 9))).to(be_like_really_close_to(p(11, 14)))
expect(p(2, 1).rotate2d(Math::PI/6.0, p(1, 1))).to(be_like_really_close_to(p(Math.sqrt(3)/2 + 1, 1.5)))
end
end
describe '#scale2d' do
context "without center" do
context 'scaling by 0 leads to p(0, 0)' do
points.each do |point|
it "for #{point}" do
expect(point.scale2d(0)).to(be_like_really_close_to(p(0, 0)))
end
end
end
context 'scaling by 1 is identity' do
points.each do |point|
it "for #{point}" do
expect(point.scale2d(1)).to(be_like_really_close_to(point))
end
end
end
context 'p(0, 0) is not changed' do
scalars.each do |factor|
it "for #{factor}" do
expect(p(0, 0).scale2d(factor)).to(be_like_really_close_to(p(0, 0)))
end
end
end
it 'scales up points, duh' do
expect(p(3, 2).scale2d(2)).to(be_like_really_close_to(p(6, 4)))
expect(p(3, 2).scale2d(0.5)).to(be_like_really_close_to(p(1.5, 1)))
end
end
context "with center" do
points.each do |center|
context "set to #{center}" do
context "scaling by 0 leads to center" do
points.each do |point|
it "for #{point}" do
expect(point.scale2d(0, center)).to(be_like_really_close_to(center))
end
end
end
context 'scaling by 1 is identity' do
points.each do |point|
it "for #{point}" do
expect(point.scale2d(1, center)).to(be_like_really_close_to(point))
end
end
end
context 'center is not changed' do
scalars.each do |factor|
it "for #{factor}" do
expect(center.scale2d(factor, center)).to(be_like_really_close_to(center))
end
end
end
end
end
it 'scales up points, duh' do
expect(p(0, 0).scale2d(3, p(1,1))).to(be_like_really_close_to(p(-2, -2)))
expect(p(3, 2).scale2d(2, p(0, 0))).to(be_like_really_close_to(p(6, 4)))
expect(p(3, 2).scale2d(0.5, p(9, 9))).to(be_like_really_close_to(p(6, 5.5)))
end
end
end
describe '#distance2d' do
it 'returns the disctance betweend two points' do
expect(p(0, 0).distance2d(p(1, 0))).to(be_within(EPS).of(1))
expect(p(0, 0).distance2d(p(0, 1))).to(be_within(EPS).of(1))
expect(p(0, 0).distance2d(p(-1, 0))).to(be_within(EPS).of(1))
expect(p(0, 0).distance2d(p(0, -1))).to(be_within(EPS).of(1))
expect(p(0, 0).distance2d(p(3, 4))).to(be_within(EPS).of(5))
expect(p(0, 0).distance2d(p(-3, 4))).to(be_within(EPS).of(5))
expect(p(0, 0).distance2d(p(3, -4))).to(be_within(EPS).of(5))
expect(p(0, 0).distance2d(p(-3, -4))).to(be_within(EPS).of(5))
expect(p(3, 2).distance2d(p(6, 6))).to(be_within(EPS).of(5))
expect(p(3, 2).distance2d(p(7, 5))).to(be_within(EPS).of(5))
expect(p(3, 2).distance2d(p(-1, -1))).to(be_within(EPS).of(5))
end
context 'distance to itself is 0' do
points.each do |point|
it "for #{point}" do
expect(point.distance2d(point)).to(be_within(EPS).of(0))
end
end
end
end
end