2017-03-03 20:04:23 +00:00
|
|
|
#ifndef RECT2_H
|
|
|
|
#define RECT2_H
|
|
|
|
|
2017-03-06 07:49:24 +00:00
|
|
|
#include "Vector2.hpp"
|
2017-03-03 20:04:23 +00:00
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
|
2017-03-06 02:30:46 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
|
2017-03-03 20:04:23 +00:00
|
|
|
namespace godot {
|
|
|
|
|
2017-03-06 02:30:46 +00:00
|
|
|
class String;
|
|
|
|
|
2017-03-03 20:04:23 +00:00
|
|
|
typedef Vector2 Size2;
|
|
|
|
typedef Vector2 Point2;
|
|
|
|
|
2018-02-23 11:39:35 +00:00
|
|
|
struct Transform2D;
|
2017-03-03 20:04:23 +00:00
|
|
|
|
2017-07-23 15:53:50 +00:00
|
|
|
struct Rect2 {
|
2017-03-03 20:04:23 +00:00
|
|
|
|
2018-12-13 09:10:25 +00:00
|
|
|
Point2 position;
|
2017-03-03 20:04:23 +00:00
|
|
|
Size2 size;
|
|
|
|
|
2018-12-13 09:10:25 +00:00
|
|
|
inline const Vector2 &get_position() const { return position; }
|
|
|
|
inline void set_position(const Vector2 &p_position) { position = p_position; }
|
2018-11-23 22:09:41 +00:00
|
|
|
inline const Vector2 &get_size() const { return size; }
|
|
|
|
inline void set_size(const Vector2 &p_size) { size = p_size; }
|
2017-03-03 20:04:23 +00:00
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
inline real_t get_area() const { return size.width * size.height; }
|
2017-03-03 20:04:23 +00:00
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
inline bool intersects(const Rect2 &p_rect) const {
|
2018-12-13 09:10:25 +00:00
|
|
|
if (position.x >= (p_rect.position.x + p_rect.size.width))
|
2017-03-03 20:04:23 +00:00
|
|
|
return false;
|
2018-12-13 09:10:25 +00:00
|
|
|
if ((position.x + size.width) <= p_rect.position.x)
|
2017-03-03 20:04:23 +00:00
|
|
|
return false;
|
2018-12-13 09:10:25 +00:00
|
|
|
if (position.y >= (p_rect.position.y + p_rect.size.height))
|
2017-03-03 20:04:23 +00:00
|
|
|
return false;
|
2018-12-13 09:10:25 +00:00
|
|
|
if ((position.y + size.height) <= p_rect.position.y)
|
2017-03-03 20:04:23 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
real_t distance_to(const Vector2 &p_point) const;
|
2017-03-03 20:04:23 +00:00
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
bool intersects_transformed(const Transform2D &p_xform, const Rect2 &p_rect) const;
|
2017-03-03 20:04:23 +00:00
|
|
|
|
2018-12-13 09:10:25 +00:00
|
|
|
bool intersects_segment(const Point2 &p_from, const Point2 &p_to, Point2 *r_position = nullptr, Point2 *r_normal = nullptr) const;
|
2017-03-03 20:04:23 +00:00
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
inline bool encloses(const Rect2 &p_rect) const {
|
2017-03-03 20:04:23 +00:00
|
|
|
|
2018-12-13 09:10:25 +00:00
|
|
|
return (p_rect.position.x >= position.x) && (p_rect.position.y >= position.y) &&
|
|
|
|
((p_rect.position.x + p_rect.size.x) < (position.x + size.x)) &&
|
|
|
|
((p_rect.position.y + p_rect.size.y) < (position.y + size.y));
|
2017-03-03 20:04:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool has_no_area() const {
|
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
return (size.x <= 0 || size.y <= 0);
|
2017-03-03 20:04:23 +00:00
|
|
|
}
|
2018-11-23 22:09:41 +00:00
|
|
|
Rect2 clip(const Rect2 &p_rect) const;
|
2017-03-03 20:04:23 +00:00
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
Rect2 merge(const Rect2 &p_rect) const;
|
2017-03-03 20:04:23 +00:00
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
inline bool has_point(const Point2 &p_point) const {
|
2018-12-13 09:10:25 +00:00
|
|
|
if (p_point.x < position.x)
|
2017-03-03 20:04:23 +00:00
|
|
|
return false;
|
2018-12-13 09:10:25 +00:00
|
|
|
if (p_point.y < position.y)
|
2017-03-03 20:04:23 +00:00
|
|
|
return false;
|
|
|
|
|
2018-12-13 09:10:25 +00:00
|
|
|
if (p_point.x >= (position.x + size.x))
|
2017-03-03 20:04:23 +00:00
|
|
|
return false;
|
2018-12-13 09:10:25 +00:00
|
|
|
if (p_point.y >= (position.y + size.y))
|
2017-03-03 20:04:23 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
inline bool no_area() const { return (size.width <= 0 || size.height <= 0); }
|
2017-03-03 20:04:23 +00:00
|
|
|
|
2018-12-13 09:10:25 +00:00
|
|
|
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 position != p_rect.position || size != p_rect.size; }
|
2017-03-03 20:04:23 +00:00
|
|
|
|
|
|
|
inline Rect2 grow(real_t p_by) const {
|
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
Rect2 g = *this;
|
2018-12-13 09:10:25 +00:00
|
|
|
g.position.x -= p_by;
|
|
|
|
g.position.y -= p_by;
|
2018-11-23 22:09:41 +00:00
|
|
|
g.size.width += p_by * 2;
|
|
|
|
g.size.height += p_by * 2;
|
2017-03-03 20:04:23 +00:00
|
|
|
return g;
|
|
|
|
}
|
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
inline Rect2 expand(const Vector2 &p_vector) const {
|
2017-03-03 20:04:23 +00:00
|
|
|
|
|
|
|
Rect2 r = *this;
|
|
|
|
r.expand_to(p_vector);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
inline void expand_to(const Vector2 &p_vector) { //in place function for speed
|
2017-03-03 20:04:23 +00:00
|
|
|
|
2018-12-13 09:10:25 +00:00
|
|
|
Vector2 begin = position;
|
|
|
|
Vector2 end = position + size;
|
2017-03-03 20:04:23 +00:00
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
if (p_vector.x < begin.x)
|
|
|
|
begin.x = p_vector.x;
|
|
|
|
if (p_vector.y < begin.y)
|
|
|
|
begin.y = p_vector.y;
|
2017-03-03 20:04:23 +00:00
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
if (p_vector.x > end.x)
|
|
|
|
end.x = p_vector.x;
|
|
|
|
if (p_vector.y > end.y)
|
|
|
|
end.y = p_vector.y;
|
2017-03-03 20:04:23 +00:00
|
|
|
|
2018-12-13 09:10:25 +00:00
|
|
|
position = begin;
|
2018-11-23 22:09:41 +00:00
|
|
|
size = end - begin;
|
2017-03-03 20:04:23 +00:00
|
|
|
}
|
|
|
|
|
2017-03-06 02:30:46 +00:00
|
|
|
operator String() const;
|
2017-03-03 20:04:23 +00:00
|
|
|
|
2017-03-06 02:30:46 +00:00
|
|
|
inline Rect2() {}
|
2018-11-23 22:09:41 +00:00
|
|
|
inline Rect2(real_t p_x, real_t p_y, real_t p_width, real_t p_height) {
|
2018-12-13 09:10:25 +00:00
|
|
|
position = Point2(p_x, p_y);
|
2018-11-23 22:09:41 +00:00
|
|
|
size = Size2(p_width, p_height);
|
|
|
|
}
|
2018-12-13 09:10:25 +00:00
|
|
|
inline Rect2(const Point2 &p_position, const Size2 &p_size) {
|
|
|
|
position = p_position;
|
2018-11-23 22:09:41 +00:00
|
|
|
size = p_size;
|
|
|
|
}
|
2017-03-03 20:04:23 +00:00
|
|
|
};
|
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
} // namespace godot
|
2017-03-03 20:04:23 +00:00
|
|
|
|
|
|
|
#endif // RECT2_H
|