-
-
Notifications
You must be signed in to change notification settings - Fork 360
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
Changes from 1 commit
8875a08
248e5ae
49b73d1
f3e18a9
bedad3e
c4e859d
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 | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,102 @@ | ||||||
#include<iostream> | ||||||
#include<vector> | ||||||
|
||||||
struct point { | ||||||
double x; | ||||||
double y; | ||||||
}; | ||||||
|
||||||
point origin; | ||||||
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. Don't use a global
Suggested change
|
||||||
|
||||||
void swap(point &p1, point &p2){ | ||||||
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's a swap function |
||||||
point temp = p1; | ||||||
p1 = p2; | ||||||
p2 = temp; | ||||||
} | ||||||
|
||||||
int distSq(point p1, point p2){ | ||||||
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. As |
||||||
return (p1.x - p2.x)*(p1.x - p2.x) +(p1.y - p2.y)*(p1.y - p2.y); | ||||||
} | ||||||
|
||||||
int ccwCheck(point a, point b, point c){ | ||||||
|
||||||
int crossProduct = (b.y - a.y) * (c.x - b.x) - (b.x - a.x) * (c.y - b.y); | ||||||
if (crossProduct == 0) | ||||||
{ | ||||||
return 0; | ||||||
} | ||||||
return ( crossProduct > 0)? -1 : 1; | ||||||
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's unnececcary to return 1,0 or -1. It's not a bool, so we have to compare it again, therefore, why not just return it?
Suggested change
|
||||||
} | ||||||
|
||||||
int compare(const void *vp1, const void *vp2){ | ||||||
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. The |
||||||
|
||||||
point *p1 = (point *)vp1; | ||||||
point *p2 = (point *)vp2; | ||||||
|
||||||
int o = ccwCheck(origin, *p1, *p2); | ||||||
if (o == 0) | ||||||
return (distSq(origin, *p2) >= distSq(origin, *p1))? -1 : 1; | ||||||
|
||||||
return (o == 1)? -1: 1; | ||||||
} | ||||||
|
||||||
void print(std::vector<point> points) | ||||||
{ | ||||||
std::cout << "the points of hull are as follows:\n"; | ||||||
for (size_t i = 0; i < points.size(); i++) { | ||||||
std::cout << "(" << points[i].x << "," << points[i].y << ")\n"; | ||||||
} | ||||||
} | ||||||
|
||||||
std::vector<point> grahamScan(std::vector<point> points){ | ||||||
//selecting origin(the element with y minimum) | ||||||
double yMin = points[0].y; | ||||||
int min = 0; | ||||||
for (size_t i = 1; i < points.size(); i++) { | ||||||
double y = points[i].y; | ||||||
if ((y < yMin) || (yMin == y && points[i].x < points[min].x)){ | ||||||
yMin = points[i].y; | ||||||
min = i; | ||||||
} | ||||||
} | ||||||
swap(points[0], points[min]); | ||||||
origin = points[0]; | ||||||
|
||||||
//sorting by polar angle and removing duplicates | ||||||
qsort(&points[1], points.size() - 1, sizeof(point), compare); | ||||||
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 prefer to use |
||||||
std::vector<point> pointsSorted; | ||||||
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. You don't needed to create new one. original one is OK to edit |
||||||
pointsSorted.push_back(origin); | ||||||
for(size_t i = 1; i < points.size(); i++){ | ||||||
while(i < points.size() - 1 && ccwCheck(origin, points[i], | ||||||
points[i+1]) == 0){ | ||||||
i++; | ||||||
} | ||||||
pointsSorted.push_back(points[i]); | ||||||
} | ||||||
|
||||||
//creating the convex hull | ||||||
std::vector<point> hull; | ||||||
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. As mentioned, the algorithm shouldn't create a new vector to store hull.
|
||||||
hull.push_back(points[0]); | ||||||
hull.push_back(points[1]); | ||||||
hull.push_back(points[2]); | ||||||
for (size_t i = 3; i <= pointsSorted.size(); i++) { | ||||||
while (ccwCheck(hull.at(hull.size()-2), hull.at(hull.size()-1), | ||||||
pointsSorted[i]) != 1) | ||||||
{ | ||||||
hull.pop_back(); | ||||||
} | ||||||
hull.push_back(points[i]); | ||||||
} | ||||||
return hull; | ||||||
} | ||||||
|
||||||
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::vector<point> hull = grahamScan(points); | ||||||
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'd like a printing statment before finding hull, in order to show the points. |
||||||
print(hull); | ||||||
return 0; | ||||||
} |
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.
Please space between
#include
and header in order to be consistent with other implementations in AAA.