-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrectangles_intersect.cpp
More file actions
34 lines (27 loc) · 984 Bytes
/
rectangles_intersect.cpp
File metadata and controls
34 lines (27 loc) · 984 Bytes
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
/**
* You are given two rectangles, each defined by an upper-left (UL) corner and a
* lower-right (LR) corner.
* Both rectangles’ edges will always be parallel to the x or y axis.
* Write a function that determines whether the two rectangles overlap.
*/
#include "rectangles_intersect.hpp"
bool is_point_to_the_left(const p2d& a, const p2d& b) {
return a.x < b.x;
}
bool is_point_to_the_right(const p2d& a, const p2d& b) {
return !is_point_to_the_left(a, b);
}
bool is_point_above(const p2d& a, const p2d& b) {
return a.y > b.y;
}
bool is_point_below(const p2d& a, const p2d& b) {
return !is_point_above(a, b);
}
bool rectangles_intersect(const rectangle& r1, const rectangle& r2) {
const p2d& r1rb = r1.rb;
const p2d& r1lt = r1.lt;
const p2d& r2rb = r2.rb;
const p2d& r2lt = r2.lt;
return is_point_above(r2lt, r1rb) && is_point_below(r2rb, r1lt) && is_point_to_the_right(r2rb, r1lt) &&
is_point_to_the_left(r2lt, r1rb);
}