Max OS 0.3
Loading...
Searching...
No Matches
MaxOS::common::Rectangle< Type > Class Template Reference

Stores the left, top, width and height of a rectangle. More...

#include <rectangle.h>

Public Member Functions

 Rectangle ()
 _______________________________________________TEMPLATES_________________________________________________________________///
 
 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/height made positive.
 
bool intersects (const Rectangle< Type > &)
 
Rectangle< Typeintersection (const Rectangle< Type > &)
 
Vector< Rectangle< Type > > subtract (const Rectangle< Type > &)
 
bool contains (const Rectangle< Type > &)
 
bool contains (Type x, Type y)
 

Public Attributes

Type left { 0 }
 The left coordinate of the rectangle.
 
Type top { 0 }
 The top coordinate of the rectangle.
 
Type width { 0 }
 The width of the rectangle.
 
Type height { 0 }
 The height of the rectangle.
 

Detailed Description

template<class Type>
class MaxOS::common::Rectangle< Type >

Stores the left, top, width and height of a rectangle.

Template Parameters
TypeThe type of the rectangle (assumed to be numeric)

Definition at line 22 of file rectangle.h.

Constructor & Destructor Documentation

◆ Rectangle()

template<class Type >
MaxOS::common::Rectangle< Type >::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/height made positive.

Template Parameters
TypeThe type of the rectangle
Parameters
leftThe left coordinate
topThe top coordinate
widthThe width of the rectangle
heightThe height of the rectangle

Definition at line 56 of file rectangle.h.

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 }
Type height
The height of the rectangle.
Definition rectangle.h:27
Type top
The top coordinate of the rectangle.
Definition rectangle.h:25
Type left
The left coordinate of the rectangle.
Definition rectangle.h:24
Type width
The width of the rectangle.
Definition rectangle.h:26

References MaxOS::common::Rectangle< Type >::height, MaxOS::common::Rectangle< Type >::left, MaxOS::common::Rectangle< Type >::top, and MaxOS::common::Rectangle< Type >::width.

Member Function Documentation

◆ contains() [1/2]

template<class Type >
bool MaxOS::common::Rectangle< Type >::contains ( const Rectangle< Type > &  other)

Checks if this rectangle contains another rectangle

Template Parameters
TypeThe type of the rectangle
Parameters
otherThe other rectangle
Returns
True if this rectangle contains the other rectangle, false otherwise

Definition at line 192 of file rectangle.h.

192 {
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 }
Rectangle()
_______________________________________________TEMPLATES_____________________________________________...

References MaxOS::common::Rectangle< Type >::height, MaxOS::common::Rectangle< Type >::left, MaxOS::common::Rectangle< Type >::top, and MaxOS::common::Rectangle< Type >::width.

Referenced by MaxOS::gui::Widget::contains_coordinate(), MaxOS::gui::CompositeWidget::on_mouse_enter_widget(), MaxOS::gui::CompositeWidget::on_mouse_leave_widget(), and MaxOS::gui::CompositeWidget::on_mouse_move_widget().

◆ contains() [2/2]

template<class Type >
bool MaxOS::common::Rectangle< Type >::contains ( Type  x,
Type  y 
)

Checks if this rectangle contains a point

Template Parameters
TypeThe type of the rectangle
Parameters
xThe x coordinate of the point
yThe y coordinate of the point
Returns
True if this rectangle contains the point, false otherwise

Definition at line 223 of file rectangle.h.

223 {
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 }

References MaxOS::common::Rectangle< Type >::height, MaxOS::common::Rectangle< Type >::left, MaxOS::common::Rectangle< Type >::top, and MaxOS::common::Rectangle< Type >::width.

◆ intersection()

template<class Type >
Rectangle< Type > MaxOS::common::Rectangle< Type >::intersection ( const Rectangle< Type > &  other)

Returns a retangle that represents the intersection of this rectangle and another rectangle

Template Parameters
TypeThe type of the rectangle
Parameters
otherThe other rectangle
Returns
The intersection of the two rectangles

Definition at line 116 of file rectangle.h.

116 {
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 }
bool intersects(const Rectangle< Type > &)
Definition rectangle.h:88

References MaxOS::common::Rectangle< Type >::height, MaxOS::common::Rectangle< Type >::left, MaxOS::common::Rectangle< Type >::top, and MaxOS::common::Rectangle< Type >::width.

Referenced by MaxOS::gui::CompositeWidget::draw(), and MaxOS::gui::Window::draw_self().

◆ intersects()

template<class Type >
bool MaxOS::common::Rectangle< Type >::intersects ( const Rectangle< Type > &  other)

Checks if the rectangle intersects with another rectangle

Template Parameters
TypeThe type of the rectangle
Parameters
otherThe other rectangle
Returns
True if the rectangles intersect, false otherwise

Definition at line 88 of file rectangle.h.

88 {
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 }

References MaxOS::common::Rectangle< Type >::height, MaxOS::common::Rectangle< Type >::left, MaxOS::common::Rectangle< Type >::top, and MaxOS::common::Rectangle< Type >::width.

Referenced by MaxOS::gui::widgets::Button::draw(), MaxOS::gui::widgets::InputBox::draw(), MaxOS::gui::CompositeWidget::draw(), MaxOS::gui::Window::draw_self(), MaxOS::gui::Desktop::internal_invalidate(), and MaxOS::common::Rectangle< Type >::subtract().

◆ subtract()

template<class Type >
Vector< Rectangle< Type > > MaxOS::common::Rectangle< Type >::subtract ( const Rectangle< Type > &  other)

Subtracts a rectangle from this rectangle

Template Parameters
TypeThe type of the rectangle
Parameters
otherThe rectangle to subtract
Returns
A vector of rectangles that represent the subtraction

Definition at line 142 of file rectangle.h.

142 {
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>(
173 other.left + other.width, top,
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)
178 result.push_back(Rectangle<Type>(max_left, min_bottom,
180 top + height - min_bottom + 1));
181
182 return result;
183 }

References MaxOS::common::Rectangle< Type >::height, MaxOS::common::Rectangle< Type >::intersects(), MaxOS::common::Rectangle< Type >::left, MaxOS::common::Rectangle< Type >::Rectangle(), MaxOS::common::Rectangle< Type >::top, and MaxOS::common::Rectangle< Type >::width.

Referenced by MaxOS::gui::CompositeWidget::draw(), MaxOS::gui::Desktop::internal_invalidate(), and MaxOS::gui::Widget::resize().

Member Data Documentation

◆ height

◆ left

◆ top

◆ width


The documentation for this class was generated from the following file: