| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | #include<iostream> |
| | #include "lc_rect.h" |
| |
|
| | #include <QDebug> |
| | #include <qlogging.h> |
| |
|
| | #define INTERT_TEST(s) qDebug()<<"\ntesting " #s; \ |
| | assert(s); \ |
| | qDebug()<<"Passed"; |
| |
|
| | using namespace lc::geo; |
| |
|
| | Coordinate LC_Rect::Vector(Coordinate const& p, Coordinate const& q) { |
| | return {q.x - p.x, q.y - p.y}; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | LC_Rect::Area(const Coordinate& coordA, const Coordinate& coordB) : |
| | _minP{std::min(coordA.x, coordB.x), std::min(coordA.y, coordB.y)}, |
| | _maxP{std::max(coordA.x, coordB.x), std::max(coordA.y, coordB.y)} |
| | { |
| | } |
| |
|
| | LC_Rect::Area() : |
| | _minP{std::numeric_limits<double>::max(), std::numeric_limits<double>::max()}, |
| | _maxP{std::numeric_limits<double>::min(), std::numeric_limits<double>::min()} |
| | {} |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | LC_Rect::Area(const Coordinate& coord, double width, double height): |
| | Area(coord, {coord.x + width, coord.y + height}) |
| | {} |
| |
|
| | |
| | |
| | |
| | const Coordinate& LC_Rect::minP() const { |
| | return _minP; |
| | } |
| |
|
| | |
| | |
| | |
| | const Coordinate& LC_Rect::maxP() const { |
| | return _maxP; |
| | } |
| | |
| | |
| | |
| | |
| | |
| | Coordinate LC_Rect::upperLeftCorner() const { |
| | return {_minP.x, _maxP.y}; |
| | } |
| | Coordinate LC_Rect::upperRightCorner() const { |
| | return _maxP; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | Coordinate LC_Rect::lowerRightCorner() const { |
| | return {_maxP.x, _minP.y}; |
| | } |
| |
|
| | Coordinate LC_Rect::lowerLeftCorner() const { |
| | return _minP; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | double LC_Rect::width() const { |
| | return _maxP.x - _minP.x; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | double LC_Rect::height() const { |
| | return _maxP.y - _minP.y; |
| | } |
| |
|
| | bool LC_Rect::isEmpty(double tolerance) const { |
| | return std::min(width(), height()) < tolerance; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | bool LC_Rect::inArea(const Coordinate& point, double tolerance) const { |
| | return (point.x >= _minP.x - tolerance && point.x <= _maxP.x + tolerance && point.y >= _minP.y - tolerance && point.y <= _maxP.y + tolerance); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | bool LC_Rect::inArea(const Area& area) const { |
| | return _minP.x >= area._minP.x && _minP.y >= area._minP.y && _maxP.x <= area._maxP.x && _maxP.y <= area._maxP.y; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | bool LC_Rect::overlaps(const Area& otherArea) const { |
| | return intersects(otherArea); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | short LC_Rect::numCornersInside(const Area& otherArea) const { |
| | short pointsInside = 0; |
| |
|
| | if (otherArea.inArea(_minP)) { |
| | pointsInside++; |
| | } |
| |
|
| | if (otherArea.inArea(_maxP)) { |
| | pointsInside++; |
| | } |
| |
|
| | if (otherArea.inArea(upperLeftCorner())) { |
| | pointsInside++; |
| | } |
| |
|
| | if (otherArea.inArea(lowerRightCorner())) { |
| | pointsInside++; |
| | } |
| |
|
| | return pointsInside; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | Area LC_Rect::merge(const Area& other) const { |
| | return { |
| | {std::min(other.minP().x, this->minP().x), std::min(other.minP().y, this->minP().y)}, |
| | {std::max(other.maxP().x, this->maxP().x), std::max(other.maxP().y, this->maxP().y)} |
| | }; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | Area LC_Rect::merge(const Coordinate& other) const { |
| | return { |
| | {std::min(other.x, this->minP().x), std::min(other.y, this->minP().y)}, |
| | {std::max(other.x, this->maxP().x), std::max(other.y, this->maxP().y)} |
| | }; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | Area LC_Rect::intersection(const Area& other, double tolerance) const { |
| |
|
| | Area const ret{ |
| | {std::max(other.minP().x, this->minP().x), std::max(other.minP().y, this->minP().y)}, |
| | {std::min(other.maxP().x, this->maxP().x), std::min(other.maxP().y, this->maxP().y)} |
| | }; |
| | if (ret.width() < tolerance || ret.height() < tolerance) { |
| | return {}; |
| | } |
| | return ret; |
| | } |
| |
|
| | bool LC_Rect::intersects(Area const& rhs, double tolerance) const { |
| | return maxP().x + tolerance >= rhs.minP().x && |
| | maxP().y + tolerance >= rhs.minP().y && |
| | rhs.maxP().x + tolerance >= minP().x && |
| | rhs.maxP().y + tolerance >= minP().y; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | Coordinate LC_Rect::top() const { |
| | return Vector(upperLeftCorner(), _maxP); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | Coordinate LC_Rect::bottom() const { |
| | return Vector(_minP, lowerRightCorner()); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | Coordinate LC_Rect::left() const { |
| | return Vector(_minP, upperLeftCorner()); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | Coordinate LC_Rect::right() const { |
| | return Vector(lowerRightCorner(), _maxP); |
| | } |
| |
|
| | |
| | |
| | |
| | Area LC_Rect::increaseBy(double increaseBy) const { |
| | return {_minP - increaseBy, _maxP + increaseBy}; |
| | } |
| |
|
| | std::array<Coordinate, 4> LC_Rect::vertices() const |
| | { |
| | return {{lowerLeftCorner(), lowerRightCorner(), |
| | upperRightCorner(), upperLeftCorner()}}; |
| | } |
| |
|
| | std::ostream& operator<<(std::ostream& os, const Area& area) { |
| | os << "Area(" << area.minP() << " " << area.maxP() << ")"; |
| | return os; |
| | } |
| |
|
| | void LC_Rect::unitTest() { |
| | LC_Rect const rect0{{0., 0.}, {1., 1.}}; |
| | LC_Rect const rect1{{0.5, 0.5}, {1.5, 1.5}}; |
| | LC_Rect const rect2{{1.5, 1.5}, {2.5, 2.5}}; |
| | LC_Rect const rect3{{0.0, 1.}, {1.5, 1.5}}; |
| |
|
| | |
| | INTERT_TEST(rect0.intersects(rect1)) |
| | INTERT_TEST(rect1.intersects(rect0)) |
| | INTERT_TEST(rect1.intersects(rect2)) |
| | INTERT_TEST(rect2.intersects(rect1)) |
| |
|
| | INTERT_TEST(!rect0.intersects(rect2)) |
| | INTERT_TEST(!rect2.intersects(rect0)) |
| |
|
| | INTERT_TEST(rect2.intersects(rect3)) |
| | INTERT_TEST(rect3.intersects(rect2)) |
| |
|
| | |
| | INTERT_TEST(rect0.inArea({0.1, 0.1})) |
| | INTERT_TEST(rect0.inArea({0.5, 0.5})) |
| |
|
| | INTERT_TEST(!rect0.inArea({1.1, 1.1})) |
| | INTERT_TEST(!rect0.inArea({-1.1, -1.1})) |
| |
|
| | } |
| |
|