-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathRectangle.cpp
64 lines (55 loc) · 1.59 KB
/
Rectangle.cpp
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
#include "Rectangle.h"
namespace Engine
{
Rectangle::Rectangle()
: X(0), Y(0), Width(0), Height(0), TopLeft(Point(0, 0)), Size(Engine::Size(0, 0))
{ }
Rectangle::Rectangle(int x, int y, unsigned int width, unsigned int height)
: X(x), Y(y), Width(width), Height(height), TopLeft(Point(x, y)), Size(Engine::Size(width, height))
{ }
Rectangle::Rectangle(const Point &topleft, const Engine::Size &size)
: TopLeft(topleft), Size(size), X(topleft.X), Y(topleft.Y), Width(size.Width), Height(size.Height)
{ }
//Rectangle::~Rectangle()
//{ }
Point Rectangle::GetBottomRight(void) const
{
return Point(this->X + this->Width,
this->Y + this->Height);
}
void Rectangle::GetBottomRight(Point *p) const
{
p->X = this->X + this->Width;
p->Y = this->Y + this->Height;
}
void Rectangle::SetBottomRight(Point p)
{
if(p < this->TopLeft)
{
Point tmp;
tmp = this->TopLeft;
this->TopLeft = p;
p = tmp;
}
this->Width = p.X - this->TopLeft.X;
this->Height = p.Y - this->TopLeft.Y;
}
bool Rectangle::Contains(const Rectangle &other) const
{
Point P1 = this->TopLeft;
Point P2 = this->GetBottomRight();
Point otherP1 = other.TopLeft;
Point otherP2 = other.GetBottomRight();
return (P1 <= otherP1 &&
P2 >= otherP2);
}
bool Rectangle::Intersects(const Rectangle &other) const
{
Point P1 = this->TopLeft;
Point P2 = this->GetBottomRight();
Point otherP1 = other.TopLeft;
Point otherP2 = other.GetBottomRight();
return ((P1 < otherP1 && P2 > otherP1) ||
(P1 < otherP2 && P2 > otherP2));
}
}