commit
262d53c05e
|
@ -18,24 +18,24 @@ struct Transform2D;
|
||||||
|
|
||||||
struct Rect2 {
|
struct Rect2 {
|
||||||
|
|
||||||
Point2 pos;
|
Point2 position;
|
||||||
Size2 size;
|
Size2 size;
|
||||||
|
|
||||||
inline const Vector2 &get_pos() const { return pos; }
|
inline const Vector2 &get_position() const { return position; }
|
||||||
inline void set_pos(const Vector2 &p_pos) { pos = p_pos; }
|
inline void set_position(const Vector2 &p_position) { position = p_position; }
|
||||||
inline const Vector2 &get_size() const { return size; }
|
inline const Vector2 &get_size() const { return size; }
|
||||||
inline void set_size(const Vector2 &p_size) { size = p_size; }
|
inline void set_size(const Vector2 &p_size) { size = p_size; }
|
||||||
|
|
||||||
inline real_t get_area() const { return size.width * size.height; }
|
inline real_t get_area() const { return size.width * size.height; }
|
||||||
|
|
||||||
inline bool intersects(const Rect2 &p_rect) const {
|
inline bool intersects(const Rect2 &p_rect) const {
|
||||||
if (pos.x >= (p_rect.pos.x + p_rect.size.width))
|
if (position.x >= (p_rect.position.x + p_rect.size.width))
|
||||||
return false;
|
return false;
|
||||||
if ((pos.x + size.width) <= p_rect.pos.x)
|
if ((position.x + size.width) <= p_rect.position.x)
|
||||||
return false;
|
return false;
|
||||||
if (pos.y >= (p_rect.pos.y + p_rect.size.height))
|
if (position.y >= (p_rect.position.y + p_rect.size.height))
|
||||||
return false;
|
return false;
|
||||||
if ((pos.y + size.height) <= p_rect.pos.y)
|
if ((position.y + size.height) <= p_rect.position.y)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -45,13 +45,13 @@ struct Rect2 {
|
||||||
|
|
||||||
bool intersects_transformed(const Transform2D &p_xform, const Rect2 &p_rect) const;
|
bool intersects_transformed(const Transform2D &p_xform, const Rect2 &p_rect) const;
|
||||||
|
|
||||||
bool intersects_segment(const Point2 &p_from, const Point2 &p_to, Point2 *r_pos = nullptr, Point2 *r_normal = nullptr) const;
|
bool intersects_segment(const Point2 &p_from, const Point2 &p_to, Point2 *r_position = nullptr, Point2 *r_normal = nullptr) const;
|
||||||
|
|
||||||
inline bool encloses(const Rect2 &p_rect) const {
|
inline bool encloses(const Rect2 &p_rect) const {
|
||||||
|
|
||||||
return (p_rect.pos.x >= pos.x) && (p_rect.pos.y >= pos.y) &&
|
return (p_rect.position.x >= position.x) && (p_rect.position.y >= position.y) &&
|
||||||
((p_rect.pos.x + p_rect.size.x) < (pos.x + size.x)) &&
|
((p_rect.position.x + p_rect.size.x) < (position.x + size.x)) &&
|
||||||
((p_rect.pos.y + p_rect.size.y) < (pos.y + size.y));
|
((p_rect.position.y + p_rect.size.y) < (position.y + size.y));
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool has_no_area() const {
|
inline bool has_no_area() const {
|
||||||
|
@ -63,14 +63,14 @@ struct Rect2 {
|
||||||
Rect2 merge(const Rect2 &p_rect) const;
|
Rect2 merge(const Rect2 &p_rect) const;
|
||||||
|
|
||||||
inline bool has_point(const Point2 &p_point) const {
|
inline bool has_point(const Point2 &p_point) const {
|
||||||
if (p_point.x < pos.x)
|
if (p_point.x < position.x)
|
||||||
return false;
|
return false;
|
||||||
if (p_point.y < pos.y)
|
if (p_point.y < position.y)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (p_point.x >= (pos.x + size.x))
|
if (p_point.x >= (position.x + size.x))
|
||||||
return false;
|
return false;
|
||||||
if (p_point.y >= (pos.y + size.y))
|
if (p_point.y >= (position.y + size.y))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -78,14 +78,14 @@ struct Rect2 {
|
||||||
|
|
||||||
inline bool no_area() const { return (size.width <= 0 || size.height <= 0); }
|
inline bool no_area() const { return (size.width <= 0 || size.height <= 0); }
|
||||||
|
|
||||||
inline bool operator==(const Rect2 &p_rect) const { return pos == p_rect.pos && size == p_rect.size; }
|
inline bool operator==(const Rect2 &p_rect) const { return position == p_rect.position && size == p_rect.size; }
|
||||||
inline bool operator!=(const Rect2 &p_rect) const { return pos != p_rect.pos || size != p_rect.size; }
|
inline bool operator!=(const Rect2 &p_rect) const { return position != p_rect.position || size != p_rect.size; }
|
||||||
|
|
||||||
inline Rect2 grow(real_t p_by) const {
|
inline Rect2 grow(real_t p_by) const {
|
||||||
|
|
||||||
Rect2 g = *this;
|
Rect2 g = *this;
|
||||||
g.pos.x -= p_by;
|
g.position.x -= p_by;
|
||||||
g.pos.y -= p_by;
|
g.position.y -= p_by;
|
||||||
g.size.width += p_by * 2;
|
g.size.width += p_by * 2;
|
||||||
g.size.height += p_by * 2;
|
g.size.height += p_by * 2;
|
||||||
return g;
|
return g;
|
||||||
|
@ -100,8 +100,8 @@ struct Rect2 {
|
||||||
|
|
||||||
inline void expand_to(const Vector2 &p_vector) { //in place function for speed
|
inline void expand_to(const Vector2 &p_vector) { //in place function for speed
|
||||||
|
|
||||||
Vector2 begin = pos;
|
Vector2 begin = position;
|
||||||
Vector2 end = pos + size;
|
Vector2 end = position + size;
|
||||||
|
|
||||||
if (p_vector.x < begin.x)
|
if (p_vector.x < begin.x)
|
||||||
begin.x = p_vector.x;
|
begin.x = p_vector.x;
|
||||||
|
@ -113,7 +113,7 @@ struct Rect2 {
|
||||||
if (p_vector.y > end.y)
|
if (p_vector.y > end.y)
|
||||||
end.y = p_vector.y;
|
end.y = p_vector.y;
|
||||||
|
|
||||||
pos = begin;
|
position = begin;
|
||||||
size = end - begin;
|
size = end - begin;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -121,11 +121,11 @@ struct Rect2 {
|
||||||
|
|
||||||
inline Rect2() {}
|
inline Rect2() {}
|
||||||
inline Rect2(real_t p_x, real_t p_y, real_t p_width, real_t p_height) {
|
inline Rect2(real_t p_x, real_t p_y, real_t p_width, real_t p_height) {
|
||||||
pos = Point2(p_x, p_y);
|
position = Point2(p_x, p_y);
|
||||||
size = Size2(p_width, p_height);
|
size = Size2(p_width, p_height);
|
||||||
}
|
}
|
||||||
inline Rect2(const Point2 &p_pos, const Size2 &p_size) {
|
inline Rect2(const Point2 &p_position, const Size2 &p_size) {
|
||||||
pos = p_pos;
|
position = p_position;
|
||||||
size = p_size;
|
size = p_size;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -19,17 +19,17 @@ real_t Rect2::distance_to(const Vector2 &p_point) const {
|
||||||
|
|
||||||
real_t dist = 1e20;
|
real_t dist = 1e20;
|
||||||
|
|
||||||
if (p_point.x < pos.x) {
|
if (p_point.x < position.x) {
|
||||||
dist = MIN(dist, pos.x - p_point.x);
|
dist = MIN(dist, position.x - p_point.x);
|
||||||
}
|
}
|
||||||
if (p_point.y < pos.y) {
|
if (p_point.y < position.y) {
|
||||||
dist = MIN(dist, pos.y - p_point.y);
|
dist = MIN(dist, position.y - p_point.y);
|
||||||
}
|
}
|
||||||
if (p_point.x >= (pos.x + size.x)) {
|
if (p_point.x >= (position.x + size.x)) {
|
||||||
dist = MIN(p_point.x - (pos.x + size.x), dist);
|
dist = MIN(p_point.x - (position.x + size.x), dist);
|
||||||
}
|
}
|
||||||
if (p_point.y >= (pos.y + size.y)) {
|
if (p_point.y >= (position.y + size.y)) {
|
||||||
dist = MIN(p_point.y - (pos.y + size.y), dist);
|
dist = MIN(p_point.y - (position.y + size.y), dist);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dist == 1e20)
|
if (dist == 1e20)
|
||||||
|
@ -45,14 +45,14 @@ Rect2 Rect2::clip(const Rect2 &p_rect) const { /// return a clipped rect
|
||||||
if (!intersects(new_rect))
|
if (!intersects(new_rect))
|
||||||
return Rect2();
|
return Rect2();
|
||||||
|
|
||||||
new_rect.pos.x = MAX(p_rect.pos.x, pos.x);
|
new_rect.position.x = MAX(p_rect.position.x, position.x);
|
||||||
new_rect.pos.y = MAX(p_rect.pos.y, pos.y);
|
new_rect.position.y = MAX(p_rect.position.y, position.y);
|
||||||
|
|
||||||
Point2 p_rect_end = p_rect.pos + p_rect.size;
|
Point2 p_rect_end = p_rect.position + p_rect.size;
|
||||||
Point2 end = pos + size;
|
Point2 end = position + size;
|
||||||
|
|
||||||
new_rect.size.x = MIN(p_rect_end.x, end.x) - new_rect.pos.x;
|
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.pos.y;
|
new_rect.size.y = MIN(p_rect_end.y, end.y) - new_rect.position.y;
|
||||||
|
|
||||||
return new_rect;
|
return new_rect;
|
||||||
}
|
}
|
||||||
|
@ -61,22 +61,22 @@ Rect2 Rect2::merge(const Rect2 &p_rect) const { ///< return a merged rect
|
||||||
|
|
||||||
Rect2 new_rect;
|
Rect2 new_rect;
|
||||||
|
|
||||||
new_rect.pos.x = MIN(p_rect.pos.x, pos.x);
|
new_rect.position.x = MIN(p_rect.position.x, position.x);
|
||||||
new_rect.pos.y = MIN(p_rect.pos.y, pos.y);
|
new_rect.position.y = MIN(p_rect.position.y, position.y);
|
||||||
|
|
||||||
new_rect.size.x = MAX(p_rect.pos.x + p_rect.size.x, pos.x + size.x);
|
new_rect.size.x = MAX(p_rect.position.x + p_rect.size.x, position.x + size.x);
|
||||||
new_rect.size.y = MAX(p_rect.pos.y + p_rect.size.y, pos.y + size.y);
|
new_rect.size.y = MAX(p_rect.position.y + p_rect.size.y, position.y + size.y);
|
||||||
|
|
||||||
new_rect.size = new_rect.size - new_rect.pos; //make relative again
|
new_rect.size = new_rect.size - new_rect.position; //make relative again
|
||||||
|
|
||||||
return new_rect;
|
return new_rect;
|
||||||
}
|
}
|
||||||
|
|
||||||
Rect2::operator String() const {
|
Rect2::operator String() const {
|
||||||
return String(pos) + ", " + String(size);
|
return String(position) + ", " + String(size);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Rect2::intersects_segment(const Point2 &p_from, const Point2 &p_to, Point2 *r_pos, Point2 *r_normal) const {
|
bool Rect2::intersects_segment(const Point2 &p_from, const Point2 &p_to, Point2 *r_position, Point2 *r_normal) const {
|
||||||
|
|
||||||
real_t min = 0, max = 1;
|
real_t min = 0, max = 1;
|
||||||
int axis = 0;
|
int axis = 0;
|
||||||
|
@ -85,7 +85,7 @@ bool Rect2::intersects_segment(const Point2 &p_from, const Point2 &p_to, Point2
|
||||||
for (int i = 0; i < 2; i++) {
|
for (int i = 0; i < 2; i++) {
|
||||||
real_t seg_from = p_from[i];
|
real_t seg_from = p_from[i];
|
||||||
real_t seg_to = p_to[i];
|
real_t seg_to = p_to[i];
|
||||||
real_t box_begin = pos[i];
|
real_t box_begin = position[i];
|
||||||
real_t box_end = box_begin + size[i];
|
real_t box_end = box_begin + size[i];
|
||||||
real_t cmin, cmax;
|
real_t cmin, cmax;
|
||||||
real_t csign;
|
real_t csign;
|
||||||
|
@ -128,8 +128,8 @@ bool Rect2::intersects_segment(const Point2 &p_from, const Point2 &p_to, Point2
|
||||||
*r_normal = normal;
|
*r_normal = normal;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (r_pos)
|
if (r_position)
|
||||||
*r_pos = p_from + rel * min;
|
*r_position = p_from + rel * min;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -139,30 +139,30 @@ bool Rect2::intersects_transformed(const Transform2D &p_xform, const Rect2 &p_re
|
||||||
//SAT intersection between local and transformed rect2
|
//SAT intersection between local and transformed rect2
|
||||||
|
|
||||||
Vector2 xf_points[4] = {
|
Vector2 xf_points[4] = {
|
||||||
p_xform.xform(p_rect.pos),
|
p_xform.xform(p_rect.position),
|
||||||
p_xform.xform(Vector2(p_rect.pos.x + p_rect.size.x, p_rect.pos.y)),
|
p_xform.xform(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y)),
|
||||||
p_xform.xform(Vector2(p_rect.pos.x, p_rect.pos.y + p_rect.size.y)),
|
p_xform.xform(Vector2(p_rect.position.x, p_rect.position.y + p_rect.size.y)),
|
||||||
p_xform.xform(Vector2(p_rect.pos.x + p_rect.size.x, p_rect.pos.y + p_rect.size.y)),
|
p_xform.xform(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y + p_rect.size.y)),
|
||||||
};
|
};
|
||||||
|
|
||||||
real_t low_limit;
|
real_t low_limit;
|
||||||
|
|
||||||
//base rect2 first (faster)
|
//base rect2 first (faster)
|
||||||
|
|
||||||
if (xf_points[0].y > pos.y)
|
if (xf_points[0].y > position.y)
|
||||||
goto next1;
|
goto next1;
|
||||||
if (xf_points[1].y > pos.y)
|
if (xf_points[1].y > position.y)
|
||||||
goto next1;
|
goto next1;
|
||||||
if (xf_points[2].y > pos.y)
|
if (xf_points[2].y > position.y)
|
||||||
goto next1;
|
goto next1;
|
||||||
if (xf_points[3].y > pos.y)
|
if (xf_points[3].y > position.y)
|
||||||
goto next1;
|
goto next1;
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
next1:
|
next1:
|
||||||
|
|
||||||
low_limit = pos.y + size.y;
|
low_limit = position.y + size.y;
|
||||||
|
|
||||||
if (xf_points[0].y < low_limit)
|
if (xf_points[0].y < low_limit)
|
||||||
goto next2;
|
goto next2;
|
||||||
|
@ -177,20 +177,20 @@ next1:
|
||||||
|
|
||||||
next2:
|
next2:
|
||||||
|
|
||||||
if (xf_points[0].x > pos.x)
|
if (xf_points[0].x > position.x)
|
||||||
goto next3;
|
goto next3;
|
||||||
if (xf_points[1].x > pos.x)
|
if (xf_points[1].x > position.x)
|
||||||
goto next3;
|
goto next3;
|
||||||
if (xf_points[2].x > pos.x)
|
if (xf_points[2].x > position.x)
|
||||||
goto next3;
|
goto next3;
|
||||||
if (xf_points[3].x > pos.x)
|
if (xf_points[3].x > position.x)
|
||||||
goto next3;
|
goto next3;
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
next3:
|
next3:
|
||||||
|
|
||||||
low_limit = pos.x + size.x;
|
low_limit = position.x + size.x;
|
||||||
|
|
||||||
if (xf_points[0].x < low_limit)
|
if (xf_points[0].x < low_limit)
|
||||||
goto next4;
|
goto next4;
|
||||||
|
@ -206,10 +206,10 @@ next3:
|
||||||
next4:
|
next4:
|
||||||
|
|
||||||
Vector2 xf_points2[4] = {
|
Vector2 xf_points2[4] = {
|
||||||
pos,
|
position,
|
||||||
Vector2(pos.x + size.x, pos.y),
|
Vector2(position.x + size.x, position.y),
|
||||||
Vector2(pos.x, pos.y + size.y),
|
Vector2(position.x, position.y + size.y),
|
||||||
Vector2(pos.x + size.x, pos.y + size.y),
|
Vector2(position.x + size.x, position.y + size.y),
|
||||||
};
|
};
|
||||||
|
|
||||||
real_t maxa = p_xform.elements[0].dot(xf_points2[0]);
|
real_t maxa = p_xform.elements[0].dot(xf_points2[0]);
|
||||||
|
|
|
@ -50,13 +50,13 @@ Rect2 Transform2D::xform(const Rect2 &p_rect) const {
|
||||||
|
|
||||||
Vector2 x = elements[0] * p_rect.size.x;
|
Vector2 x = elements[0] * p_rect.size.x;
|
||||||
Vector2 y = elements[1] * p_rect.size.y;
|
Vector2 y = elements[1] * p_rect.size.y;
|
||||||
Vector2 pos = xform(p_rect.pos);
|
Vector2 position = xform(p_rect.position);
|
||||||
|
|
||||||
Rect2 new_rect;
|
Rect2 new_rect;
|
||||||
new_rect.pos = pos;
|
new_rect.position = position;
|
||||||
new_rect.expand_to(pos + x);
|
new_rect.expand_to(position + x);
|
||||||
new_rect.expand_to(pos + y);
|
new_rect.expand_to(position + y);
|
||||||
new_rect.expand_to(pos + x + y);
|
new_rect.expand_to(position + x + y);
|
||||||
return new_rect;
|
return new_rect;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,14 +71,14 @@ void Transform2D::set_rotation_and_scale(real_t p_rot, const Size2 &p_scale) {
|
||||||
Rect2 Transform2D::xform_inv(const Rect2 &p_rect) const {
|
Rect2 Transform2D::xform_inv(const Rect2 &p_rect) const {
|
||||||
|
|
||||||
Vector2 ends[4] = {
|
Vector2 ends[4] = {
|
||||||
xform_inv(p_rect.pos),
|
xform_inv(p_rect.position),
|
||||||
xform_inv(Vector2(p_rect.pos.x, p_rect.pos.y + p_rect.size.y)),
|
xform_inv(Vector2(p_rect.position.x, p_rect.position.y + p_rect.size.y)),
|
||||||
xform_inv(Vector2(p_rect.pos.x + p_rect.size.x, p_rect.pos.y + p_rect.size.y)),
|
xform_inv(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y + p_rect.size.y)),
|
||||||
xform_inv(Vector2(p_rect.pos.x + p_rect.size.x, p_rect.pos.y))
|
xform_inv(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y))
|
||||||
};
|
};
|
||||||
|
|
||||||
Rect2 new_rect;
|
Rect2 new_rect;
|
||||||
new_rect.pos = ends[0];
|
new_rect.position = ends[0];
|
||||||
new_rect.expand_to(ends[1]);
|
new_rect.expand_to(ends[1]);
|
||||||
new_rect.expand_to(ends[2]);
|
new_rect.expand_to(ends[2]);
|
||||||
new_rect.expand_to(ends[3]);
|
new_rect.expand_to(ends[3]);
|
||||||
|
@ -143,7 +143,7 @@ void Transform2D::set_rotation(real_t p_rot) {
|
||||||
elements[1][1] = cr;
|
elements[1][1] = cr;
|
||||||
}
|
}
|
||||||
|
|
||||||
Transform2D::Transform2D(real_t p_rot, const Vector2 &p_pos) {
|
Transform2D::Transform2D(real_t p_rot, const Vector2 &p_position) {
|
||||||
|
|
||||||
real_t cr = ::cos(p_rot);
|
real_t cr = ::cos(p_rot);
|
||||||
real_t sr = ::sin(p_rot);
|
real_t sr = ::sin(p_rot);
|
||||||
|
@ -151,7 +151,7 @@ Transform2D::Transform2D(real_t p_rot, const Vector2 &p_pos) {
|
||||||
elements[0][1] = sr;
|
elements[0][1] = sr;
|
||||||
elements[1][0] = -sr;
|
elements[1][0] = -sr;
|
||||||
elements[1][1] = cr;
|
elements[1][1] = cr;
|
||||||
elements[2] = p_pos;
|
elements[2] = p_position;
|
||||||
}
|
}
|
||||||
|
|
||||||
Size2 Transform2D::get_scale() const {
|
Size2 Transform2D::get_scale() const {
|
||||||
|
|
Loading…
Reference in New Issue