-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrectangle_intersection.h
More file actions
64 lines (57 loc) · 1.58 KB
/
Copy pathrectangle_intersection.h
File metadata and controls
64 lines (57 loc) · 1.58 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
#ifndef CPP_ALGORITHM_RECTANGLE_INTERSECTION_H
#define CPP_ALGORITHM_RECTANGLE_INTERSECTION_H
#include <algorithm>
namespace RectangleIntersection
{
struct Rectangle
{
int X;
int Y;
int Width;
int Height;
};
/**
* \brief Check if two rectangles intersect.
* \param r1 rectangle 1
* \param r2 rectangle 2
* \return result
*/
Rectangle IntersectRectangle(
const Rectangle& r1,
const Rectangle& r2);
/**
* \brief Check the intersection of two rectangles.
* \param r1 rectangle 1
* \param r2 rectangle 2
* \return Whether two rectangles intersect
*/
bool IsIntersect(
const Rectangle& r1,
const Rectangle& r2);
}
// ----------------------------------------------------------------------------
inline RectangleIntersection::Rectangle RectangleIntersection::IntersectRectangle(
const Rectangle& r1,
const Rectangle& r2)
{
if (!IsIntersect(r1, r2))
{
return {0, 0, -1, -1};
}
return {
std::max(r1.X, r2.X),
std::max(r1.Y, r2.Y),
std::min(r1.X + r1.Width, r2.X + r2.Width) - std::max(r1.X, r2.X),
std::min(r1.Y + r1.Height, r2.Y + r2.Height) - std::max(r1.Y, r2.Y)};
}
// ----------------------------------------------------------------------------
inline bool RectangleIntersection::IsIntersect(
const Rectangle& r1,
const Rectangle& r2)
{
return r1.X <= r2.X + r2.Width
&& r1.X + r1.Width >= r2.X
&& r1.Y <= r2.Y + r2.Height
&& r1.Y + r1.Height >= r2.Y;
}
#endif