diff --git a/includes/polygon.hpp b/includes/polygon.hpp index 0eeb44b..a81c4bd 100644 --- a/includes/polygon.hpp +++ b/includes/polygon.hpp @@ -15,6 +15,7 @@ class Polygon { private: std::shared_ptr> vertexs; + std::vector holes; std::shared_ptr> lines; std::shared_ptr> localminmaxs; bool clockwise; @@ -56,6 +57,7 @@ class Polygon { this->vertexs = std::make_shared>(vertexs.begin(), vertexs.end()); this->lines = std::make_shared>(); this->localminmaxs = std::make_shared>(); + this->holes = std::vector(); for (Point p : vertexs) { min_x = std::min(p.GetX(), min_x); max_x = std::max(p.GetX(), max_x); @@ -66,8 +68,13 @@ class Polygon { ConstructLocalMinMaxPoints(); DetermineClockwise(); } + Polygon(std::vector outbound_vertexs, std::vector holes) : Polygon(outbound_vertexs) { + this->holes = holes; + } + bool operator==(const Polygon &other) const { return this->GetVertexs() == other.GetVertexs(); } ~Polygon() = default; - std::shared_ptr> GetVertexs(); + std::shared_ptr> GetVertexs() const; + std::vector GetHoles() { return this->holes; }; std::vector GetVertexsVector(); std::shared_ptr> GetLocalMinMaxVertexs(); std::shared_ptr> GetLines(); diff --git a/src/polygon.cpp b/src/polygon.cpp index 8d4486b..19f0ea2 100644 --- a/src/polygon.cpp +++ b/src/polygon.cpp @@ -17,7 +17,7 @@ double Polygon::GetArea() { return 0.5 * abs(v); } -std::shared_ptr> Polygon::GetVertexs() { return this->vertexs; } +std::shared_ptr> Polygon::GetVertexs() const { return this->vertexs; } std::vector Polygon::GetVertexsVector() { return *this->vertexs; } diff --git a/tests/cpp/ut_polygon.cpp b/tests/cpp/ut_polygon.cpp index b399378..9f4760b 100644 --- a/tests/cpp/ut_polygon.cpp +++ b/tests/cpp/ut_polygon.cpp @@ -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 expected_vertexs = {Point(0, 0), Point(3, 0), Point(3, 3), Point(0, 3)}; + std::vector 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{hole_polygon}); + + std::shared_ptr> vertexs = polygon.GetVertexs(); + ASSERT_EQ(expected_vertexs, *vertexs.get()); +} + +TEST(POLYGON_TEST, test_constructor_with_holes_should_have_correct_holes) { + std::vector expected_vertexs = {Point(0, 0), Point(3, 0), Point(3, 3), Point(0, 3)}; + std::vector 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{hole_polygon}); + + std::vector holes = polygon.GetHoles(); + ASSERT_EQ(holes, std::vector{hole_polygon}); +} + TEST(POLYGON_TEST, test_get_area_with_valid_polygon_should_have_correct_value) { std::vector vertexs = {Point(1, 6), Point(5, 4), Point(-3, 3), Point(0, -4), Point(-4, -1), Point(-4, 4)}; Polygon polygon(vertexs); @@ -74,4 +96,4 @@ TEST(POLYGON_TEST, test_polygon_with_clockwise_vertex_set_should_determine_clock std::make_shared(std::vector{Point(0, 0), Point(-5, 0), Point(-2.5, 3)}); ASSERT_EQ(polygon->IsClockwise(), false); -} \ No newline at end of file +}