-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay19.cpp
More file actions
47 lines (37 loc) · 1.4 KB
/
Day19.cpp
File metadata and controls
47 lines (37 loc) · 1.4 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
/*
This problem was asked by Google.
Given two rectangles on a 2D graph, return the area of their intersection. If the rectangles don't intersect, return 0.
For example, given the following rectangles:
{
"top_left": (1, 4),
"dimensions": (3, 3) # width, height
}
and
{
"top_left": (0, 5),
"dimensions": (4, 3) # width, height
}
return 6.
*/
#include <bits/stdc++.h>
using namespace std;
int calculateIntersectionArea(pair<int, int> top_left1, pair<int, int> dimensions1, pair<int, int> top_left2, pair<int, int> dimensions2)
{
pair<int, int> bottom_right1 = {top_left1.first + dimensions1.first, top_left1.second - dimensions1.second};
pair<int, int> bottom_right2 = {top_left2.first + dimensions2.first, top_left2.second - dimensions2.second};
int x_overlap = max(0, min(bottom_right1.first, bottom_right2.first) - max(top_left1.first, top_left2.first));
int y_overlap = max(0, min(top_left1.second, top_left2.second) - max(bottom_right1.second, bottom_right2.second));
return x_overlap * y_overlap;
}
int main()
{
// Test case 1
pair<int, int> top_left1 = {1, 4};
pair<int, int> dimensions1 = {3, 3};
pair<int, int> top_left2 = {0, 5};
pair<int, int> dimensions2 = {4, 3};
// Calculate area of intersection
int area = calculateIntersectionArea(top_left1, dimensions1, top_left2, dimensions2);
cout << "Area of intersection: " << area << endl;
return 0;
}