/
rectangle.h
138 lines (127 loc) · 4.72 KB
/
rectangle.h
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
127
128
129
130
131
132
133
134
135
136
137
138
/*
* The Doomsday Engine Project -- libdeng2
*
* Copyright (c) 2009-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LIBDENG2_RECTANGLE_H
#define LIBDENG2_RECTANGLE_H
#include "Vector"
#ifdef WIN32
# undef min
# undef max
#endif
namespace de {
/**
* Template for 2D rectangles. The members are public for convenient access.
*
* @ingroup math
*/
template <typename CornerVectorType, typename SizeVectorType>
class Rectangle
{
public:
typedef Rectangle<CornerVectorType, SizeVectorType> RectangleType;
typedef CornerVectorType Corner;
typedef SizeVectorType Size;
typedef typename Corner::ValueType Type;
typedef typename Size::ValueType SizeType;
public:
Rectangle() {}
Rectangle(Type left, Type top, SizeType width, SizeType height)
: topLeft(left, top), bottomRight(left + width, top + height) {}
Rectangle(Corner const &tl, Corner const &br) : topLeft(tl), bottomRight(br) {}
static RectangleType fromSize(Size const &size) {
return RectangleType(0, 0, size.x, size.y);
}
static RectangleType fromSize(Corner const &tl, Size const &size) {
return RectangleType(tl.x, tl.y, size.x, size.y);
}
SizeType width() const { return abs(bottomRight.x - topLeft.x); }
SizeType height() const { return abs(bottomRight.y - topLeft.y); }
Size size() const { return Size(width(), height()); }
void moveTopLeft(CornerVectorType const &point) {
Size s = size();
topLeft = point;
bottomRight.x = point.x + s.x;
bottomRight.y = point.y + s.y;
}
void setWidth(SizeType w) { bottomRight.x = topLeft.x + w; }
void setHeight(SizeType h) { bottomRight.y = topLeft.y + h; }
void setSize(SizeVectorType const &s) { setWidth(s.x); setHeight(s.y); }
void include(Corner const &point) {
topLeft = topLeft.min(point);
bottomRight = bottomRight.max(point);
}
RectangleType expanded(Type n) const {
return RectangleType(topLeft - Corner(n, n), bottomRight + Corner(n, n));
}
RectangleType adjusted(CornerVectorType const &tl, CornerVectorType const &br) const {
return RectangleType(topLeft + tl, bottomRight + br);
}
bool contains(Corner const &point) const {
return point >= topLeft && point <= bottomRight;
}
bool operator == (RectangleType const &other) const {
return topLeft == other.topLeft && bottomRight == other.bottomRight;
}
RectangleType operator | (RectangleType const &other) const {
return RectangleType(topLeft.min(other.topLeft),
bottomRight.max(other.bottomRight));
}
RectangleType &operator |= (RectangleType const &other) {
topLeft = topLeft.min(other.topLeft);
bottomRight = bottomRight.max(other.bottomRight);
return *this;
}
String asText() const {
return "[" + topLeft.asText() + "->" + bottomRight.asText() +
" size:" + size().asText() + "]";
}
Type left() const { return topLeft.x; }
Type right() const { return bottomRight.x; }
Type top() const { return topLeft.y; }
Type bottom() const { return bottomRight.y; }
Corner topRight() const {
return Corner(bottomRight.x, topLeft.y);
}
Corner bottomLeft() const {
return Corner(topLeft.x, bottomRight.y);
}
Corner midLeft() const {
return Corner(topLeft.x, (topLeft.y + bottomRight.y)/2.0);
}
Corner midRight() const {
return Corner(bottomRight.x, (topLeft.y + bottomRight.y)/2.0);
}
Corner midTop() const {
return Corner((topLeft.x + bottomRight.x)/2.0, topLeft.y);
}
Corner midBottom() const {
return Corner((topLeft.x + bottomRight.x)/2.0, bottomRight.y);
}
Corner middle() const {
return Corner((topLeft.x + bottomRight.x)/2.0, (topLeft.y + bottomRight.y)/2.0);
}
public:
Corner topLeft;
Corner bottomRight;
};
// Common types.
typedef Rectangle<Vector2i, Vector2ui> Rectanglei;
typedef Rectangle<Vector2ui, Vector2ui> Rectangleui;
typedef Rectangle<Vector2f, Vector2f> Rectanglef;
} // namespace de
#endif /* LIBDENG2_RECTANGLE_H */