Skip to content

Commit

Permalink
Implement: Polygon with Holes support (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
ntut-xuan authored Dec 20, 2024
1 parent 56da7b8 commit 32a6127
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
9 changes: 8 additions & 1 deletion includes/polygon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
class Polygon {
private:
std::shared_ptr<std::vector<Point>> vertexs;
std::vector<Polygon> holes;
std::shared_ptr<std::vector<Line>> lines;
std::shared_ptr<std::set<Point>> localminmaxs;
bool clockwise;
Expand Down Expand Up @@ -56,6 +57,7 @@ class Polygon {
this->vertexs = std::make_shared<std::vector<Point>>(vertexs.begin(), vertexs.end());
this->lines = std::make_shared<std::vector<Line>>();
this->localminmaxs = std::make_shared<std::set<Point>>();
this->holes = std::vector<Polygon>();
for (Point p : vertexs) {
min_x = std::min(p.GetX(), min_x);
max_x = std::max(p.GetX(), max_x);
Expand All @@ -66,8 +68,13 @@ class Polygon {
ConstructLocalMinMaxPoints();
DetermineClockwise();
}
Polygon(std::vector<Point> outbound_vertexs, std::vector<Polygon> holes) : Polygon(outbound_vertexs) {
this->holes = holes;
}
bool operator==(const Polygon &other) const { return this->GetVertexs() == other.GetVertexs(); }
~Polygon() = default;
std::shared_ptr<std::vector<Point>> GetVertexs();
std::shared_ptr<std::vector<Point>> GetVertexs() const;
std::vector<Polygon> GetHoles() { return this->holes; };
std::vector<Point> GetVertexsVector();
std::shared_ptr<std::vector<Point>> GetLocalMinMaxVertexs();
std::shared_ptr<std::vector<Line>> GetLines();
Expand Down
2 changes: 1 addition & 1 deletion src/polygon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ double Polygon::GetArea() {
return 0.5 * abs(v);
}

std::shared_ptr<std::vector<Point>> Polygon::GetVertexs() { return this->vertexs; }
std::shared_ptr<std::vector<Point>> Polygon::GetVertexs() const { return this->vertexs; }

std::vector<Point> Polygon::GetVertexsVector() { return *this->vertexs; }

Expand Down
24 changes: 23 additions & 1 deletion tests/cpp/ut_polygon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,28 @@ TEST(POLYGON_TEST, test_constructor_should_have_correct_vertex) {
ASSERT_EQ(expected_vertexs, *vertexs.get());
}

TEST(POLYGON_TEST, test_constructor_with_holes_should_have_correct_vertex) {
std::vector<Point> expected_vertexs = {Point(0, 0), Point(3, 0), Point(3, 3), Point(0, 3)};
std::vector<Point> hole_vertexs = {Point(1, 1), Point(1, 2), Point(2, 2), Point(2, 1)};
Polygon hole_polygon = Polygon(hole_vertexs);

Polygon polygon(expected_vertexs, std::vector<Polygon>{hole_polygon});

std::shared_ptr<std::vector<Point>> vertexs = polygon.GetVertexs();
ASSERT_EQ(expected_vertexs, *vertexs.get());
}

TEST(POLYGON_TEST, test_constructor_with_holes_should_have_correct_holes) {
std::vector<Point> expected_vertexs = {Point(0, 0), Point(3, 0), Point(3, 3), Point(0, 3)};
std::vector<Point> hole_vertexs = {Point(1, 1), Point(1, 2), Point(2, 2), Point(2, 1)};
Polygon hole_polygon = Polygon(hole_vertexs);

Polygon polygon(expected_vertexs, std::vector<Polygon>{hole_polygon});

std::vector<Polygon> holes = polygon.GetHoles();
ASSERT_EQ(holes, std::vector<Polygon>{hole_polygon});
}

TEST(POLYGON_TEST, test_get_area_with_valid_polygon_should_have_correct_value) {
std::vector<Point> vertexs = {Point(1, 6), Point(5, 4), Point(-3, 3), Point(0, -4), Point(-4, -1), Point(-4, 4)};
Polygon polygon(vertexs);
Expand Down Expand Up @@ -74,4 +96,4 @@ TEST(POLYGON_TEST, test_polygon_with_clockwise_vertex_set_should_determine_clock
std::make_shared<Polygon>(std::vector<Point>{Point(0, 0), Point(-5, 0), Point(-2.5, 3)});

ASSERT_EQ(polygon->IsClockwise(), false);
}
}

0 comments on commit 32a6127

Please sign in to comment.