Skip to content

implemented graham scan in C plus plus #616

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 6 commits into from
Jul 10, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ This file lists everyone, who contributed to this repo and wanted to show up her
- Olav Sundfør
- dovisutu
- Antetokounpo
- Akash Dhiman
65 changes: 65 additions & 0 deletions contents/graham_scan/code/c++/graham_scan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <iostream>
#include <vector>
#include <algorithm>

struct point{
double x;
double y;
};

bool ccw(point a, point b, point c){
return ((b.x - a.x)*(c.y - a.y) > (b.y - a.y)*(c.x - a.x));
}

std::vector<point> grahamScan(std::vector<point> points){
//selecting lowest point as pivot
int lowIndex = 0;
for(size_t i = 1; i < points.size(); i++) {
if(points[i].y < points[lowIndex].y){
lowIndex = i;
}
}
std::swap(points[0], points[lowIndex]);
point pivot = points[0];

//sorting points by polar angle
std::sort(points.begin()+1, points.end(), [pivot](point a, point b){
return ccw(pivot, a, b);
});

//creating convex hull
size_t m = 1;
for(size_t i = 2; i < points.size(); i++) {
while(ccw(points[m - 1], points[m], points[i]) <= 0){
if(m > 1) {
m--;
continue;
} else if(i == points.size()) {
break;
} else {
i++;
}
}
m++;
std::swap(points[i], points[m]);
}
return std::vector<point>(points.begin(), points.begin() + m + 1);
}

void print(std::vector<point> points){
for ( auto p : points){
std::cout <<"(" << p.x << ", " << p.y <<")\n";
}
}

int main(){
std::vector<point> points = {{-5, 2}, {5, 7}, {-6, -12}, {-14, -14}, {9, 9},
{-1, -1}, {-10, 11}, {-6, 15}, {-6, -8}, {15, -9},
{7, -7}, {-2, -9}, {6, -5}, {0, 14}, {2, 8}};
std::cout << "original points are as follows:\n";
print(points);
std::vector<point> hull = grahamScan(points);
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd like a printing statment before finding hull, in order to show the points.

std::cout << "points in hull are as follows:\n";
print(hull);
return 0;
}
6 changes: 6 additions & 0 deletions contents/graham_scan/graham_scan.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ We can find whether a rotation is counter-clockwise with trigonometric functions
[import:13-15, lang:"go"](code/golang/graham.go)
{% sample lang="java" %}
[import:27-29, lang:"java"](code/java/GrahamScan.java)
{% sample lang="cpp" %}
[import:10-12, lang="cpp"](code/c++/graham_scan.cpp)
{% endmethod %}

If the output of this function is 0, the points are collinear.
Expand Down Expand Up @@ -54,6 +56,8 @@ In the end, the code should look something like this:
[import:21-42, lang:"go"](code/golang/graham.go)
{% sample lang="java" %}
[import:35-70, lang:"java"](code/java/GrahamScan.java)
{% sample lang="cpp" %}
[import:14-47, lang="cpp"](code/c++/graham_scan.cpp)
{% endmethod %}

### Bibliography
Expand All @@ -77,6 +81,8 @@ In the end, the code should look something like this:
[import, lang:"go"](code/golang/graham.go)
{% sample lang="java" %}
[import, lang:"java"](code/java/GrahamScan.java)
{% sample lang="cpp" %}
[import, lang="cpp"](code/c++/graham_scan.cpp)
{% endmethod %}

<script>
Expand Down