相交矩形

1
2
3
4
5
6
7
8
9
10
11
pair<int, int> calculateIntersection(pair<pair<int, int>, pair<int, int>> rect1, pair<pair<int, int>, pair<int, int>> rect2) {
int x1 = max(rect1.first.first, rect2.first.first);
int y1 = max(rect1.first.second, rect2.first.second);
int x2 = min(rect1.second.first, rect2.second.first);
int y2 = min(rect1.second.second, rect2.second.second);

int width = max(0, x2 - x1);
int height = max(0, y2 - y1);

return make_pair(width, height);
}