Skip to content

Commit 4c73370

Browse files
berquistleios
authored andcommitted
Apply clang-format to Graham Scan in C++ (algorithm-archivists#641)
* Apply clang-format to Graham Scan in C++ * camel_case rather than pascalCase for C++ * Use const and references where possible * Replace print function with printing to ostream * Use const references in sorting lambda * Actually sort initial gift by polar angle
1 parent b98217a commit 4c73370

File tree

2 files changed

+62
-43
lines changed

2 files changed

+62
-43
lines changed
+60-41
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,84 @@
1+
#include <algorithm>
2+
#include <cmath>
13
#include <iostream>
24
#include <vector>
3-
#include <algorithm>
45

5-
struct point{
6+
struct point {
67
double x;
78
double y;
89
};
910

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));
11+
std::ostream& operator<<(std::ostream& os, const std::vector<point>& points) {
12+
for (auto p : points) {
13+
os << "(" << p.x << ", " << p.y << ")\n";
14+
}
15+
return os;
16+
}
17+
18+
double ccw(const point& a, const point& b, const point& c) {
19+
return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
1220
}
1321

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;
22+
double polar_angle(const point& origin, const point& p) {
23+
return std::atan2(p.y - origin.y, p.x - origin.x);
24+
}
25+
26+
std::vector<point> graham_scan(std::vector<point>& points) {
27+
// selecting lowest point as pivot
28+
size_t low_index = 0;
29+
for (size_t i = 1; i < points.size(); i++) {
30+
if (points[i].y < points[low_index].y) {
31+
low_index = i;
2032
}
2133
}
22-
std::swap(points[0], points[lowIndex]);
34+
std::swap(points[0], points[low_index]);
2335
point pivot = points[0];
2436

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-
});
37+
// sorting points by polar angle
38+
std::sort(
39+
points.begin() + 1,
40+
points.end(),
41+
[&pivot](const point& pa, const point& pb) {
42+
return polar_angle(pivot, pa) < polar_angle(pivot, pb);
43+
});
2944

30-
//creating convex hull
45+
// creating convex hull
3146
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-
}
47+
for (size_t i = 2; i < points.size(); i++) {
48+
while (ccw(points[m - 1], points[m], points[i]) <= 0) {
49+
if (m > 1) {
50+
m--;
51+
continue;
52+
} else if (i == points.size()) {
53+
break;
54+
} else {
55+
i++;
56+
}
4257
}
4358
m++;
4459
std::swap(points[i], points[m]);
4560
}
4661
return std::vector<point>(points.begin(), points.begin() + m + 1);
4762
}
4863

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+
int main() {
65+
std::vector<point> points = {{-5, 2},
66+
{5, 7},
67+
{-6, -12},
68+
{-14, -14},
69+
{9, 9},
70+
{-1, -1},
71+
{-10, 11},
72+
{-6, 15},
73+
{-6, -8},
74+
{15, -9},
75+
{7, -7},
76+
{-2, -9},
77+
{6, -5},
78+
{0, 14},
79+
{2, 8}};
80+
std::cout << "original points are as follows:\n" << points;
81+
const std::vector<point> hull = graham_scan(points);
82+
std::cout << "points in hull are as follows:\n" << hull;
6483
return 0;
6584
}

contents/graham_scan/graham_scan.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ We can find whether a rotation is counter-clockwise with trigonometric functions
2727
{% sample lang="java" %}
2828
[import:27-29, lang:"java"](code/java/GrahamScan.java)
2929
{% sample lang="cpp" %}
30-
[import:10-12, lang="cpp"](code/c++/graham_scan.cpp)
30+
[import:18-20, lang="cpp"](code/c++/graham_scan.cpp)
3131
{% endmethod %}
3232

3333
If the output of this function is 0, the points are collinear.
@@ -57,7 +57,7 @@ In the end, the code should look something like this:
5757
{% sample lang="java" %}
5858
[import:35-70, lang:"java"](code/java/GrahamScan.java)
5959
{% sample lang="cpp" %}
60-
[import:14-47, lang="cpp"](code/c++/graham_scan.cpp)
60+
[import:26-62, lang="cpp"](code/c++/graham_scan.cpp)
6161
{% endmethod %}
6262

6363
### Bibliography

0 commit comments

Comments
 (0)