Added expand_to to Rect2.

This commit is contained in:
Relintai 2021-11-16 03:06:58 +01:00
parent f50f52e593
commit e74549776f
2 changed files with 41 additions and 10 deletions

View File

@ -83,6 +83,33 @@ void Rect2::shrink(const float by) {
w -= by;
}
//Taken from the Godot Engine (MIT License)
//Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur.
//Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md).
void Rect2::expand_to(const Vector2 &p_vector) {
Vector2 begin = Vector2(x, y);
Vector2 end = Vector2(x, y) + Vector2(w, h);
if (p_vector.x < begin.x) {
begin.x = p_vector.x;
}
if (p_vector.y < begin.y) {
begin.y = p_vector.y;
}
if (p_vector.x > end.x) {
end.x = p_vector.x;
}
if (p_vector.y > end.y) {
end.y = p_vector.y;
}
x = begin.x;
y = begin.y;
w = end.x - begin.x;
h = end.y - begin.y;
}
#ifdef SDL_AVAILABLE
SDL_Rect Rect2::as_rect() const {
SDL_Rect r;

View File

@ -5,6 +5,8 @@
#include <SDL.h>
#endif
#include "vector2.h"
class Rect2 {
public:
float get_area() const;
@ -18,6 +20,8 @@ public:
void grow(const float by);
void shrink(const float by);
void expand_to(const Vector2 &p_vector);
#ifdef SDL_AVAILABLE
SDL_Rect as_rect() const;
SDL_FRect as_frect() const;