From ca69882d21891be4638837a5f64887a1bcbeea88 Mon Sep 17 00:00:00 2001 From: shudgit Date: Mon, 3 Apr 2023 18:19:51 -0400 Subject: [PATCH 1/3] Pulled Iterators and Added Line2D Iterator --- .gitignore | 1 + Line2D.cpp | 11 +++++++- Line2D.h | 4 +++ Point2D.cpp | 41 +++++++++++++++++++++++---- Point2D.h | 73 +++++++++++++++++++++++++++++++++++++----------- Point2DImplr.cpp | 5 ++++ Point2DImplr.h | 11 ++++++++ Region2D.cpp | 71 +++++++++++++++++++++++++++++++++++----------- Region2D.h | 10 ++++++- 9 files changed, 188 insertions(+), 39 deletions(-) create mode 100644 Point2DImplr.cpp create mode 100644 Point2DImplr.h diff --git a/.gitignore b/.gitignore index 4a3fcd4..20773b2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ test *.swp +.vscode/settings.json diff --git a/Line2D.cpp b/Line2D.cpp index 73dbe52..b6c20eb 100644 --- a/Line2D.cpp +++ b/Line2D.cpp @@ -49,4 +49,13 @@ Line2D::Line2D(Line2D &&sourceLine2D) } //destructor -Line2D::~Line2D(){} \ No newline at end of file +Line2D::~Line2D(){} + +Line2D::iterator Line2D::begin() +{ + return this->pimpl->lineSegments.begin(); +} +Line2D::iterator Line2D::end() +{ + return this->pimpl->lineSegments.end(); +} \ No newline at end of file diff --git a/Line2D.h b/Line2D.h index 8384eb4..467af39 100644 --- a/Line2D.h +++ b/Line2D.h @@ -17,6 +17,10 @@ class Line2D { Line2D(const Line2D &sourceLine2D); //copy constructor Line2D(Line2D &&sourceLine2D); //move constructor ~Line2D(); //Destructor + + typedef std::vector::iterator iterator; + iterator begin(); + iterator end(); }; diff --git a/Point2D.cpp b/Point2D.cpp index 77c79f6..f4f284f 100644 --- a/Point2D.cpp +++ b/Point2D.cpp @@ -1,5 +1,6 @@ #include "Point2D.h" #include +#include using namespace std; @@ -10,18 +11,34 @@ struct Point2D::Impl Impl(vector _pointCollection): pointCollection(move(_pointCollection)){}; vector pointCollection; + + bool ordered; }; Point2D::Point2D() {} + Point2D::Point2D(vector _pointCollection):pimpl(new Impl()) { _pointCollection.erase(unique(_pointCollection.begin(), _pointCollection.end()), _pointCollection.end()); sort(_pointCollection.begin(), _pointCollection.end()); this->pimpl->pointCollection = _pointCollection; + this->pimpl->ordered = true; +} + +Point2D::Point2D(vector _pointCollection, bool _ordered):pimpl(new Impl()) +{ + this->pimpl->ordered = _ordered; + if(_ordered){ + _pointCollection.erase(unique(_pointCollection.begin(), _pointCollection.end()), _pointCollection.end()); + sort(_pointCollection.begin(), _pointCollection.end()); + } + this->pimpl->pointCollection = _pointCollection; } + Point2D::Point2D(Point2D const &sourcePoint2D): pimpl(new Impl(*sourcePoint2D.pimpl)) { } + Point2D::Point2D(Point2D &&sourcePoint2D) { this->pimpl = move(sourcePoint2D.pimpl); @@ -30,12 +47,26 @@ Point2D::Point2D(Point2D &&sourcePoint2D) Point2D::~Point2D(){} -Point2D::iterator Point2D::begin() +int Point2D::count() +{ + return this->pimpl->pointCollection.size(); +} + +Point2D::Iterator Point2D::begin() +{ + return Iterator(&(this->pimpl->pointCollection)[0]); +} + +Point2D::Iterator Point2D::end() { - return this->pimpl->pointCollection.begin(); + return Iterator(&(this->pimpl->pointCollection)[this->pimpl->pointCollection.size()]); } -Point2D::iterator Point2D::end() +/*Point2D randomPoint2D(long count, int minX, int maxX, int minY, int maxY) { - return this->pimpl->pointCollection.end(); -} \ No newline at end of file + generateSeed(); + std::vector points; + for (long i = 0; i < count; i++) + points.push_back(randomSimplePoint2D(minX, maxX, minY, maxY)); + return Point2D(points); +}*/ \ No newline at end of file diff --git a/Point2D.h b/Point2D.h index 23f1c79..54ba1cc 100644 --- a/Point2D.h +++ b/Point2D.h @@ -7,22 +7,63 @@ using namespace std; class Point2D { -private: - class Impl; - unique_ptr pimpl; - -public: - Point2D(); - Point2D(vector _pointCollection); - Point2D(Point2D const &sourcePoint2D); - Point2D(Point2D &&sourcePoint2D); - ~Point2D(); - - typedef vector::iterator iterator; - - iterator begin(); - iterator end(); + private: + class Impl; + unique_ptr pimpl; + + public: + struct Iterator{ + public: + using iterator_category = input_iterator_tag; + using difference_type = ptrdiff_t; + using value_type = SimplePoint2D; + using pointer = SimplePoint2D*; + using reference = SimplePoint2D&; + Iterator() {m_ptr = nullptr;} + Iterator(pointer ptr): m_ptr(ptr){} + + const reference operator*()const{return *m_ptr;} + const pointer operator->(){return m_ptr;} + + Iterator& operator++() + { + m_ptr++; + return *this; + } + Iterator operator++(int) + { + Iterator tmp = *this; + ++(*this); + return tmp; + } + + friend bool operator==(const Iterator& a, const Iterator& b) + { + return a.m_ptr == b.m_ptr; + }; + friend bool operator!=(const Iterator& a, const Iterator& b) + { + return a.m_ptr != b.m_ptr; + }; + + + private: + pointer m_ptr; + + }; + + Point2D(); + Point2D(vector _pointCollection, bool _ordered); + Point2D(vector _pointCollection); + Point2D(Point2D const &sourcePoint2D); + Point2D(Point2D &&sourcePoint2D); + ~Point2D(); + int count(); + Iterator begin(); + Iterator end(); }; -#endif //POINT2D_H +//Point2D randomPoint2D(long count, int minX, int maxX, int minY, int maxY); + +#endif //POINT2D_H \ No newline at end of file diff --git a/Point2DImplr.cpp b/Point2DImplr.cpp new file mode 100644 index 0000000..efa6e3d --- /dev/null +++ b/Point2DImplr.cpp @@ -0,0 +1,5 @@ +#include "Point2DImplr.h" +vector Point2DImplr::getPoints() +{ + return Point2D::pimpl->pointCollection; +} \ No newline at end of file diff --git a/Point2DImplr.h b/Point2DImplr.h new file mode 100644 index 0000000..9f1fcb5 --- /dev/null +++ b/Point2DImplr.h @@ -0,0 +1,11 @@ +#ifndef POINT2DIMPLR_H +#define POINT2DIMPLR_H +#include "Point2D.h" +#include + +struct Point2DImplr : public Point2D +{ + vector getPoints(); +}; + +#endif //POINT2DIMPLR_H diff --git a/Region2D.cpp b/Region2D.cpp index 00ea77a..641f9fc 100644 --- a/Region2D.cpp +++ b/Region2D.cpp @@ -7,6 +7,7 @@ #include "HalfSegment2D.h" #include "Segment2D.h" #include +#include //Implementation struct Region2D::Impl { @@ -17,6 +18,7 @@ struct Region2D::Impl { std::vector regionSegments; std::vector halfSegments; + std::vector segments; void setFlags(); SimplePoint2D GetDominatePoint(HalfSegment2D inputHalfSegment); @@ -35,6 +37,8 @@ Region2D::Impl::~Impl() {} Region2D::Impl::Impl(std::vector _regionSegments) { + this->segments = _regionSegments; + std::vector HalfSegVec; //temporary vector of half segments for later use for(int i = 0; i < _regionSegments.size(); i++) { @@ -45,9 +49,7 @@ Region2D::Impl::Impl(std::vector _regionSegments) } std::sort(HalfSegVec.begin(), HalfSegVec.end()); - this->halfSegments = HalfSegVec; - setFlags(); } SimplePoint2D Region2D::Impl::GetDominatePoint(HalfSegment2D inputHalfSegment) @@ -82,20 +84,19 @@ bool Region2D::Impl::GetAboveFlag(HalfSegment2D currentHalfSeg) bool Region2D::Impl::CheckLessThan(SimplePoint2D dp, HalfSegment2D halfSeg) { Segment2D seg = halfSeg.s; - SimplePoint2D leftPoint = seg.leftEndPoint; - SimplePoint2D rightPoint = seg.rightEndPoint; - Number slope = (rightPoint.y - leftPoint.y) / (rightPoint.x - leftPoint.x); - Number b = leftPoint.y / (slope * leftPoint.x); - Number halfSegY = (dp.x * slope) + b; - if(dp.y < halfSegY) - { - return true; - } - else - { - return false; + SimplePoint2D left = seg.leftEndPoint; + SimplePoint2D right = seg.rightEndPoint; + + // handle infinite slope + if (right.x == left.x) { + return dp < left; } - + + Number slope = (right.y - left.y) / (right.x - left.x); + Number b = left.y - (slope * left.x); // The minus '-' was originally a '/'. I think this is supposed to be based on y=mx+b, which rewrites into b=y-mx. + Number halfSegY = (dp.x * slope) + b; + + return dp.y < halfSegY; } void Region2D::Impl::setFlags() @@ -142,7 +143,7 @@ void Region2D::Impl::setFlags() } else { //remove halfsegment from status - for(int i = 0; i < sweepStatus.size(); i++) + for(int i = 0; i < sweepStatus.size(); i++) { if(sweepStatus[i].s == currentHalfSeg.s) { @@ -176,3 +177,41 @@ Region2D::Region2D(std::vector region) : pimpl(new Impl(region)) {} //move constructor Region2D::Region2D(Region2D &®ion) : pimpl(std::move(region.pimpl)) {} +//destructor +Region2D::~Region2D() {} + +//iterator methods +Region2D::iterator Region2D::begin() +{ + return this->pimpl->regionSegments.begin(); +} +Region2D::iterator Region2D::end() +{ + return this->pimpl->regionSegments.end(); +} + +/*void Region2D::print() +{ + for (Segment2D s : this->getSegments()) + s.print(); +}*/ + +std::vector Region2D::getSegments() +{ + std::vector segments; + for (HalfSegment2D h : this->pimpl->halfSegments) + if (h.isDominatingPointLeft) + segments.push_back(h.s); + return segments; +} + +bool Region2D::operator==(const Region2D& other) const +{ + for (int i = 0; i < std::min(this->pimpl->halfSegments.size(), other.pimpl->halfSegments.size()); i++) { + HalfSegment2D h1 = this->pimpl->halfSegments[i]; + HalfSegment2D h2 = other.pimpl->halfSegments[i]; + if (h1 != h2) + return false; + } + return true; +} \ No newline at end of file diff --git a/Region2D.h b/Region2D.h index f8acb77..024b52e 100644 --- a/Region2D.h +++ b/Region2D.h @@ -16,10 +16,18 @@ class Region2D { public: Region2D(); + ~Region2D(); Region2D(std::vector region); Region2D(Region2D const ®ion); Region2D(Region2D &®ion); + + typedef std::vector::iterator iterator; + iterator begin(); + iterator end(); + //void print(); + std::vector getSegments(); + bool operator==(const Region2D& other) const; }; -#endif //REGION2D_H +#endif //REGION2D_H \ No newline at end of file From 4ecd6c7249f216f92ef6e54af2aa3ad13c1fc46d Mon Sep 17 00:00:00 2001 From: shudgit <119703954+shudgit@users.noreply.github.com> Date: Fri, 7 Apr 2023 14:10:35 -0400 Subject: [PATCH 2/3] Delete Point2DImplr.cpp --- Point2DImplr.cpp | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 Point2DImplr.cpp diff --git a/Point2DImplr.cpp b/Point2DImplr.cpp deleted file mode 100644 index efa6e3d..0000000 --- a/Point2DImplr.cpp +++ /dev/null @@ -1,5 +0,0 @@ -#include "Point2DImplr.h" -vector Point2DImplr::getPoints() -{ - return Point2D::pimpl->pointCollection; -} \ No newline at end of file From 3ebd83f78dce12edf0fa9895b9ab7e7cf1e64873 Mon Sep 17 00:00:00 2001 From: shudgit <119703954+shudgit@users.noreply.github.com> Date: Fri, 7 Apr 2023 14:10:47 -0400 Subject: [PATCH 3/3] Delete Point2DImplr.h --- Point2DImplr.h | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 Point2DImplr.h diff --git a/Point2DImplr.h b/Point2DImplr.h deleted file mode 100644 index 9f1fcb5..0000000 --- a/Point2DImplr.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef POINT2DIMPLR_H -#define POINT2DIMPLR_H -#include "Point2D.h" -#include - -struct Point2DImplr : public Point2D -{ - vector getPoints(); -}; - -#endif //POINT2DIMPLR_H