-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRectangleIntersection.java
More file actions
96 lines (88 loc) · 3.45 KB
/
Copy pathRectangleIntersection.java
File metadata and controls
96 lines (88 loc) · 3.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
package epi.primitive._5._11;
import static org.junit.Assert.assertEquals;
/**
* Question:
* Write a program which tests if two rectangles have a nonempty intersection.
* If the intersection is nonempty, return the rectangle formed by their intersection.
* ---
* Solution:
* We have 4 cases for relative positions between 2 rectangles:
* <p>
* x1 x2 x1` x1`
* ┌───────┐ ┌───────┐
* │ R1 │ │ R2 │
* │ │ │ │
* └───────┘ │ │
* └───────┘
* x1` x1`
* ┌───────────┐
* x1 │ x2 │
* ┌─────┼──┐ │
* │ R1│ │ R2 │
* └─────┼──┘ │
* │ │
* └───────────┘
* <p>
* <p>
* x1` x2`
* ┌─────────────┐
* │ x1 x2 │
* │ ┌───────┐ │
* │ │ R1 │ │
* │ │ │ │
* │ └───────┘ │
* │ R2 │
* └─────────────┘
* <p>
* x1` x2`
* ┌─────────┐
* │ x1 │ x2
* │ ┌────┼──────┐
* │ R2 │ │ │
* │ │ │ R1 │
* │ └────┼──────┘
* │ │
* └─────────┘
* x1` x2`
* ┌──────────┐ ┌─────────┐
* │ │ │ │
* │ R2 │ │ R1 │
* │ │ │ │
* └──────────┘ └─────────┘
* x1 x2
* ---
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
public class RectangleIntersection {
public static void main(String[] args) {
assertEquals(new Rectangle(44, 66, 49, 6), intersectRectangle(new Rectangle(44, 66, 84, 14), new Rectangle(8, 54, 85, 18)));
}
private static Rectangle intersectRectangle(Rectangle R1, Rectangle R2) {
// Use the above drawings as guidelines
Rectangle rectangle = new Rectangle(0, 0, -1, -1);
// First we tackle the corner cases where there is no intersection (first and last drawing)
// We compare the last edge (let's say A) (for both x and y axes) from R1 / R2 with the first edge from R2 / R1 (let's say B)
// If any A < B then there is no intersection
if (R1.x + R1.width < R2.x || R2.x + R2.width < R1.x || R1.y + R1.height < R2.y || R2.y + R2.height < R1.y) {
return rectangle;
}
// We only have left the 3 middle cases
// Check x axis lines, by comparing first edge of R1 / R2 with second edge of R2 / R1
// As we can see from the drawings, the intersection occurs when:
// - x1` < x2
// - x1 < x2`
if (R2.x < R1.x + R1.width || R1.x < R2.x + R2.width) {
// Compute intersection x axis points
rectangle.x = Math.max(R1.x, R2.x);
rectangle.width = Math.min(R1.x + R1.width, R2.x + R2.width) - rectangle.x;
}
// Repeat the above step for y axis
if (R2.y < R1.y + R1.height || R1.y < R2.y + R2.height) {
// Compute intersection y axis points
rectangle.y = Math.max(R1.y, R2.y);
rectangle.height = Math.min(R1.y + R1.height, R2.y + R2.height) - rectangle.y;
}
return rectangle;
}
}