9 template <
typename Type>
11 typedef Type ElementType;
62 template <
typename OtherType>
70 const auto r =
right();
102 return BaseRect(x0, y0, x1-x0, y1-y0);
111 const float inter = (rect1 & rect2).
getArea();
112 return inter / rect1.
getArea();
121 const float inter = (rect1 & rect2).
getArea();
129 template <
typename OtherType>
131 if(reinterpret_cast<const void*>(
this) != reinterpret_cast<const void*>(&other)) {
132 x =
static_cast<Type
>(other.x);
133 y =
static_cast<Type
>(other.y);
134 width =
static_cast<Type
>(other.width);
135 height =
static_cast<Type
>(other.height);
144 template <
typename OtherType>
149 width == other.width &&
157 template <
typename OtherType>
159 return !(*
this == other);
168 newRect.
x =
x > other.x ?
x : other.x;
169 newRect.
y =
y > other.y ?
y : other.y;
170 newRect.
width = (
x +
width < other.x + other.width ?
x +
width : other.x + other.width) - newRect.
x;
171 newRect.
height = (
y +
height < other.y + other.height ?
y +
height : other.y + other.height) - newRect.
y;
183 *
this = *
this & other;
194 newRect.
x =
x < other.x ?
x : other.x;
195 newRect.
y =
y < other.y ?
y : other.y;
196 newRect.
width = (
x +
width > other.x + other.width ?
x +
width : other.x + other.width) - newRect.
x;
197 newRect.
height = (
y +
height > other.y + other.height ?
y +
height : other.y + other.height) - newRect.
y;
208 *
this = *
this & other;
219 newRect.
x =
static_cast<Type
>(
x * scaleFactor);
220 newRect.
y =
static_cast<Type
>(
y * scaleFactor);
221 newRect.
width =
static_cast<Type
>(
width * scaleFactor);
222 newRect.
height =
static_cast<Type
>(
height * scaleFactor);
233 newRect.
x =
static_cast<Type
>(
x / scaleFactor);
234 newRect.
y =
static_cast<Type
>(
y / scaleFactor);
235 newRect.
width =
static_cast<Type
>(
width / scaleFactor);
236 newRect.
height =
static_cast<Type
>(
height / scaleFactor);
249 BaseRect operator - (Vector2<typename BaseRect::ElementType> vec) {
290 Type
top() const noexcept {
return y; }
300 Type
left() const noexcept {
return x; }
325 void adjust(Type dx, Type dy, Type dw, Type dh) noexcept {
341 rect.
adjust(dx, dy, dw, dh);
356 topLeft() >= other.topLeft() &&
376 template <
typename Type>
379 out.
x = in.x - 0.5*(scaleFactor-1)*in.width;
380 out.
y = in.y - 0.5*(scaleFactor-1)*in.height;
381 out.
width = in.width * scaleFactor;
382 out.
height = in.height * scaleFactor;
386 using Rect = BaseRect<int>;
387 using FloatRect = BaseRect<float>;
void setBottom(Type b) noexcept
Set rect bottom.
Definition: Rect.h:91
Type height
Rectangle height.
Definition: Rect.h:16
BaseRect() noexcept
Initializes a default invalid rectangle.
Definition: Rect.h:20
Vector2< Type > size() const noexcept
Gets rect size (width, height).
Definition: Rect.h:260
bool inside(const BaseRect &other) const noexcept
Checks whether this rect is inside of another rect.
Definition: Rect.h:354
static BaseRect coords(Type x0, Type y0, Type x1, Type y1) noexcept
Create new Rect by coordinates.
Definition: Rect.h:101
Vector2< Type > center() const noexcept
Gets rect center coordinates.
Definition: Rect.h:274
Type left() const noexcept
Gets rect left x coordinate.
Definition: Rect.h:300
BaseRect & operator=(const BaseRect< OtherType > &other) noexcept
Copies another rect.
Definition: Rect.h:130
Type width
Rectangle width.
Definition: Rect.h:15
Type getArea() const noexcept
Computes rect area (width x height).
Definition: Rect.h:348
void setLeft(Type l) noexcept
Set rect left.
Definition: Rect.h:69
T x
x coordinate.
Definition: Vector2.h:12
BaseRect(Type x_, Type y_, Type w_, Type h_) noexcept
Initializes a rectangle with given values.
Definition: Rect.h:33
bool isValid() const noexcept
Checks whether a rect is valid.
Definition: Rect.h:366
void adjust(Type dx, Type dy, Type dw, Type dh) noexcept
Adjusts the rect by given amounts.
Definition: Rect.h:325
BaseRect operator|=(const BaseRect &other) noexcept
Returns minimum area rectangle containing both rects.
Definition: Rect.h:206
BaseRect(const BaseRect &other) noexcept
Copies another rect.
Definition: Rect.h:58
BaseRect operator*(float scaleFactor) const noexcept
Multiplicates Rect scale by specified scale factor.
Definition: Rect.h:217
BaseRect(const Vector2< Type > &topLeft, const Vector2< Type > &bottomRight) noexcept
Initializes a rectangle with given values.
Definition: Rect.h:48
Vector2< Type > topLeft() const noexcept
Gets rect top-left corner coordinates.
Definition: Rect.h:267
bool operator!=(const BaseRect< OtherType > &other) const noexcept
Checks whether two rects are not equal.
Definition: Rect.h:158
Type bottom() const noexcept
Gets rect bottom y coordinate.
Definition: Rect.h:295
static float intersectionOverFirstRect(const BaseRect &rect1, const BaseRect &rect2)
Calculates rects intersection rate over first rect.
Definition: Rect.h:110
BaseRect operator|(const BaseRect &other) const noexcept
Returns minimum area rectangle containing both rects.
Definition: Rect.h:192
void setRight(Type r) noexcept
Set rect right.
Definition: Rect.h:85
bool operator==(const BaseRect< OtherType > &other) const noexcept
Checks whether two rects are equal.
Definition: Rect.h:145
void setTop(Type t) noexcept
Set rect top.
Definition: Rect.h:77
static float intersectionOverUnion(const BaseRect &rect1, const BaseRect &rect2)
Calculates rects intersection rate over union.
Definition: Rect.h:120
BaseRect< Type > centerScale(const BaseRect< Type > &in, float scaleFactor) noexcept
scale rect out of center
Definition: Rect.h:377
BaseRect operator/(float scaleFactor) const noexcept
Divides Rect scale by specified scale factor.
Definition: Rect.h:231
BaseRect adjusted(Type dx, Type dy, Type dw, Type dh) const noexcept
Copies and adjusts the rect by given amounts.
Definition: Rect.h:339
Vector2< Type > bottomRight() const noexcept
Gets rect bottom-right corner coordinates.
Definition: Rect.h:283
BaseRect operator&=(const BaseRect &other) noexcept
Returns rect that is intersection of rects.
Definition: Rect.h:181
BaseRect operator&(const BaseRect &other) const noexcept
Returns rect that is intersection of rects.
Definition: Rect.h:166
Rectangle.
Definition: Rect.h:10
Type top() const noexcept
Gets rect top y coordinate.
Definition: Rect.h:290
T y
y coordinate.
Definition: Vector2.h:13
void set(const Vector2< Type > &topLeft, const Vector2< Type > &bottomRight) noexcept
Sets rect corner coordinates.
Definition: Rect.h:311
Type right() const noexcept
Gets rect right x coordinate.
Definition: Rect.h:305
Type y
Upper left corner y-coordinate.
Definition: Rect.h:14
Generic 2D vector.
Definition: Vector2.h:11
Type x
Upper left corner x-coordinate.
Definition: Rect.h:13