Debian Codegraph @

closure-library/closure/goog/math/rect_test.js

raw
1 // Copyright 2006 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 goog.provide('goog.math.RectTest');
16 goog.setTestOnly('goog.math.RectTest');
17
18 goog.require('goog.math.Box');
19 goog.require('goog.math.Coordinate');
20 goog.require('goog.math.Rect');
21 goog.require('goog.math.Size');
22 goog.require('goog.testing.jsunit');
23
24
25 /**
26 * Produce legible assertion results. If two rects are not equal, the error
27 * message will be of the form
28 * "Expected <(1, 2 - 10 x 10)> (Object) but was <(3, 4 - 20 x 20)> (Object)"
29 */
30 function assertRectsEqual(expected, actual) {
31 if (!goog.math.Rect.equals(expected, actual)) {
32 assertEquals(expected, actual);
33 }
34 }
35
36 /**
37 * Create a goog.math.Rect with given coordinates.
38 *
39 * @param {Array<number>} a Array of numbers with given coordinates for rect,
40 * with expected order [x1, y1, x2, y2].
41 * @return {?goog.math.Rect} A rectangle, if coordinates were given.
42 */
43 function createRect(a) {
44 return a ? new goog.math.Rect(a[0], a[1], a[2] - a[0], a[3] - a[1]) : null;
45 }
46
47 /**
48 * Create a rect like object.
49 *
50 * @param {Array<number>} a Array of numbers with given coordinates for rect,
51 * with expected order [x1, y1, x2, y2].
52 * @return {?{left: number, top: number, width: number, height: number}} A rect
53 * like object.
54 */
55 function createIRect(a) {
56 if (a) {
57 return {left: a[0], top: a[1], width: a[2] - a[0], height: a[3] - a[1]};
58 }
59 return null;
60 }
61
62 function testRectClone() {
63 var r = new goog.math.Rect(0, 0, 0, 0);
64 assertRectsEqual(r, r.clone());
65 r.left = -10;
66 r.top = -20;
67 r.width = 10;
68 r.height = 20;
69 assertRectsEqual(r, r.clone());
70 }
71
72 function testRectIntersection() {
73 var tests = [
74 [[10, 10, 20, 20], [15, 15, 25, 25], [15, 15, 20, 20]],
75 [[10, 10, 20, 20], [20, 0, 30, 10], [20, 10, 20, 10]],
76 [[0, 0, 1, 1], [10, 11, 12, 13], null],
77 [[11, 12, 98, 99], [22, 23, 34, 35], [22, 23, 34, 35]]
78 ];
79
80 var intersectTest = function(r0, r1, expected) {
81 assertRectsEqual(expected, goog.math.Rect.intersection(r0, r1));
82 assertRectsEqual(expected, goog.math.Rect.intersection(r1, r0));
83
84 // Test in place methods.
85 var clone = r0.clone();
86
87 assertRectsEqual(expected, clone.intersection(r1) ? clone : null);
88
89 if (r1.intersection) {
90 assertRectsEqual(expected, r1.intersection(r0) ? r1 : null);
91 }
92 };
93
94 for (var i = 0; i < tests.length; ++i) {
95 var t = tests[i];
96 var r0 = createRect(t[0]);
97 var r1 = createRect(t[1]);
98
99 var expected = createRect(t[2]);
100
101 intersectTest(r0, r1, expected);
102 }
103
104 // Run same tests with IRects.
105
106 for (var i = 0; i < tests.length; ++i) {
107 var t = tests[i];
108 var r0 = createRect(t[0]);
109 var r1 = createIRect(t[1]);
110
111 var expected = createRect(t[2]);
112
113 intersectTest(r0, r1, expected);
114 }
115 }
116
117 function testRectIntersects() {
118 var r0 = createRect([10, 10, 20, 20]);
119 var r1 = createRect([15, 15, 25, 25]);
120 var r2 = createRect([0, 0, 1, 1]);
121 var ri0 = createIRect([10, 10, 20, 20]);
122 var ri1 = createIRect([15, 15, 25, 25]);
123 var ri2 = createIRect([0, 0, 1, 1]);
124
125 assertTrue(goog.math.Rect.intersects(r0, r1));
126 assertTrue(goog.math.Rect.intersects(r1, r0));
127 assertTrue(r0.intersects(r1));
128 assertTrue(r1.intersects(r0));
129
130 assertTrue(goog.math.Rect.intersects(r0, ri1));
131 assertTrue(goog.math.Rect.intersects(ri0, ri1));
132 assertTrue(goog.math.Rect.intersects(ri1, r0));
133 assertTrue(r0.intersects(ri1));
134
135 assertFalse(goog.math.Rect.intersects(r0, r2));
136 assertFalse(goog.math.Rect.intersects(r2, r0));
137 assertFalse(r0.intersects(r2));
138 assertFalse(r2.intersects(r0));
139
140 assertFalse(goog.math.Rect.intersects(ri0, ri2));
141 assertFalse(goog.math.Rect.intersects(ri2, ri0));
142 assertFalse(r0.intersects(ri2));
143 assertFalse(r2.intersects(ri0));
144 }
145
146 function testRectBoundingRect() {
147 var tests = [
148 [[10, 10, 20, 20], [15, 15, 25, 25], [10, 10, 25, 25]],
149 [[10, 10, 20, 20], [20, 0, 30, 10], [10, 0, 30, 20]],
150 [[0, 0, 1, 1], [10, 11, 12, 13], [0, 0, 12, 13]],
151 [[11, 12, 98, 99], [22, 23, 34, 35], [11, 12, 98, 99]]
152 ];
153 for (var i = 0; i < tests.length; ++i) {
154 var t = tests[i];
155 var r0 = createRect(t[0]);
156 var r1 = createRect(t[1]);
157 var expected = createRect(t[2]);
158 assertRectsEqual(expected, goog.math.Rect.boundingRect(r0, r1));
159 assertRectsEqual(expected, goog.math.Rect.boundingRect(r1, r0));
160
161 // Test in place methods.
162 var clone = r0.clone();
163
164 clone.boundingRect(r1);
165 assertRectsEqual(expected, clone);
166
167 r1.boundingRect(r0);
168 assertRectsEqual(expected, r1);
169 }
170 }
171
172 function testRectDifference() {
173 // B is the same as A.
174 assertDifference([10, 10, 20, 20], [10, 10, 20, 20], []);
175 // B does not touch A.
176 assertDifference([10, 10, 20, 20], [0, 0, 5, 5], [[10, 10, 20, 20]]);
177 // B overlaps top half of A.
178 assertDifference([10, 10, 20, 20], [5, 15, 25, 25], [[10, 10, 20, 15]]);
179 // B overlaps bottom half of A.
180 assertDifference([10, 10, 20, 20], [5, 5, 25, 15], [[10, 15, 20, 20]]);
181 // B overlaps right half of A.
182 assertDifference([10, 10, 20, 20], [15, 5, 25, 25], [[10, 10, 15, 20]]);
183 // B overlaps left half of A.
184 assertDifference([10, 10, 20, 20], [5, 5, 15, 25], [[15, 10, 20, 20]]);
185 // B touches A at its bottom right corner
186 assertDifference([10, 10, 20, 20], [20, 20, 30, 30], [[10, 10, 20, 20]]);
187 // B touches A at its top left corner
188 assertDifference([10, 10, 20, 20], [5, 5, 10, 10], [[10, 10, 20, 20]]);
189 // B touches A along its bottom edge
190 assertDifference([10, 10, 20, 20], [12, 20, 17, 25], [[10, 10, 20, 20]]);
191 // B splits A horizontally.
192 assertDifference(
193 [10, 10, 20, 20], [5, 12, 25, 18], [[10, 10, 20, 12], [10, 18, 20, 20]]);
194 // B splits A vertically.
195 assertDifference(
196 [10, 10, 20, 20], [12, 5, 18, 25], [[10, 10, 12, 20], [18, 10, 20, 20]]);
197 // B subtracts a notch from the top of A.
198 assertDifference(
199 [10, 10, 20, 20], [12, 5, 18, 15],
200 [[10, 15, 20, 20], [10, 10, 12, 15], [18, 10, 20, 15]]);
201 // B subtracts a notch from the bottom left of A
202 assertDifference([1, 6, 3, 9], [1, 7, 2, 9], [[1, 6, 3, 7], [2, 7, 3, 9]]);
203 // B subtracts a notch from the bottom right of A
204 assertDifference([1, 6, 3, 9], [2, 7, 3, 9], [[1, 6, 3, 7], [1, 7, 2, 9]]);
205 // B subtracts a notch from the top left of A
206 assertDifference([1, 6, 3, 9], [1, 6, 2, 8], [[1, 8, 3, 9], [2, 6, 3, 8]]);
207 // B subtracts a notch from the top left of A (no coinciding edge)
208 assertDifference([1, 6, 3, 9], [0, 5, 2, 8], [[1, 8, 3, 9], [2, 6, 3, 8]]);
209 // B subtracts a hole from the center of A.
210 assertDifference([-20, -20, -10, -10], [-18, -18, -12, -12], [
211 [-20, -20, -10, -18], [-20, -12, -10, -10], [-20, -18, -18, -12],
212 [-12, -18, -10, -12]
213 ]);
214 }
215
216 function assertDifference(a, b, expected) {
217 var r0 = createRect(a);
218 var r1 = createRect(b);
219 var diff = goog.math.Rect.difference(r0, r1);
220
221 assertEquals(
222 'Wrong number of rectangles in difference ', expected.length,
223 diff.length);
224
225 for (var j = 0; j < expected.length; ++j) {
226 var e = createRect(expected[j]);
227 if (!goog.math.Rect.equals(e, diff[j])) {
228 alert(j + ': ' + e + ' != ' + diff[j]);
229 }
230 assertRectsEqual(e, diff[j]);
231 }
232
233 // Test in place version
234 var diff = r0.difference(r1);
235
236 assertEquals(
237 'Wrong number of rectangles in in-place difference ', expected.length,
238 diff.length);
239
240 for (var j = 0; j < expected.length; ++j) {
241 var e = createRect(expected[j]);
242 if (!goog.math.Rect.equals(e, diff[j])) {
243 alert(j + ': ' + e + ' != ' + diff[j]);
244 }
245 assertRectsEqual(e, diff[j]);
246 }
247 }
248
249 function testRectToBox() {
250 var r = new goog.math.Rect(0, 0, 0, 0);
251 assertObjectEquals(new goog.math.Box(0, 0, 0, 0), r.toBox());
252
253 r.top = 10;
254 r.left = 10;
255 r.width = 20;
256 r.height = 20;
257 assertObjectEquals(new goog.math.Box(10, 30, 30, 10), r.toBox());
258
259 r.top = -10;
260 r.left = 0;
261 r.width = 10;
262 r.height = 10;
263 assertObjectEquals(new goog.math.Box(-10, 10, 0, 0), r.toBox());
264 }
265
266 function testBoxToRect() {
267 var box = new goog.math.Box(0, 0, 0, 0);
268 assertObjectEquals(
269 new goog.math.Rect(0, 0, 0, 0), goog.math.Rect.createFromBox(box));
270
271 box.top = 10;
272 box.left = 15;
273 box.right = 23;
274 box.bottom = 27;
275 assertObjectEquals(
276 new goog.math.Rect(15, 10, 8, 17), goog.math.Rect.createFromBox(box));
277
278 box.top = -10;
279 box.left = 3;
280 box.right = 12;
281 box.bottom = 7;
282 assertObjectEquals(
283 new goog.math.Rect(3, -10, 9, 17), goog.math.Rect.createFromBox(box));
284 }
285
286 function testBoxToRectAndBack() {
287 rectToBoxAndBackTest(new goog.math.Rect(8, 11, 20, 23));
288 rectToBoxAndBackTest(new goog.math.Rect(9, 13, NaN, NaN));
289 rectToBoxAndBackTest(new goog.math.Rect(10, 13, NaN, 21));
290 rectToBoxAndBackTest(new goog.math.Rect(5, 7, 14, NaN));
291 }
292
293 function rectToBoxAndBackTest(rect) {
294 var box = rect.toBox();
295 var rect2 = goog.math.Rect.createFromBox(box);
296
297 // Use toString for this test since otherwise NaN != NaN.
298 assertObjectEquals(rect.toString(), rect2.toString());
299 }
300
301 function testRectToBoxAndBack() {
302 // This doesn't work if left or top is undefined.
303 boxToRectAndBackTest(new goog.math.Box(11, 13, 20, 17));
304 boxToRectAndBackTest(new goog.math.Box(10, NaN, NaN, 11));
305 boxToRectAndBackTest(new goog.math.Box(9, 14, NaN, 11));
306 boxToRectAndBackTest(new goog.math.Box(10, NaN, 22, 15));
307 }
308
309 function boxToRectAndBackTest(box) {
310 var rect = goog.math.Rect.createFromBox(box);
311 var box2 = rect.toBox();
312
313 // Use toString for this test since otherwise NaN != NaN.
314 assertEquals(box.toString(), box2.toString());
315 }
316
317 function testRectContainsRect() {
318 var r = new goog.math.Rect(-10, 0, 20, 10);
319 assertTrue(r.contains(r));
320 assertFalse(r.contains(new goog.math.Rect(NaN, NaN, NaN, NaN)));
321 var r2 = new goog.math.Rect(0, 2, 5, 5);
322 assertTrue(r.contains(r2));
323 assertFalse(r2.contains(r));
324 r2.left = -11;
325 assertFalse(r.contains(r2));
326 r2.left = 0;
327 r2.width = 15;
328 assertFalse(r.contains(r2));
329 r2.width = 5;
330 r2.height = 10;
331 assertFalse(r.contains(r2));
332 r2.top = 0;
333 assertTrue(r.contains(r2));
334 }
335
336 function testRectContainsCoordinate() {
337 var r = new goog.math.Rect(20, 40, 60, 80);
338
339 // Test middle.
340 assertTrue(r.contains(new goog.math.Coordinate(50, 80)));
341
342 // Test edges.
343 assertTrue(r.contains(new goog.math.Coordinate(20, 40)));
344 assertTrue(r.contains(new goog.math.Coordinate(50, 40)));
345 assertTrue(r.contains(new goog.math.Coordinate(80, 40)));
346 assertTrue(r.contains(new goog.math.Coordinate(80, 80)));
347 assertTrue(r.contains(new goog.math.Coordinate(80, 120)));
348 assertTrue(r.contains(new goog.math.Coordinate(50, 120)));
349 assertTrue(r.contains(new goog.math.Coordinate(20, 120)));
350 assertTrue(r.contains(new goog.math.Coordinate(20, 80)));
351
352 // Test outside.
353 assertFalse(r.contains(new goog.math.Coordinate(0, 0)));
354 assertFalse(r.contains(new goog.math.Coordinate(50, 0)));
355 assertFalse(r.contains(new goog.math.Coordinate(100, 0)));
356 assertFalse(r.contains(new goog.math.Coordinate(100, 80)));
357 assertFalse(r.contains(new goog.math.Coordinate(100, 160)));
358 assertFalse(r.contains(new goog.math.Coordinate(50, 160)));
359 assertFalse(r.contains(new goog.math.Coordinate(0, 160)));
360 assertFalse(r.contains(new goog.math.Coordinate(0, 80)));
361 }
362
363 function testGetSize() {
364 assertObjectEquals(
365 new goog.math.Size(60, 80), new goog.math.Rect(20, 40, 60, 80).getSize());
366 }
367
368 function testGetBottomRight() {
369 assertObjectEquals(
370 new goog.math.Coordinate(40, 60),
371 new goog.math.Rect(10, 20, 30, 40).getBottomRight());
372 }
373
374 function testGetCenter() {
375 assertObjectEquals(
376 new goog.math.Coordinate(25, 40),
377 new goog.math.Rect(10, 20, 30, 40).getCenter());
378 }
379
380 function testGetTopLeft() {
381 assertObjectEquals(
382 new goog.math.Coordinate(10, 20),
383 new goog.math.Rect(10, 20, 30, 40).getTopLeft());
384 }
385
386 function testRectCeil() {
387 var rect = new goog.math.Rect(11.4, 26.6, 17.8, 9.2);
388 assertEquals(
389 'The function should return the target instance', rect, rect.ceil());
390 assertRectsEqual(new goog.math.Rect(12, 27, 18, 10), rect);
391 }
392
393 function testRectFloor() {
394 var rect = new goog.math.Rect(11.4, 26.6, 17.8, 9.2);
395 assertEquals(
396 'The function should return the target instance', rect, rect.floor());
397 assertRectsEqual(new goog.math.Rect(11, 26, 17, 9), rect);
398 }
399
400 function testRectRound() {
401 var rect = new goog.math.Rect(11.4, 26.6, 17.8, 9.2);
402 assertEquals(
403 'The function should return the target instance', rect, rect.round());
404 assertRectsEqual(new goog.math.Rect(11, 27, 18, 9), rect);
405 }
406
407 function testRectTranslateCoordinate() {
408 var rect = new goog.math.Rect(10, 40, 30, 20);
409 var c = new goog.math.Coordinate(10, 5);
410 assertEquals(
411 'The function should return the target instance', rect,
412 rect.translate(c));
413 assertRectsEqual(new goog.math.Rect(20, 45, 30, 20), rect);
414 }
415
416 function testRectTranslateXY() {
417 var rect = new goog.math.Rect(10, 20, 40, 35);
418 assertEquals(
419 'The function should return the target instance', rect,
420 rect.translate(15, 10));
421 assertRectsEqual(new goog.math.Rect(25, 30, 40, 35), rect);
422 }
423
424 function testRectTranslateX() {
425 var rect = new goog.math.Rect(12, 34, 113, 88);
426 assertEquals(
427 'The function should return the target instance', rect,
428 rect.translate(10));
429 assertRectsEqual(new goog.math.Rect(22, 34, 113, 88), rect);
430 }
431
432 function testRectScaleXY() {
433 var rect = new goog.math.Rect(10, 30, 100, 60);
434 assertEquals(
435 'The function should return the target instance', rect, rect.scale(2, 5));
436 assertRectsEqual(new goog.math.Rect(20, 150, 200, 300), rect);
437 }
438
439 function testRectScaleFactor() {
440 var rect = new goog.math.Rect(12, 34, 113, 88);
441 assertEquals(
442 'The function should return the target instance', rect, rect.scale(10));
443 assertRectsEqual(new goog.math.Rect(120, 340, 1130, 880), rect);
444 }
445
446 function testSquaredDistance() {
447 var rect = new goog.math.Rect(-10, -20, 15, 25);
448
449 // Test regions:
450 // 1 2 3
451 // +-+
452 // 4 |5| 6
453 // +-+
454 // 7 8 9
455
456 // Region 5 (inside the rectangle).
457 assertEquals(0, rect.squaredDistance(new goog.math.Coordinate(-10, 5)));
458 assertEquals(0, rect.squaredDistance(new goog.math.Coordinate(5, -20)));
459
460 // 1, 2, and 3.
461 assertEquals(25, rect.squaredDistance(new goog.math.Coordinate(9, 8)));
462 assertEquals(36, rect.squaredDistance(new goog.math.Coordinate(2, 11)));
463 assertEquals(53, rect.squaredDistance(new goog.math.Coordinate(12, 7)));
464
465 // 4 and 6.
466 assertEquals(81, rect.squaredDistance(new goog.math.Coordinate(-19, -10)));
467 assertEquals(64, rect.squaredDistance(new goog.math.Coordinate(13, 0)));
468
469 // 7, 8, and 9.
470 assertEquals(20, rect.squaredDistance(new goog.math.Coordinate(-12, -24)));
471 assertEquals(9, rect.squaredDistance(new goog.math.Coordinate(0, -23)));
472 assertEquals(34, rect.squaredDistance(new goog.math.Coordinate(8, -25)));
473 }
474
475
476 function testDistance() {
477 var rect = new goog.math.Rect(2, 4, 8, 16);
478
479 // Region 5 (inside the rectangle).
480 assertEquals(0, rect.distance(new goog.math.Coordinate(2, 4)));
481 assertEquals(0, rect.distance(new goog.math.Coordinate(10, 20)));
482
483 // 1, 2, and 3.
484 assertRoughlyEquals(
485 Math.sqrt(8), rect.distance(new goog.math.Coordinate(0, 22)), .0001);
486 assertEquals(8, rect.distance(new goog.math.Coordinate(9, 28)));
487 assertRoughlyEquals(
488 Math.sqrt(50), rect.distance(new goog.math.Coordinate(15, 25)), .0001);
489
490 // 4 and 6.
491 assertEquals(7, rect.distance(new goog.math.Coordinate(-5, 6)));
492 assertEquals(10, rect.distance(new goog.math.Coordinate(20, 10)));
493
494 // 7, 8, and 9.
495 assertEquals(5, rect.distance(new goog.math.Coordinate(-2, 1)));
496 assertEquals(2, rect.distance(new goog.math.Coordinate(5, 2)));
497 assertRoughlyEquals(
498 Math.sqrt(10), rect.distance(new goog.math.Coordinate(1, 1)), .0001);
499 }