-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBox.cpp
More file actions
126 lines (106 loc) · 3.59 KB
/
Box.cpp
File metadata and controls
126 lines (106 loc) · 3.59 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "Box.h"
using namespace RTE;
const std::string Box::c_ClassName = "Box";
bool IntRect::IntersectionCut(const IntRect& rhs) {
if (Intersects(rhs)) {
m_Left = std::max(m_Left, rhs.m_Left);
m_Right = std::min(m_Right, rhs.m_Right);
m_Top = std::max(m_Top, rhs.m_Top);
m_Bottom = std::min(m_Bottom, rhs.m_Bottom);
return true;
}
return false;
}
int Box::Create(const Vector& corner1, const Vector& corner2) {
m_Corner = corner1;
m_Width = corner2.m_X - corner1.m_X;
m_Height = corner2.m_Y - corner1.m_Y;
Unflip();
return 0;
}
int Box::Create(float x1, float y1, float x2, float y2) {
m_Corner.SetXY(x1, y1);
m_Width = x2 - x1;
m_Height = y2 - y1;
Unflip();
return 0;
}
int Box::Create(const Vector& corner, float width, float height) {
m_Corner = corner;
m_Width = width;
m_Height = height;
Unflip();
return 0;
}
int Box::Create(const Box& reference) {
m_Corner = reference.m_Corner;
m_Width = reference.m_Width;
m_Height = reference.m_Height;
Unflip();
return 0;
}
int Box::ReadProperty(const std::string_view& propName, Reader& reader) {
StartPropertyList(return Serializable::ReadProperty(propName, reader));
MatchProperty("Corner", { reader >> m_Corner; });
MatchProperty("Width", { reader >> m_Width; });
MatchProperty("Height", { reader >> m_Height; });
EndPropertyList;
}
int Box::Save(Writer& writer) const {
Serializable::Save(writer);
writer.NewPropertyWithValue("Corner", m_Corner);
writer.NewPropertyWithValue("Width", m_Width);
writer.NewPropertyWithValue("Height", m_Height);
return 0;
}
void Box::Unflip() {
if (m_Width < 0) {
m_Width = -m_Width;
m_Corner.m_X -= m_Width;
}
if (m_Height < 0) {
m_Height = -m_Height;
m_Corner.m_Y -= m_Height;
}
}
bool Box::IsWithinBox(const Vector& point) const {
return !IsEmpty() && (((m_Width > 0 && point.m_X >= m_Corner.m_X && point.m_X < (m_Corner.m_X + m_Width)) ||
(m_Width < 0 && point.m_X < m_Corner.m_X && point.m_X >= (m_Corner.m_X + m_Width))) &&
(m_Height > 0 && point.m_Y >= m_Corner.m_Y && point.m_Y < (m_Corner.m_Y + m_Height)) ||
(m_Height < 0 && point.m_Y < m_Corner.m_Y && point.m_Y <= (m_Corner.m_Y + m_Height)));
}
bool Box::IsWithinBoxX(float pointX) const {
return !IsEmpty() && ((m_Width > 0 && pointX >= m_Corner.m_X && pointX < (m_Corner.m_X + m_Width)) ||
(m_Width < 0 && pointX < m_Corner.m_X && pointX >= (m_Corner.m_X + m_Width)));
}
bool Box::IsWithinBoxY(float pointY) const {
return !IsEmpty() && ((m_Height > 0 && pointY >= m_Corner.m_Y && pointY < (m_Corner.m_Y + m_Height)) ||
(m_Height < 0 && pointY < m_Corner.m_Y && pointY <= (m_Corner.m_Y + m_Height)));
}
float Box::GetWithinBoxX(float pointX) const {
if (m_Width > 0) {
return Limit(pointX, m_Corner.m_X + m_Width - 1, m_Corner.m_X);
} else if (m_Width < 0) {
return Limit(pointX, m_Corner.m_X - 1, m_Corner.m_X + m_Width);
}
return m_Corner.m_X;
}
float Box::GetWithinBoxY(float pointY) const {
if (m_Height > 0) {
return Limit(pointY, m_Corner.m_Y + m_Height - 1, m_Corner.m_Y);
} else if (m_Height < 0) {
return Limit(pointY, m_Corner.m_Y - 1, m_Corner.m_Y + m_Height);
}
return m_Corner.m_Y;
}
bool Box::IntersectsBox(const Box& rhs) {
if (IsEmpty() || rhs.IsEmpty()) {
return false;
}
Box box1 = *this;
Box box2 = rhs;
box1.Unflip();
box2.Unflip();
return (box1.m_Corner.m_X < box2.m_Corner.m_X + box2.m_Width) && (box1.m_Corner.m_X + box1.m_Width > box2.m_Corner.m_X) &&
(box1.m_Corner.m_Y < box2.m_Corner.m_Y + box2.m_Height) && (box1.m_Corner.m_Y + box1.m_Height > box2.m_Corner.m_Y);
}