-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEasy_Prob185.cpp
More file actions
90 lines (73 loc) · 3.41 KB
/
Easy_Prob185.cpp
File metadata and controls
90 lines (73 loc) · 3.41 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
/* Given two rectangles on a 2D graph, return the area of their intersection. If the rectangles don't intersect, return 0. */
#include <iostream>
#include <utility>
using namespace std;
struct Rectangle {
int top_left_x;
int top_left_y;
int width;
int height;
Rectangle(int top_left_x, int top_left_y, int width, int height) : top_left_x(top_left_x), top_left_y(top_left_y), width(width), height(height) {}
Rectangle() {
top_left_x = 0;
top_left_y = 0;
width = 0;
height = 0;
}
int getTopLeftX() const { return top_left_x; }
void setTopLeftX(int top_left_x) { this->top_left_x = top_left_x; }
int getTopLeftY() const { return top_left_y; }
void setTopLeftY(int top_left_y) { this->top_left_y = top_left_y; }
int getWidth() const { return width; }
void setWidth(int width) { this->width = width; }
int getHeight() const { return height; }
void setHeight(int height) { this->height = height; }
};
int intersectRectangles(Rectangle rectangle1, Rectangle rectangle2) {
int area = 0;
Rectangle rectangleLeft;
Rectangle rectangleRight;
// Initialize left rectangle and right rectangle
if (rectangle1.getTopLeftX() <= rectangle2.getTopLeftX()) {
rectangleLeft.setTopLeftX(rectangle1.getTopLeftX());
rectangleLeft.setTopLeftY(rectangle1.getTopLeftY());
rectangleLeft.setWidth(rectangle1.getWidth());
rectangleLeft.setHeight(rectangle1.getHeight());
rectangleRight.setTopLeftX(rectangle2.getTopLeftX());
rectangleRight.setTopLeftY(rectangle2.getTopLeftY());
rectangleRight.setWidth(rectangle2.getWidth());
rectangleRight.setHeight(rectangle2.getHeight());
} else {
rectangleLeft.setTopLeftX(rectangle2.getTopLeftX());
rectangleLeft.setTopLeftY(rectangle2.getTopLeftY());
rectangleLeft.setWidth(rectangle2.getWidth());
rectangleLeft.setHeight(rectangle2.getHeight());
rectangleRight.setTopLeftX(rectangle1.getTopLeftX());
rectangleRight.setTopLeftY(rectangle1.getTopLeftY());
rectangleRight.setWidth(rectangle1.getWidth());
rectangleRight.setHeight(rectangle1.getHeight());
}
// Check for intersection along x
if (rectangleLeft.getTopLeftX() + rectangleLeft.getWidth() <= rectangleRight.getTopLeftY()) {
// Verify if rectangle left is under rectangle richt
if (rectangleLeft.getTopLeftY() >= rectangleRight.getTopLeftY()) {
// Check for intersection along y
if (rectangleRight.getTopLeftY() >= (rectangleLeft.getTopLeftY() - rectangleLeft.getHeight())) {
area = (rectangleLeft.getTopLeftX() + rectangleLeft.getWidth() - rectangleRight.getTopLeftX()) * (rectangleRight.getTopLeftY() - rectangleLeft.getTopLeftY() + rectangleLeft.getHeight());
}
}
else {
// Check for intersection along y
if (rectangleLeft.getTopLeftY() >= (rectangleRight.getTopLeftY() - rectangleRight.getHeight())) {
area = (rectangleLeft.getTopLeftX() + rectangleLeft.getWidth() - rectangleRight.getTopLeftX() + 1) * (rectangleLeft.getTopLeftY() - rectangleRight.getHeight() + rectangleRight.getTopLeftY());
}
}
}
return area;
};
int main(int argc, char *argv[])
{
Rectangle rectangle1(1, 4, 3, 3);
Rectangle rectangle2(0, 5, 4, 3);
cout << intersectRectangles(rectangle1, rectangle2) << endl;
};