Max OS 0.3
Loading...
Searching...
No Matches
rectangle.h
Go to the documentation of this file.
1
9#ifndef MAXOS_COMMON_RECTANGLE_H
10#define MAXOS_COMMON_RECTANGLE_H
11
12#include <common/vector.h>
13
14namespace MaxOS::common {
15
22 template<class Type> class Rectangle {
23 public:
24 Type left { 0 };
25 Type top { 0 };
26 Type width { 0 };
27 Type height { 0 };
28
30 Rectangle(Type left, Type top, Type width, Type height);
31 ~Rectangle();
32
35
37
39 bool contains(Type x, Type y);
40 };
41
43
44 template<class Type> Rectangle<Type>::Rectangle() = default;
45
46
56 template<class Type> Rectangle<Type>::Rectangle(Type left, Type top, Type width, Type height)
57 : left(left),
58 top(top),
59 width(width),
60 height(height) {
61
62 // If the width is negative, move the left and make the width positive
63 if(width < 0) {
64 left += width;
65 width *= -1;
66 }
67
68 // If the m_height is negative, move the top and make the height positive
69 if(height < 0) {
70 top += height;
71 height *= -1;
72 }
73
74 // Set the values
75 this->left = left;
76 this->top = top;
77 }
78
79 template<class Type> Rectangle<Type>::~Rectangle() = default;
80
88 template<class Type> bool Rectangle<Type>::intersects(const Rectangle<Type>& other) {
89 // If the other rectangle is to the left of this rectangle
90 if(other.left + other.width <= left)
91 return false;
92
93 // If the other rectangle is to the right of this rectangle
94 if(other.left >= left + width)
95 return false;
96
97 // If the other rectangle is above this rectangle
98 if(other.top + other.height <= top)
99 return false;
100
101 // If the other rectangle is below this rectangle
102 if(other.top >= top + height)
103 return false;
104
105 // If none of the above conditions are true, then the rectangles intersect
106 return true;
107 }
108
117 // If the rectangles don't intersect, return an empty rectangle
118 if(!intersects(other))
119 return Rectangle<Type>();
120
121 // Get the left and top of the intersection using the maximum of the two
122 Type i_left = this->left > other.left ? this->left : other.left;
123 Type i_top = this->top > other.top ? this->top : other.top;
124
125 // Get the right and bottom of the intersection using the minimum of the two
126 Type right = this->left + this->width < other.left + other.width ? this->left + this->width : other.left +
127 other.width;
128 Type bottom = this->top + this->height < other.top + other.height ? this->top + this->height : other.top +
130
131 // Return the intersection
132 return Rectangle<Type>(i_left, i_top, right - left, bottom - top);
133 }
134
143
144 // Store the result rectangle
146
147 // Make sure the rectangles intersect
148 if(!intersects(other)) {
149 result.push_back(*this);
150 return result;
151 }
152
153 // Get the minimum and maximum values for the width and height
157
158 // Add the non-intersecting rectangles to the result
159
160 // Add non-overlapping region above if current top is less than other top
161 if(top < other.top)
163 other.top - top));
164
165 // Add non-overlapping region to the left if current left is less than other left
166 if(left < other.left)
167 result.push_back(
169
170 // Add non-overlapping region to the right if current right is greater than other right
171 if(left + width > other.left + other.width)
172 result.push_back(Rectangle<Type>(
174 (left + width) - (other.left + other.width) + 1, height));
175
176 // Add non-overlapping region below if current bottom is greater than other bottom
177 if(this->top + this->height > other.top + other.height)
180 top + height - min_bottom + 1));
181
182 return result;
183 }
184
192 template<class Type> bool Rectangle<Type>::contains(const Rectangle<Type>& other) {
193
194 // If the other rectangle is to the left of this rectangle
195 if(other.left + other.width <= left)
196 return false;
197
198 // If the other rectangle is to the right of this rectangle
199 if(other.left >= left + width)
200 return false;
201
202 // If the other rectangle is above this rectangle
203 if(other.top + other.height <= top)
204 return false;
205
206 // If the other rectangle is below this rectangle
207 if(other.top >= top + height)
208 return false;
209
210 // If none of the above conditions are true, then the rectangles intersect
211 return true;
212 }
213
214
223 template<class Type> bool Rectangle<Type>::contains(Type x, Type y) {
224
225 // If the point is to the left of this rectangle
226 if(x < left)
227 return false;
228
229 // If the point is to the right of this rectangle
230 if(x >= left + width)
231 return false;
232
233 // If the point is above this rectangle
234 if(y < top)
235 return false;
236
237 // If the point is below this rectangle
238 if(y >= top + height)
239 return false;
240
241 // If none of the above conditions are true, then the point is inside the rectangle
242 return true;
243
244 }
245
246}
247
248#endif //MAXOS_COMMON_RECTANGLE_H
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22
Type height
The height of the rectangle.
Definition rectangle.h:27
Type top
The top coordinate of the rectangle.
Definition rectangle.h:25
bool contains(Type x, Type y)
Definition rectangle.h:223
bool intersects(const Rectangle< Type > &)
Definition rectangle.h:88
Rectangle< Type > intersection(const Rectangle< Type > &)
Definition rectangle.h:116
Type left
The left coordinate of the rectangle.
Definition rectangle.h:24
Vector< Rectangle< Type > > subtract(const Rectangle< Type > &)
Definition rectangle.h:142
Rectangle()
_______________________________________________TEMPLATES_____________________________________________...
bool contains(const Rectangle< Type > &)
Definition rectangle.h:192
Type width
The width of the rectangle.
Definition rectangle.h:26
Rectangle(Type left, Type top, Type width, Type height)
Creates a new rectangle, if width or height are negative, the left/top will be adjusted and the width...
Definition rectangle.h:56
Dynamically stores an array of elements.
Definition vector.h:39
Defines a Vector class for dynamically storing an array of elements.