/
ArtemNech
/
simpleOpenGL
Обзор
Документация
Войти
/
ArtemNech
/
simpleOpenGL
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
develop
backend/graphics-units/NRectF.cpp
239 строк
6 KB
Artem Nechitaylenko
initial
16 май 2026, 12:48
16 май 2026, 12:48
0d3ac6c
Код
Авторство
О чём код?
// // Created by artem on 5/18/25. // #include <cmath> #include "NRectF.h" #include "floats.h" namespace ngraph { NRectF::NRectF() = default; NRectF::NRectF(const NRectF &other) = default; NRectF::NRectF(const NPointF &topLeft, const NPointF &bottom_right) { double x0 = topLeft.x(), y0 = topLeft.y(); double x1 = bottom_right.x(), y1 = bottom_right.y(); set_real_tl(x0, y0, x1, y1); m_top_left = NPointF(x0, y0); m_bottom_right = NPointF(x1, y1); } NRectF::NRectF(const NPointF &topLeft, const double &width, const double &height) { m_top_left = topLeft; double x_right = m_top_left.x() + width; double y_bottom = m_top_left.y() + height; m_bottom_right = NPointF(x_right, y_bottom); } NRectF::NRectF(const double &x0, const double &y0, const double &x1, const double &y1) { double X0 = x0, Y0 = y0, X1 = x1, Y1 = y1; set_real_tl(X0, Y0, X1, Y1); m_top_left = NPointF(X0, Y0); m_bottom_right = NPointF(X1, Y1); } NRectF &NRectF::operator=(const NRectF &rhs) { if (this == &rhs) return *this; m_top_left = rhs.m_top_left; m_bottom_right = rhs.m_bottom_right; return *this; } bool NRectF::intersects(const NRectF &other) const { auto mp_this = get_mid_point(); auto mp_alien = other.get_mid_point(); bool d_x = std::fabs(mp_alien.x() - mp_this.x()) <= (width() / 2.0 + other.width() / 2.0); bool d_y = std::fabs(mp_this.y() - mp_alien.y()) <= (height() / 2.0 + other.height() / 2.0); return d_x && d_y; } bool NRectF::intersects(const NRectF *other) const { if (!other) return false; return intersects(*other); } bool NRectF::contains(const NRectF &other) const { return m_top_left.x() <= other.m_top_left.x() && m_top_left.y() <= other.m_top_left.y() && m_bottom_right.x() >= other.m_bottom_right.x() && m_bottom_right.y() >= other.m_bottom_right.y(); } bool NRectF::contains(const NRectF *other) const { if (!other) return false; return contains(*other); } bool NRectF::contains(const NPointF &point) const { return m_top_left.x() <= point.x() && m_bottom_right.x() >= point.x() && m_top_left.y() <= point.y() && m_bottom_right.y() >= point.y(); } void NRectF::set_points(const NPointF &p0, const NPointF &p1) { double x0 = p0.x(), y0 = p0.y(); double x1 = p1.x(), y1 = p1.y(); set_real_tl(x0, y0, x1, y1); m_top_left = NPointF(x0, y0); m_bottom_right = NPointF(x1, y1); } NPointF NRectF::get_mid_point() const { return {m_top_left.x() + width() / 2.0, m_top_left.y() + height() / 2.0}; } bool NRectF::common_X_axis(const NRectF &other) const { return get_mid_point().x() == other.get_mid_point().x(); } void NRectF::set_top_left(const NPointF &top_left) { double w = width(), h = height(); m_top_left = top_left; auto x = m_top_left.x() + w - 1; auto y = m_top_left.y() + h - 1; m_bottom_right = NPointF(x, y); } double NRectF::width() const { return m_bottom_right.x() - m_top_left.x(); } double NRectF::height() const { return m_bottom_right.y() - m_top_left.y(); } NPointF NRectF::left_top() const { return m_top_left; } NPointF NRectF::bottom_right() const { return m_bottom_right; } double NRectF::left() const { return m_top_left.x(); } double NRectF::right() const { return m_bottom_right.x(); } double NRectF::top() const { return m_top_left.y(); } double NRectF::bottom() const { return m_bottom_right.y(); } void NRectF::set_real_tl(double &x0, double &y0, double &x1, double &y1) { double x_left = x0 < x1 ? x0 : x1; double y_top = y0 < y1 ? y0 : y1; double x_right = x1 > x0 ? x1 : x0; double y_bottom = y1 > y0 ? y1 : y0; x0 = x_left; y0 = y_top; x1 = x_right; y1 = y_bottom; } bool NRectF::operator==(NRectF &rhs) const { bool eq_tl = floats::is_floats_equal(m_top_left.x(), rhs.m_top_left.x()) && floats::is_floats_equal(m_top_left.y(), rhs.m_top_left.y()); bool eq_size = floats::is_floats_equal(width(), rhs.width()) && floats::is_floats_equal(height(), rhs.height()); return eq_tl && eq_size; } void NRectF::set_width(const double &width) { double width_ = std::fabs(width); m_bottom_right.set_x(m_top_left.x() + width_); } void NRectF::set_height(const double &height) { double Height = std::fabs(height); m_bottom_right.set_y(m_top_left.y() + Height); } NSizeF NRectF::size() const { return {m_bottom_right.x() - m_top_left.x(), m_bottom_right.y() - m_top_left.y()}; } void NRectF::moveToPoint(const NPointF &pos) { NSizeF size = this->size(); m_top_left = pos; m_bottom_right = NPointF(pos.x() + size.width(), pos.y() + size.height()); } NPointF NRectF::center() const { double x = m_top_left.x() + width()/2.0; double y = m_top_left.y() + height() / 2.0; return {x, y}; } NRectF NRectF::united(const NRectF &other) const { if (this->isEmpty()) return other; if (other.isEmpty()) return *this; double left = std::min(this->left(), other.left()); double top = std::min(this->top(), other.top()); double right = std::max(this->right(), other.right()); double bottom = std::max(this->bottom(), other.bottom()); return {left, top, right - left, bottom - top}; } void NRectF::set_size(const NSizeF &size) { m_bottom_right = NPointF(m_top_left.x() + size.width(), m_top_left.y() + size.height()); } double NRectF::centerX() const { return m_top_left.x() + (m_bottom_right.x() - m_top_left.x())/2.0; } double NRectF::centerY() const { return m_top_left.y() + (m_bottom_right.y() - m_top_left.y())/2.0; } } // namespace ngraph