Skip to content

Commit e067fcb

Browse files
AkashDhimanleios
authored andcommitted
implemented graham scan in C plus plus (#616)
* implemented graham scan in cpp * improved code for graham-scan implementation in cpp * improved implementation based on suggestions * fixed the runtimeerror * fixed runtime error based on suggestion
1 parent 4793490 commit e067fcb

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

CONTRIBUTORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,4 @@ This file lists everyone, who contributed to this repo and wanted to show up her
4545
- Olav Sundfør
4646
- dovisutu
4747
- Antetokounpo
48+
- Akash Dhiman
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <algorithm>
4+
5+
struct point{
6+
double x;
7+
double y;
8+
};
9+
10+
bool ccw(point a, point b, point c){
11+
return ((b.x - a.x)*(c.y - a.y) > (b.y - a.y)*(c.x - a.x));
12+
}
13+
14+
std::vector<point> grahamScan(std::vector<point> points){
15+
//selecting lowest point as pivot
16+
int lowIndex = 0;
17+
for(size_t i = 1; i < points.size(); i++) {
18+
if(points[i].y < points[lowIndex].y){
19+
lowIndex = i;
20+
}
21+
}
22+
std::swap(points[0], points[lowIndex]);
23+
point pivot = points[0];
24+
25+
//sorting points by polar angle
26+
std::sort(points.begin()+1, points.end(), [pivot](point a, point b){
27+
return ccw(pivot, a, b);
28+
});
29+
30+
//creating convex hull
31+
size_t m = 1;
32+
for(size_t i = 2; i < points.size(); i++) {
33+
while(ccw(points[m - 1], points[m], points[i]) <= 0){
34+
if(m > 1) {
35+
m--;
36+
continue;
37+
} else if(i == points.size()) {
38+
break;
39+
} else {
40+
i++;
41+
}
42+
}
43+
m++;
44+
std::swap(points[i], points[m]);
45+
}
46+
return std::vector<point>(points.begin(), points.begin() + m + 1);
47+
}
48+
49+
void print(std::vector<point> points){
50+
for ( auto p : points){
51+
std::cout <<"(" << p.x << ", " << p.y <<")\n";
52+
}
53+
}
54+
55+
int main(){
56+
std::vector<point> points = {{-5, 2}, {5, 7}, {-6, -12}, {-14, -14}, {9, 9},
57+
{-1, -1}, {-10, 11}, {-6, 15}, {-6, -8}, {15, -9},
58+
{7, -7}, {-2, -9}, {6, -5}, {0, 14}, {2, 8}};
59+
std::cout << "original points are as follows:\n";
60+
print(points);
61+
std::vector<point> hull = grahamScan(points);
62+
std::cout << "points in hull are as follows:\n";
63+
print(hull);
64+
return 0;
65+
}

contents/graham_scan/graham_scan.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ We can find whether a rotation is counter-clockwise with trigonometric functions
2626
[import:13-15, lang:"go"](code/golang/graham.go)
2727
{% sample lang="java" %}
2828
[import:27-29, lang:"java"](code/java/GrahamScan.java)
29+
{% sample lang="cpp" %}
30+
[import:10-12, lang="cpp"](code/c++/graham_scan.cpp)
2931
{% endmethod %}
3032

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

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

8288
<script>

0 commit comments

Comments
 (0)