Skip to content

Commit

Permalink
add comments, gitignore (#2)
Browse files Browse the repository at this point in the history
Co-authored-by: Akul Singhal <42439193+predator810@users.noreply.github.com>
  • Loading branch information
arcane810 and arcane810 authored Oct 2, 2020
1 parent ab93b29 commit 03ee142
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/a.exe
Binary file removed a.exe
Binary file not shown.
12 changes: 10 additions & 2 deletions grahamScan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ std::vector<Point> grahamScan(std::vector<Point> points)
{
return points;
}
std::vector<Point> convex_hull;

// Finding leftmost point (bottom most among them if there are multiple)
Point leftMostPoint = points[0];
for (Point point : points)
{
Expand All @@ -21,6 +22,8 @@ std::vector<Point> grahamScan(std::vector<Point> points)
leftMostPoint = point;
}
}

// Custom Comparator which will help in sorting points with respect to angle
auto comparator = [leftMostPoint](Point &left, Point &right) {
if (left == leftMostPoint)
{
Expand All @@ -42,8 +45,10 @@ std::vector<Point> grahamScan(std::vector<Point> points)
tempCH.push(points[0]);
tempCH.push(points[1]);

// Graham's Scan
for (int i = 2; i < points.size(); i++)
{
// Delete as many points as possible before adding current point
while (tempCH.size() > 1)
{
Point prev = tempCH.top();
Expand All @@ -56,14 +61,17 @@ std::vector<Point> grahamScan(std::vector<Point> points)
}
tempCH.pop();
}
// Add current point
tempCH.push(points[i]);
}

std::vector<Point> convex_hull;
while (!tempCH.empty())
{
convex_hull.push_back(tempCH.top());
tempCH.pop();
}

//Emptying the stack returns points in reverse order, reverse again to get CH in clockwise order
reverse(convex_hull.begin(), convex_hull.end());

return convex_hull;
Expand Down

0 comments on commit 03ee142

Please sign in to comment.