Skip to content

Added Jarvis March for C++ #283

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Aug 3, 2018
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 2 additions & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,5 @@ Max Weinstein
<br>
Gibus Wearing Brony
<br>
Gorzoid
<br>
64 changes: 64 additions & 0 deletions contents/jarvis_march/code/c++/jarvis_march.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <vector>
#include <iostream>
#include <algorithm>

struct Point
{
double x, y;

bool operator==(const Point& b) const
{
return x == b.x && y == b.y;
}
};

template<typename ForwardIter>
std::vector<Point> jarvis_march(ForwardIter start, ForwardIter end)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Take as input const std::vector<Point> &

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It crashes in case of empty set. Should return an empty vector.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is possible to make it work without making empty vector a special case. For instance use while loop instead of do...while. Can you play around and try to refactor code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I take out the do...while I need to add extra code to add the first element. Anyway the main problem is the min_element to find the left most point. If empty it will return points.end(), I can't think of any way to resolve that would result in shorter code.

{
std::vector<Point> 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){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[&p1] is slightly better.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually incorrect, [p1] will generate better code since the Point struct is fairly small (and so you can pass it in registers). Since you don't need to mutate the captured variable, I would stick with the by value version

return (p1 == p2) || (p2.x - p1.x) * (p3.y - p1.y) > (p3.x - p1.x) * (p2.y - p1.y);
}
);
}
while(next_point_it != first_point_it);

return hull_points;
}

int main() {
std::vector<Point> 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;
}
}

2 changes: 2 additions & 0 deletions contents/jarvis_march/jarvis_march.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}

<script>
Expand Down