From cc68b0523cf35728c0144ebc0520da62807cfbe0 Mon Sep 17 00:00:00 2001 From: gorzoid Date: Fri, 20 Jul 2018 00:56:52 +0100 Subject: [PATCH 1/7] Added cpp file --- .../jarvis_march/code/c++/jarvis_march.cpp | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 contents/jarvis_march/code/c++/jarvis_march.cpp diff --git a/contents/jarvis_march/code/c++/jarvis_march.cpp b/contents/jarvis_march/code/c++/jarvis_march.cpp new file mode 100644 index 000000000..9a8423883 --- /dev/null +++ b/contents/jarvis_march/code/c++/jarvis_march.cpp @@ -0,0 +1,70 @@ +#include +#include +#include + +#define NUMPOINTS 64 + +struct Point +{ + double x, y; + + bool operator==(const Point& b) const + { + return x == b.x && y == b.y; + } +}; + +template +std::vector jarvis_march(ForwardIter start, ForwardIter end) +{ + std::vector hull_points; + + // Left most point + auto first_point_it = std::min_element(start, end, [](const Point& a, const Point& b){ return a.x < b.x; }); + + + auto next_point_it = first_point_it; + do + { + + hull_points.push_back(*next_point_it); + + const Point& p1 = hull_points.back(); + + // Largest internal angle + next_point_it = std::max_element( + start, + end, + [p1](const Point& p2, const Point& p3) + { + return (p1 == p2) || (p2.x - p1.x) * (p3.y - p1.y) > (p3.x - p1.x) * (p2.y - p1.y); // p2 < p3 if p2 == p3 or p3 is left of the line p1 -> p2 + } + ); + } + while(next_point_it != first_point_it); + + return hull_points; +} + +int main() { + std::vector points = { + { 1.0, 3.0 }, + { 2.0, 4.0 }, + { 4.0, 0.0 }, + { 1.0, 0.0 }, + { 0.0, 2.0 }, + { 2.0, 2.0 }, + { 3.0, 4.0 }, + { 3.0, 1.0 }, + }; + + auto hull_points = jarvis_march(std::begin(points), std::end(points)); + + std::cout << "Hull points are:" << std::endl; + + for(const Point& point : hull_points) + { + std::cout << '(' << point.x << ", " << point.y << ')' << std::endl; + } +} + From 3b870724ddd0fb39995418cb64330dc21b1f53fb Mon Sep 17 00:00:00 2001 From: gorzoid Date: Fri, 20 Jul 2018 00:58:56 +0100 Subject: [PATCH 2/7] Added Gorzoid to CONTRIBUTORS.md --- CONTRIBUTORS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 4dd246093..8591b5458 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -44,3 +44,5 @@ Max Weinstein
Gibus Wearing Brony
+Gorzoid +
From d7372751c8a0c5a6db82d4152e592df7561dd0a9 Mon Sep 17 00:00:00 2001 From: gorzoid Date: Fri, 20 Jul 2018 01:15:43 +0100 Subject: [PATCH 3/7] Fixed style to conform with guidelines https://github.com/algorithm-archivists/algorithm-archive/issues/18 --- contents/jarvis_march/code/c++/jarvis_march.cpp | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/contents/jarvis_march/code/c++/jarvis_march.cpp b/contents/jarvis_march/code/c++/jarvis_march.cpp index 9a8423883..a6feed59f 100644 --- a/contents/jarvis_march/code/c++/jarvis_march.cpp +++ b/contents/jarvis_march/code/c++/jarvis_march.cpp @@ -22,11 +22,9 @@ std::vector jarvis_march(ForwardIter start, ForwardIter end) // Left most point auto first_point_it = std::min_element(start, end, [](const Point& a, const Point& b){ return a.x < b.x; }); - auto next_point_it = first_point_it; do { - hull_points.push_back(*next_point_it); const Point& p1 = hull_points.back(); @@ -35,9 +33,8 @@ std::vector jarvis_march(ForwardIter start, ForwardIter end) next_point_it = std::max_element( start, end, - [p1](const Point& p2, const Point& p3) - { - return (p1 == p2) || (p2.x - p1.x) * (p3.y - p1.y) > (p3.x - p1.x) * (p2.y - p1.y); // p2 < p3 if p2 == p3 or p3 is left of the line p1 -> p2 + [p1](const Point& p2, const Point& p3){ + return (p1 == p2) || (p2.x - p1.x) * (p3.y - p1.y) > (p3.x - p1.x) * (p2.y - p1.y); } ); } @@ -62,8 +59,7 @@ int main() { std::cout << "Hull points are:" << std::endl; - for(const Point& point : hull_points) - { + for(const Point& point : hull_points) { std::cout << '(' << point.x << ", " << point.y << ')' << std::endl; } } From 1ff6c5853f2d28221644c830732198b90d4dec55 Mon Sep 17 00:00:00 2001 From: gorzoid Date: Fri, 20 Jul 2018 01:24:56 +0100 Subject: [PATCH 4/7] Removed unnecessary define --- contents/jarvis_march/code/c++/jarvis_march.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/contents/jarvis_march/code/c++/jarvis_march.cpp b/contents/jarvis_march/code/c++/jarvis_march.cpp index a6feed59f..48d5f492f 100644 --- a/contents/jarvis_march/code/c++/jarvis_march.cpp +++ b/contents/jarvis_march/code/c++/jarvis_march.cpp @@ -2,8 +2,6 @@ #include #include -#define NUMPOINTS 64 - struct Point { double x, y; From cf3cce2f5ab7979d4d63cc352c16e13d882523e6 Mon Sep 17 00:00:00 2001 From: gorzoid Date: Fri, 20 Jul 2018 01:36:30 +0100 Subject: [PATCH 5/7] Added entry to jarvis_march.md --- contents/jarvis_march/jarvis_march.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/contents/jarvis_march/jarvis_march.md b/contents/jarvis_march/jarvis_march.md index 165756184..f4c690158 100644 --- a/contents/jarvis_march/jarvis_march.md +++ b/contents/jarvis_march/jarvis_march.md @@ -38,6 +38,8 @@ Program.cs [import, lang:"javascript"](code/javascript/jarvis-march.js) {% sample lang="py" %} [import, lang:"python"](code/py/jarvisMarch.py) +{% sample lang="cpp" %} +[import, lang:"c_cpp"](code/c++/jarvis_march.cpp) {% endmethod %}