/
d.vision
/
division_engine_cpp
Обзор
Документация
Войти
/
d.vision
/
division_engine_cpp
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
include/division_engine/rect.hpp
98 строк
3 KB
den163
Added SDF font support
05 дек 2024, 01:01
05 дек 2024, 01:01
a1df803
Код
Авторство
О чём код?
#pragma once #include "glm/common.hpp" #include <glm/vec2.hpp> namespace division_engine { struct Rect { glm::vec2 center; // Half size glm::vec2 extents; static Rect from_center(glm::vec2 center, glm::vec2 size) { return Rect { center, size * 0.5f }; // NOLINT } static Rect from_top_right(glm::vec2 top_right, glm::vec2 size) { auto extents = size * 0.5f; // NOLINT return Rect { top_right - extents, extents }; } static Rect from_top_left(glm::vec2 top_left, glm::vec2 size) { auto extents = size * 0.5f; // NOLINT return Rect { { top_left.x + extents.x, top_left.y - extents.y }, extents }; } static Rect from_bottom_right(glm::vec2 bottom_right, glm::vec2 size) { auto extents = size * 0.5f; // NOLINT return Rect { { bottom_right.x - extents.x, bottom_right.y + extents.y } }; } static Rect from_bottom_left(glm::vec2 bottom_left, glm::vec2 size) { auto extents = size * 0.5f; // NOLINT return Rect { bottom_left + extents, extents }; } static Rect from_bottom_left_top_right(const glm::vec2& bottom_left, const glm::vec2& top_right) { const auto& extents = (top_right - bottom_left) * 0.5f; // NOLINT const auto& center = (bottom_left + top_right) * 0.5f; // NOLINT return Rect { center, extents }; } glm::vec2 size() const { return extents * 2.f; /* NOLINT */ } float area() const { return extents.x * 2.f * extents.y * 2.f; /* NOLINT */ } float left() const { return center.x - extents.x; } float right() const { return center.x + extents.x; } float top() const { return center.y + extents.y; } float bottom() const { return center.y - extents.y; } glm::vec2 bottom_left() const { return { left(), bottom() }; } glm::vec2 top_left() const { return { left(), top() }; } glm::vec2 top_right() const { return { right(), top() }; } glm::vec2 bottom_right() const { return { right(), bottom() }; } void set_left(float value) { center.x = value + extents.x; } void set_right(float value) { center.x = value - extents.x; } void set_top(float value) { center.y = value - extents.y; } void set_bottom(float value) { center.y = value + extents.y; } bool contains(Rect rect) const { return (left() <= rect.left()) & (rect.right() <= right()) & (bottom() <= rect.bottom()) & (rect.top() <= top()); } bool contains(glm::vec2 point) const { return (left() <= point.x) & (point.x <= right()) & (bottom() <= point.y) & (point.y <= top()); } Rect clamp(const Rect& other) const { return Rect::from_bottom_left_top_right( glm::max(bottom_left(), other.bottom_left()), glm::min(top_right(), other.top_right()) ); } Rect include(const Rect& other) const { return Rect::from_bottom_left_top_right( glm::min(bottom_left(), other.bottom_left()), glm::max(top_right(), other.top_right()) ); } }; }