-
-
Notifications
You must be signed in to change notification settings - Fork 360
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
Changes from 5 commits
cc68b05
3b87072
d737275
1ff6c58
cf3cce2
c46039d
6a637b0
1618007
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,3 +44,5 @@ Max Weinstein | |
<br> | ||
Gibus Wearing Brony | ||
<br> | ||
Gorzoid | ||
<br> |
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It crashes in case of empty set. Should return an empty vector. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is actually incorrect, |
||
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; | ||
} | ||
} | ||
|
There was a problem hiding this comment.
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> &