Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
test
*.swp
.vscode/settings.json
11 changes: 10 additions & 1 deletion Line2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,13 @@ Line2D::Line2D(Line2D &&sourceLine2D)
}

//destructor
Line2D::~Line2D(){}
Line2D::~Line2D(){}

Line2D::iterator Line2D::begin()
{
return this->pimpl->lineSegments.begin();
}
Line2D::iterator Line2D::end()
{
return this->pimpl->lineSegments.end();
}
4 changes: 4 additions & 0 deletions Line2D.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class Line2D {
Line2D(const Line2D &sourceLine2D); //copy constructor
Line2D(Line2D &&sourceLine2D); //move constructor
~Line2D(); //Destructor

typedef std::vector<HalfSegment2D>::iterator iterator;
iterator begin();
iterator end();
};


Expand Down
41 changes: 36 additions & 5 deletions Point2D.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "Point2D.h"
#include <algorithm>
#include <iostream>
using namespace std;


Expand All @@ -10,18 +11,34 @@ struct Point2D::Impl
Impl(vector<SimplePoint2D> _pointCollection): pointCollection(move(_pointCollection)){};

vector<SimplePoint2D> pointCollection;

bool ordered;
};
Point2D::Point2D() {}


Point2D::Point2D(vector<SimplePoint2D> _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<SimplePoint2D> _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);
Expand All @@ -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();
}
generateSeed();
std::vector<SimplePoint2D> points;
for (long i = 0; i < count; i++)
points.push_back(randomSimplePoint2D(minX, maxX, minY, maxY));
return Point2D(points);
}*/
73 changes: 57 additions & 16 deletions Point2D.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,63 @@ using namespace std;


class Point2D {
private:
class Impl;
unique_ptr<Impl> pimpl;

public:
Point2D();
Point2D(vector<SimplePoint2D> _pointCollection);
Point2D(Point2D const &sourcePoint2D);
Point2D(Point2D &&sourcePoint2D);
~Point2D();

typedef vector<SimplePoint2D>::iterator iterator;

iterator begin();
iterator end();
private:
class Impl;
unique_ptr<Impl> 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<SimplePoint2D> _pointCollection, bool _ordered);
Point2D(vector<SimplePoint2D> _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
71 changes: 55 additions & 16 deletions Region2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "HalfSegment2D.h"
#include "Segment2D.h"
#include <memory>
#include <iostream>

//Implementation
struct Region2D::Impl {
Expand All @@ -17,6 +18,7 @@ struct Region2D::Impl {

std::vector<AttributedHalfSegment2D> regionSegments;
std::vector<HalfSegment2D> halfSegments;
std::vector<Segment2D> segments;

void setFlags();
SimplePoint2D GetDominatePoint(HalfSegment2D inputHalfSegment);
Expand All @@ -35,6 +37,8 @@ Region2D::Impl::~Impl() {}

Region2D::Impl::Impl(std::vector<Segment2D> _regionSegments)
{
this->segments = _regionSegments;

std::vector<HalfSegment2D> HalfSegVec; //temporary vector of half segments for later use
for(int i = 0; i < _regionSegments.size(); i++)
{
Expand All @@ -45,9 +49,7 @@ Region2D::Impl::Impl(std::vector<Segment2D> _regionSegments)
}
std::sort(HalfSegVec.begin(), HalfSegVec.end());


this->halfSegments = HalfSegVec;
setFlags();
}

SimplePoint2D Region2D::Impl::GetDominatePoint(HalfSegment2D inputHalfSegment)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -176,3 +177,41 @@ Region2D::Region2D(std::vector<Segment2D> region) : pimpl(new Impl(region)) {}
//move constructor
Region2D::Region2D(Region2D &&region) : 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<Segment2D> Region2D::getSegments()
{
std::vector<Segment2D> 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;
}
10 changes: 9 additions & 1 deletion Region2D.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,18 @@ class Region2D {

public:
Region2D();
~Region2D();
Region2D(std::vector<Segment2D> region);
Region2D(Region2D const &region);
Region2D(Region2D &&region);

typedef std::vector<AttributedHalfSegment2D>::iterator iterator;
iterator begin();
iterator end();
//void print();
std::vector<Segment2D> getSegments();
bool operator==(const Region2D& other) const;
};


#endif //REGION2D_H
#endif //REGION2D_H