-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindRectangularOverlap.js
116 lines (80 loc) · 3.52 KB
/
findRectangularOverlap.js
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
// Write a function to find the rectangular intersection of two given love rectangles.
// As with the example above, love rectangles are always "straight" and never "diagonal." More rigorously: each side is parallel with either the x-axis or the y-axis.
// They are defined as objects ↴ like this:
// const myRectangle = {
// // Coordinates of bottom-left corner
// leftX: 1,
// bottomY: 1,
// // Width and height
// width: 6,
// height: 3,
// };
// Your output rectangle should use this format as well.
function findRectangularOverlap(rect1, rect2) {
let result = { leftX: 0, bottomY: 0, width: 0, height: 0 };
if (rect1.leftX > rect2.leftX) {
let temp = rect1;
rect1 = rect2;
rect2 = temp;
}
if (rect1.leftX + rect1.width <= rect2.leftX || rect1.bottomY + rect1.height <= rect2.bottomY) {
return result;
}
result.leftX = rect2.leftX;
result.bottomY = rect2.bottomY;
result.width = (rect1.leftX + rect1.width < rect2.leftX + rect2.width ? rect1.leftX + rect1.width : rect2.leftX + rect2.width) - result.leftX;
result.height = (rect1.bottomY + rect1.height <= rect2.bottomY + rect2.height ? rect1.bottomY + rect1.height : rect2.bottomY + rect2.height) - result.bottomY;
return result;
}
// Tests
let desc = 'overlap along both axes';
let rect1 = { leftX: 1, bottomY: 1, width: 6, height: 3 };
let rect2 = { leftX: 5, bottomY: 2, width: 3, height: 6 };
let actual = findRectangularOverlap(rect1, rect2);
let expected = { leftX: 5, bottomY: 2, width: 2, height: 2 };
assertObjectEquals(actual, expected, desc);
desc = 'one rectangle inside another';
rect1 = { leftX: 1, bottomY: 1, width: 6, height: 6 };
rect2 = { leftX: 3, bottomY: 3, width: 2, height: 2 };
actual = findRectangularOverlap(rect1, rect2);
expected = { leftX: 3, bottomY: 3, width: 2, height: 2 };
assertObjectEquals(actual, expected, desc);
desc = 'both rectangles the same';
rect1 = { leftX: 2, bottomY: 2, width: 4, height: 4 };
rect2 = { leftX: 2, bottomY: 2, width: 4, height: 4 };
actual = findRectangularOverlap(rect1, rect2);
expected = { leftX: 2, bottomY: 2, width: 4, height: 4 };
assertObjectEquals(actual, expected, desc);
desc = 'touch on horizontal edge';
rect1 = { leftX: 1, bottomY: 2, width: 3, height: 4 };
rect2 = { leftX: 2, bottomY: 6, width: 2, height: 2 };
actual = findRectangularOverlap(rect1, rect2);
expected = { leftX: 0, bottomY: 0, width: 0, height: 0 };
assertObjectEquals(actual, expected, desc);
desc = 'touch on vertical edge';
rect1 = { leftX: 1, bottomY: 2, width: 3, height: 4 };
rect2 = { leftX: 4, bottomY: 3, width: 2, height: 2 };
actual = findRectangularOverlap(rect1, rect2);
expected = { leftX: 0, bottomY: 0, width: 0, height: 0 };
assertObjectEquals(actual, expected, desc);
desc = 'touch at a corner';
rect1 = { leftX: 1, bottomY: 1, width: 2, height: 2 };
rect2 = { leftX: 3, bottomY: 3, width: 2, height: 2 };
actual = findRectangularOverlap(rect1, rect2);
expected = { leftX: 0, bottomY: 0, width: 0, height: 0 };
assertObjectEquals(actual, expected, desc);
desc = 'no overlap';
rect1 = { leftX: 1, bottomY: 1, width: 2, height: 2 };
rect2 = { leftX: 4, bottomY: 6, width: 3, height: 6 };
actual = findRectangularOverlap(rect1, rect2);
expected = { leftX: 0, bottomY: 0, width: 0, height: 0 };
assertObjectEquals(actual, expected, desc);
function assertObjectEquals(a, b, desc) {
const objectA = JSON.stringify(a);
const objectB = JSON.stringify(b);
if (objectA !== objectB) {
console.log(`${desc} ... FAIL: ${objectA} != ${objectB}`)
} else {
console.log(`${desc} ... PASS`);
}
}