3 #include <fsdk/Types/Vector2.h>
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();
141 template <
typename OtherType>
143 if(reinterpret_cast<const void*>(
this) != reinterpret_cast<const void*>(&other)) {
144 x =
static_cast<Type
>(other.x);
145 y =
static_cast<Type
>(other.y);
146 width =
static_cast<Type
>(other.width);
147 height =
static_cast<Type
>(other.height);
156 template <
typename OtherType>
161 width == other.width &&
169 template <
typename OtherType>
171 return !(*
this == other);
180 newRect.
x =
x > other.x ?
x : other.x;
181 newRect.
y =
y > other.y ?
y : other.y;
182 newRect.
width = (
x +
width < other.x + other.width ?
x +
width : other.x + other.width) - newRect.
x;
183 newRect.
height = (
y +
height < other.y + other.height ?
y +
height : other.y + other.height) - newRect.
y;
195 *
this = *
this & other;
206 newRect.
x =
x < other.x ?
x : other.x;
207 newRect.
y =
y < other.y ?
y : other.y;
208 newRect.
width = (
x +
width > other.x + other.width ?
x +
width : other.x + other.width) - newRect.
x;
209 newRect.
height = (
y +
height > other.y + other.height ?
y +
height : other.y + other.height) - newRect.
y;
220 *
this = *
this & other;
231 newRect.
x =
static_cast<Type
>(
x * scaleFactor);
232 newRect.
y =
static_cast<Type
>(
y * scaleFactor);
233 newRect.
width =
static_cast<Type
>(
width * scaleFactor);
234 newRect.
height =
static_cast<Type
>(
height * scaleFactor);
245 newRect.
x =
static_cast<Type
>(
x / scaleFactor);
246 newRect.
y =
static_cast<Type
>(
y / scaleFactor);
247 newRect.
width =
static_cast<Type
>(
width / scaleFactor);
248 newRect.
height =
static_cast<Type
>(
height / scaleFactor);
261 BaseRect operator - (Vector2<typename BaseRect::ElementType> vec) {
302 Type
top() const noexcept {
return y; }
312 Type
left() const noexcept {
return x; }
337 void adjust(Type dx, Type dy, Type dw, Type dh) noexcept {
353 rect.
adjust(dx, dy, dw, dh);
368 topLeft() >= other.topLeft() &&
388 template <
typename Type>
391 out.
x = in.x - 0.5*(scaleFactor-1)*in.width;
392 out.
y = in.y - 0.5*(scaleFactor-1)*in.height;
393 out.
width = in.width * scaleFactor;
394 out.
height = in.height * scaleFactor;
398 using Rect = BaseRect<int>;
399 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:272
bool inside(const BaseRect &other) const noexcept
Checks whether this rect is inside of another rect.
Definition: Rect.h:366
static BaseRect coords(Type x0, Type y0, Type x1, Type y1) noexcept
Create new Rect by coordinates.
Definition: Rect.h:101
static float intersectionOverForeground(const BaseRect &rect1, const BaseRect &rect2)
Calculates rects intersection rate over foreground.
Definition: Rect.h:130
Vector2< Type > center() const noexcept
Gets rect center coordinates.
Definition: Rect.h:286
Type left() const noexcept
Gets rect left x coordinate.
Definition: Rect.h:312
BaseRect & operator=(const BaseRect< OtherType > &other) noexcept
Copies another rect.
Definition: Rect.h:142
Type width
Rectangle width.
Definition: Rect.h:15
Type getArea() const noexcept
Computes rect area (width x height).
Definition: Rect.h:360
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:378
void adjust(Type dx, Type dy, Type dw, Type dh) noexcept
Adjusts the rect by given amounts.
Definition: Rect.h:337
BaseRect operator|=(const BaseRect &other) noexcept
Returns minimum area rectangle containing both rects.
Definition: Rect.h:218
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:229
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:279
bool operator!=(const BaseRect< OtherType > &other) const noexcept
Checks whether two rects are not equal.
Definition: Rect.h:170
Type bottom() const noexcept
Gets rect bottom y coordinate.
Definition: Rect.h:307
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:204
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:157
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:389
BaseRect operator/(float scaleFactor) const noexcept
Divides Rect scale by specified scale factor.
Definition: Rect.h:243
BaseRect adjusted(Type dx, Type dy, Type dw, Type dh) const noexcept
Copies and adjusts the rect by given amounts.
Definition: Rect.h:351
Vector2< Type > bottomRight() const noexcept
Gets rect bottom-right corner coordinates.
Definition: Rect.h:295
BaseRect operator&=(const BaseRect &other) noexcept
Returns rect that is intersection of rects.
Definition: Rect.h:193
BaseRect operator&(const BaseRect &other) const noexcept
Returns rect that is intersection of rects.
Definition: Rect.h:178
Rectangle.
Definition: Rect.h:10
Type top() const noexcept
Gets rect top y coordinate.
Definition: Rect.h:302
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:323
Type right() const noexcept
Gets rect right x coordinate.
Definition: Rect.h:317
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