Also did Rect2i.

This commit is contained in:
Relintai 2022-08-14 14:00:32 +02:00
parent 899ac5cbd8
commit 662c3a7e88
4 changed files with 70 additions and 9 deletions

View File

@ -30,3 +30,7 @@
#include "core/math/transform_2d.h" // Includes rect2.h but Rect2 needs Transform2D #include "core/math/transform_2d.h" // Includes rect2.h but Rect2 needs Transform2D
Rect2i::operator String() const {
return "[P: " + position.operator String() + ", S: " + size + "]";
}

View File

@ -72,6 +72,7 @@ struct _NO_DISCARD_CLASS_ Rect2i {
_FORCE_INLINE_ bool has_no_area() const { _FORCE_INLINE_ bool has_no_area() const {
return (size.x <= 0 || size.y <= 0); return (size.x <= 0 || size.y <= 0);
} }
inline Rect2i clip(const Rect2i &p_rect) const { /// return a clipped rect inline Rect2i clip(const Rect2i &p_rect) const { /// return a clipped rect
Rect2i new_rect = p_rect; Rect2i new_rect = p_rect;
@ -92,6 +93,26 @@ struct _NO_DISCARD_CLASS_ Rect2i {
return new_rect; return new_rect;
} }
// Returns the instersection between two Rect2is or an empty Rect2i if there is no intersection
inline Rect2i intersection(const Rect2i &p_rect) const {
Rect2i new_rect = p_rect;
if (!intersects(new_rect)) {
return Rect2i();
}
new_rect.position.x = MAX(p_rect.position.x, position.x);
new_rect.position.y = MAX(p_rect.position.y, position.y);
Point2i p_rect_end = p_rect.position + p_rect.size;
Point2i end = position + size;
new_rect.size.x = MIN(p_rect_end.x, end.x) - new_rect.position.x;
new_rect.size.y = MIN(p_rect_end.y, end.y) - new_rect.position.y;
return new_rect;
}
inline Rect2i merge(const Rect2i &p_rect) const { ///< return a merged rect inline Rect2i merge(const Rect2i &p_rect) const { ///< return a merged rect
Rect2i new_rect; Rect2i new_rect;
@ -105,8 +126,9 @@ struct _NO_DISCARD_CLASS_ Rect2i {
new_rect.size = new_rect.size - new_rect.position; //make relative again new_rect.size = new_rect.size - new_rect.position; //make relative again
return new_rect; return new_rect;
}; }
bool has_point(const Point2 &p_point) const {
bool has_point(const Point2i &p_point) const {
if (p_point.x < position.x) { if (p_point.x < position.x) {
return false; return false;
} }
@ -145,6 +167,15 @@ struct _NO_DISCARD_CLASS_ Rect2i {
return g; return g;
} }
inline Rect2i grow_side(Side p_side, int p_amount) const {
Rect2i g = *this;
g = g.grow_individual((SIDE_LEFT == p_side) ? p_amount : 0,
(SIDE_TOP == p_side) ? p_amount : 0,
(SIDE_RIGHT == p_side) ? p_amount : 0,
(SIDE_BOTTOM == p_side) ? p_amount : 0);
return g;
}
inline Rect2i grow_individual(int p_left, int p_top, int p_right, int p_bottom) const { inline Rect2i grow_individual(int p_left, int p_top, int p_right, int p_bottom) const {
Rect2i g = *this; Rect2i g = *this;
g.position.x -= p_left; g.position.x -= p_left;
@ -183,9 +214,21 @@ struct _NO_DISCARD_CLASS_ Rect2i {
size = end - begin; size = end - begin;
} }
operator String() const { return String(position) + ", " + String(size); } _FORCE_INLINE_ Rect2i abs() const {
return Rect2i(Point2i(position.x + MIN(size.x, 0), position.y + MIN(size.y, 0)), size.abs());
}
_FORCE_INLINE_ void set_end(const Vector2i &p_end) {
size = p_end - position;
}
_FORCE_INLINE_ Vector2i get_end() const {
return position + size;
}
operator String() const;
operator Rect2() const { return Rect2(position, size); } operator Rect2() const { return Rect2(position, size); }
Rect2i(const Rect2 &p_r2) : Rect2i(const Rect2 &p_r2) :
position(p_r2.position), position(p_r2.position),
size(p_r2.size) { size(p_r2.size) {

View File

@ -95,6 +95,8 @@ struct _NO_DISCARD_CLASS_ Vector2i {
real_t get_aspect() const { return width / (real_t)height; } real_t get_aspect() const { return width / (real_t)height; }
Vector2i abs() const { return Vector2i(ABS(x), ABS(y)); }
Vector2 to_vector2() const { return Vector2(x, y); } Vector2 to_vector2() const { return Vector2(x, y); }
operator String() const { return String::num(x) + ", " + String::num(y); } operator String() const { return String::num(x) + ", " + String::num(y); }

View File

@ -493,16 +493,22 @@ struct _VariantCall {
VCALL_LOCALMEM0R(Rect2i, get_area); VCALL_LOCALMEM0R(Rect2i, get_area);
VCALL_LOCALMEM0R(Rect2i, get_center); VCALL_LOCALMEM0R(Rect2i, get_center);
VCALL_LOCALMEM0R(Rect2i, has_no_area);
VCALL_LOCALMEM1R(Rect2i, has_point);
VCALL_LOCALMEM1R(Rect2i, intersects); VCALL_LOCALMEM1R(Rect2i, intersects);
VCALL_LOCALMEM1R(Rect2i, encloses); VCALL_LOCALMEM1R(Rect2i, encloses);
VCALL_LOCALMEM0R(Rect2i, has_no_area);
VCALL_LOCALMEM1R(Rect2i, clip); VCALL_LOCALMEM1R(Rect2i, clip);
VCALL_LOCALMEM1R(Rect2i, intersection);
VCALL_LOCALMEM1R(Rect2i, merge); VCALL_LOCALMEM1R(Rect2i, merge);
VCALL_LOCALMEM1R(Rect2i, expand); VCALL_LOCALMEM1R(Rect2i, has_point);
VCALL_LOCALMEM1R(Rect2i, grow); VCALL_LOCALMEM1R(Rect2i, grow);
VCALL_LOCALMEM2R(Rect2i, grow_margin); VCALL_LOCALMEM2R(Rect2i, grow_margin);
VCALL_LOCALMEM2R(Rect2i, grow_side);
VCALL_LOCALMEM4R(Rect2i, grow_individual); VCALL_LOCALMEM4R(Rect2i, grow_individual);
VCALL_LOCALMEM1R(Rect2i, expand);
VCALL_LOCALMEM1(Rect2i, expand_to);
VCALL_LOCALMEM0R(Rect2i, abs);
VCALL_LOCALMEM1(Rect2i, set_end);
VCALL_LOCALMEM0R(Rect2i, get_end);
VCALL_LOCALMEM0R(Vector3, min_axis); VCALL_LOCALMEM0R(Vector3, min_axis);
VCALL_LOCALMEM0R(Vector3, max_axis); VCALL_LOCALMEM0R(Vector3, max_axis);
@ -2218,16 +2224,22 @@ void register_variant_methods() {
ADDFUNC0R(RECT2I, INT, Rect2i, get_area, varray()); ADDFUNC0R(RECT2I, INT, Rect2i, get_area, varray());
ADDFUNC0R(RECT2I, VECTOR2I, Rect2i, get_center, varray()); ADDFUNC0R(RECT2I, VECTOR2I, Rect2i, get_center, varray());
ADDFUNC0R(RECT2I, BOOL, Rect2i, has_no_area, varray());
ADDFUNC1R(RECT2I, BOOL, Rect2i, has_point, VECTOR2, "point", varray());
ADDFUNC1R(RECT2I, BOOL, Rect2i, intersects, RECT2I, "b", varray()); ADDFUNC1R(RECT2I, BOOL, Rect2i, intersects, RECT2I, "b", varray());
ADDFUNC1R(RECT2I, BOOL, Rect2i, encloses, RECT2I, "b", varray()); ADDFUNC1R(RECT2I, BOOL, Rect2i, encloses, RECT2I, "b", varray());
ADDFUNC0R(RECT2I, BOOL, Rect2i, has_no_area, varray());
ADDFUNC1R(RECT2I, RECT2I, Rect2i, clip, RECT2I, "b", varray()); ADDFUNC1R(RECT2I, RECT2I, Rect2i, clip, RECT2I, "b", varray());
ADDFUNC1R(RECT2I, RECT2I, Rect2i, intersection, RECT2I, "rect", varray());
ADDFUNC1R(RECT2I, RECT2I, Rect2i, merge, RECT2I, "b", varray()); ADDFUNC1R(RECT2I, RECT2I, Rect2i, merge, RECT2I, "b", varray());
ADDFUNC1R(RECT2I, RECT2I, Rect2i, expand, VECTOR2I, "to", varray()); ADDFUNC1R(RECT2I, BOOL, Rect2i, has_point, VECTOR2I, "point", varray());
ADDFUNC1R(RECT2I, RECT2I, Rect2i, grow, INT, "by", varray()); ADDFUNC1R(RECT2I, RECT2I, Rect2i, grow, INT, "by", varray());
ADDFUNC2R(RECT2I, RECT2I, Rect2i, grow_margin, INT, "margin", INT, "by", varray()); ADDFUNC2R(RECT2I, RECT2I, Rect2i, grow_margin, INT, "margin", INT, "by", varray());
ADDFUNC2R(RECT2I, RECT2I, Rect2i, grow_side, INT, "side", INT, "by", varray());
ADDFUNC4R(RECT2I, RECT2I, Rect2i, grow_individual, INT, "left", INT, "top", INT, "right", INT, " bottom", varray()); ADDFUNC4R(RECT2I, RECT2I, Rect2i, grow_individual, INT, "left", INT, "top", INT, "right", INT, " bottom", varray());
ADDFUNC1R(RECT2I, RECT2I, Rect2i, expand, VECTOR2I, "to", varray());
ADDFUNC1R(RECT2I, RECT2I, Rect2i, expand_to, VECTOR2I, "to", varray());
ADDFUNC0R(RECT2I, RECT2I, Rect2i, abs, varray());
ADDFUNC1(RECT2I, NIL, Rect2i, set_end, VECTOR2I, "end", varray());
ADDFUNC0R(RECT2I, VECTOR2I, Rect2i, get_end, varray());
ADDFUNC0R(VECTOR3, INT, Vector3, min_axis, varray()); ADDFUNC0R(VECTOR3, INT, Vector3, min_axis, varray());
ADDFUNC0R(VECTOR3, INT, Vector3, max_axis, varray()); ADDFUNC0R(VECTOR3, INT, Vector3, max_axis, varray());