/
ArtemNech
/
simpleOpenGL
Обзор
Документация
Войти
/
ArtemNech
/
simpleOpenGL
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
develop
backend/graphics-units/NRect.cpp
233 строки
6 KB
Artem Nechitaylenko
initial
16 май 2026, 12:48
16 май 2026, 12:48
0d3ac6c
Код
Авторство
О чём код?
// // Created by artem on 2/17/25. // #include "NRect.h" #include "NPointF.h" #include <cmath> namespace ngraph { NRect::NRect() = default; NRect::NRect(const NPoint & topLeft, const NPoint & bottom_right) { get_real(topLeft.x(), topLeft.y(), bottom_right.x(), bottom_right.y()); } NRect::NRect(const int &x0, const int &y0, const int &x1, const int &y1) { get_real(x0, y0, x1, y1); } NRect::NRect(const NPoint &topLeft, const int &width, const int &height) { get_real(topLeft.x(), topLeft.y(), topLeft.x() + width, topLeft.y() + height); } NRect::NRect(const NRect &src) { *this = src; } NRect::NRect(const NPoint & tl, const NSize & size) { get_real(tl.x(), tl.y(), tl.x() + size.width(), tl.y() + size.height()); } NRect &NRect::operator=(const NRect &rhs) { if (this == &rhs) return *this; this->top_left_ = rhs.top_left_; this->bottom_right_ = rhs.bottom_right_; return *this; } void NRect::get_real(const NPoint &tl, const NPoint &br) { int left = tl.x() < br.x() ? tl.x() : br.x(); int top = tl.y() < br.y() ? tl.y() : br.y(); int right= br.x() > tl.x() ? br.x() : tl.x(); int bott = br.y() > tl.y() ? br.y() : tl.y(); this->top_left_ = {left, top}; this->bottom_right_ = {right, bott}; } void NRect::get_real(const int &x0, const int &y0, const int &x1, const int &y1) { int left = x0 < x1 ? x0 : x1; int top = y0 < y1 ? y0 : y1; int right= x1 > x0 ? x1 : x0; int bott = y1 > y0 ? y1 : y0; this->top_left_ = {left, top}; this->bottom_right_ = {right, bott}; } NPoint NRect::mid_point(const NRect &rect) const { int midX = (rect.top_left_.x() + rect.bottom_right_.x()) / 2; int midY = (rect.top_left_.y() + rect.bottom_right_.y()) / 2; return {midX, midY}; } int NRect::deltaX(const NRect &other) const { return std::abs(mid_point(*this).x() - mid_point(other).x()); } int NRect::deltaY(const NRect &other) const { return std::abs(mid_point(*this).y() - mid_point(other).y()); } bool NRect::intersects(const NRect &other) const { return deltaX(other) <= (other.width()/2 + this->width()/2) && deltaY(other) <= (other.height()/2+this->height()/2); } bool NRect::intersects(const NRect *other) const { if (!other) return false; return intersects(*other); } bool NRect::contains(const NRect &other) const { return (other.top_left_.x() >= this->top_left_.x()) && (other.top_left_.y() >= this->top_left_.y()) && (other.bottom_right_.x() <= this->bottom_right_.x()) && (other.bottom_right_.y() <= this->bottom_right_.y()); } bool NRect::contains(const NRect *other) const { if (!other) return false; return contains(*other); } bool operator==(NRect &lhs, NRect &rhs) { return (lhs.top_left_ == rhs.top_left_) && (lhs.bottom_right_ == rhs.bottom_right_); } int NRect::width() const { return std::abs(this->left() - this->right()) + 1; } int NRect::height() const { return std::abs(this->top_left_.y() - this->bottom_right_.y()) + 1; } NPoint NRect::left_top() const { return this->top_left_; } NPoint NRect::bottom_right() const { return this->bottom_right_; } void NRect::set_equal_me(const NRect &rect) { NPoint tl = NPoint(rect.top_left_.x(), rect.top_left_.y()); NPoint br = NPoint(rect.bottom_right().x(), rect.bottom_right().y()); get_real(tl.x(), tl.y(), br.x(), br.y()); } void NRect::set_equal(NRect *rect) { *rect = NRect(top_left_.x(), top_left_.y(), this->width(), this->height()); } int NRect::left() const { return this->top_left_.x(); } int NRect::right() const { return this->bottom_right_.x(); } int NRect::top() const { return this->top_left_.y(); } int NRect::bottom() const { return this->bottom_right_.y(); } bool NRect::contains(const NPoint &point) const { return point.x() >= this->top_left_.x() && point.x() <= this->bottom_right_.x() && point.y() >= this->top_left_.y() && point.y() <= this->bottom_right_.y(); } RECT_DIR NRect::direction() const { if (this->width() > this->height()) return RD_HOR; if (this->width() < this->height()) return RD_VERT; return RD_NONE; } void NRect::set_points(const NPoint &p0, const NPoint &p1) { get_real(p0.x(), p0.y(), p1.x(), p1.y()); } NPoint NRect::get_mid_point() const { return {this->top_left_.x() + this->width()/2, this->top_left_.y() + this->height()/2}; } bool NRect::common_X_axis(const NRect &other, bool inside) const { if (inside) { if ((other.left() > this->left() && other.left() < this->right()) || (other.right() > this->left() && other.right() < this->right())) return true; return false; } if ((other.left() >= this->left() && other.left() <= this->right()) || (other.right() >= this->left() && other.right() <= this->right())) return true; return false; } /*bool NRect::common_X_axis_offset(const NRect &other) const { if ( (other.left() + BETWEEN_COMPS/2 >= this->left() + BETWEEN_COMPS/2 && other.left() + BETWEEN_COMPS/2 <= this->right() - BETWEEN_COMPS/2) || (other.right() - BETWEEN_COMPS/2 >= this->left() + BETWEEN_COMPS/2 && other.right() - BETWEEN_COMPS/2 <= this->right() - BETWEEN_COMPS/2) ) return true; return false; }*/ void NRect::set_top_left(const NPoint &top_left) { int w = this->width(); int h = this->height(); this->top_left_ = top_left; this->bottom_right_.set_x(top_left.x() + w); this->bottom_right_.set_y(top_left.y() + h); } }