Skip to content

feat: Added Graham Scan Algorithm. #1836

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 55 commits into from
Nov 14, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
e53047e
Implementated Grahamscan Algorithm for Convex Hull
Lazeeez Nov 3, 2021
27d349a
Update graham_scan_algorithm.cpp
Lazeeez Nov 3, 2021
4a5987a
Update graham_scan_functions.h
Lazeeez Nov 3, 2021
d06a7b5
Update graham_scan_algorithm.cpp
Lazeeez Nov 3, 2021
f1b83dd
Update graham_scan_functions.h
Lazeeez Nov 3, 2021
95cd2f4
Update graham_scan_algorithm.cpp
Lazeeez Nov 3, 2021
23a5665
Update and rename graham_scan_functions.h to graham_scan_functions.hpp
Lazeeez Nov 3, 2021
17a2e85
Update geometry/graham_scan_algorithm.cpp
Lazeeez Nov 4, 2021
20f80fc
Update geometry/graham_scan_algorithm.cpp
Lazeeez Nov 4, 2021
6be4aaf
Merge branch 'master' into Lazeeez
Lazeeez Nov 4, 2021
a16bf9b
Update geometry/graham_scan_functions.hpp
Lazeeez Nov 4, 2021
e89e4c8
Update geometry/graham_scan_functions.hpp
Lazeeez Nov 6, 2021
c88a858
updating DIRECTORY.md
Nov 6, 2021
af70211
clang-format and clang-tidy fixes for e89e4c8c
Nov 6, 2021
7df4778
Merge branch 'master' into Lazeeez
Lazeeez Nov 6, 2021
ea11776
clang-format and clang-tidy fixes for 7df4778f
Nov 6, 2021
3331f18
Fix #1
Lazeeez Nov 6, 2021
6b0a7d0
Merge branch 'Lazeeez' of https://github.com/Lazeeez/C-Plus-Plus into…
Lazeeez Nov 6, 2021
a41612c
Update graham_scan_functions.hpp
Lazeeez Nov 6, 2021
f5ccb3e
Delete composite_simpson_rule.cpp
Lazeeez Nov 6, 2021
2d15e14
Delete inverse_fast_fourier_transform.cpp
Lazeeez Nov 6, 2021
69b6832
Fix #2
Lazeeez Nov 6, 2021
1a4bd72
updating DIRECTORY.md
Nov 6, 2021
e90e0d6
clang-format and clang-tidy fixes for 69b6832b
Nov 6, 2021
e68ba6c
Fix #3
Lazeeez Nov 6, 2021
1c05ca7
Merge branch 'Lazeeez' of https://github.com/Lazeeez/C-Plus-Plus into…
Lazeeez Nov 6, 2021
02696ce
clang-format and clang-tidy fixes for 1c05ca7c
Nov 6, 2021
44fd627
Update graham_scan_functions.hpp
Lazeeez Nov 6, 2021
2957fd2
Fix #4
Lazeeez Nov 7, 2021
9465ef2
clang-format and clang-tidy fixes for 2957fd21
Nov 7, 2021
e79e317
Create composite_simpson_rule.cpp
Panquesito7 Nov 7, 2021
13c3d22
updating DIRECTORY.md
Nov 7, 2021
405d21a
Create inverse_fast_fourier_transform.cpp
Panquesito7 Nov 7, 2021
280cf61
updating DIRECTORY.md
Nov 7, 2021
91d669a
clang-format and clang-tidy fixes for 405d21a5
Nov 7, 2021
333ef5c
Merge branch 'master' into Lazeeez
Panquesito7 Nov 7, 2021
176051b
clang-format and clang-tidy fixes for 333ef5ca
Nov 7, 2021
8577fcf
Update geometry/graham_scan_functions.hpp
Lazeeez Nov 7, 2021
7c3fc8b
Update geometry/graham_scan_algorithm.cpp
Lazeeez Nov 7, 2021
02ff191
Update geometry/graham_scan_algorithm.cpp
Lazeeez Nov 7, 2021
7ef02d1
Merge branch 'master' into Lazeeez
Panquesito7 Nov 7, 2021
f7f735b
Update geometry/graham_scan_algorithm.cpp
Lazeeez Nov 8, 2021
bfe80a0
Update geometry/graham_scan_functions.hpp
Lazeeez Nov 8, 2021
47d3758
Update geometry/graham_scan_algorithm.cpp
Lazeeez Nov 8, 2021
ee4cb63
Update geometry/graham_scan_algorithm.cpp
Lazeeez Nov 8, 2021
ef83af8
clang-format and clang-tidy fixes for ee4cb635
Nov 8, 2021
d502cfe
Update graham_scan_algorithm.cpp
Lazeeez Nov 8, 2021
f2f6923
Update graham_scan_functions.hpp
Lazeeez Nov 8, 2021
fd92a75
clang-format and clang-tidy fixes for f2f69234
Nov 8, 2021
3b91daa
Update graham_scan_functions.hpp
Lazeeez Nov 8, 2021
cafe74b
Create partition_problem.cpp
Lazeeez Nov 9, 2021
bfa7d06
Update partition_problem.cpp
Lazeeez Nov 9, 2021
3d2c81b
Delete partition_problem.cpp
Lazeeez Nov 9, 2021
b72d427
Merge branch 'master' into Lazeeez
Lazeeez Nov 14, 2021
12e3851
Merge branch 'master' into Lazeeez
Panquesito7 Nov 14, 2021
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
Prev Previous commit
Next Next commit
clang-format and clang-tidy fixes for e89e4c8
  • Loading branch information
github-actions authored and github-actions committed Nov 6, 2021
commit af702116fdc77f9e48334569d5fcdfcebba628e5
99 changes: 48 additions & 51 deletions geometry/graham_scan_algorithm.cpp
Original file line number Diff line number Diff line change
@@ -1,61 +1,58 @@
/******************************************************************************
* @file
* @brief Implementation of the [Convex Hull](https://en.wikipedia.org/wiki/Convex_hull)
* implementation using [Graham Scan](https://en.wikipedia.org/wiki/Graham_scan)
* @details
* In geometry, the convex hull or convex envelope or convex closure of a shape
* is the smallest convex set that contains it. The convex hull may be defined
* either as the intersection of all convex sets containing a given subset of a
* Euclidean space, or equivalently as the set of all convex combinations of
* points in the subset. For a bounded subset of the plane, the convex hull may
* be visualized as the shape enclosed by a rubber band stretched around the subset.
*
* The worst case time complexity of Jarvis’s Algorithm is O(n^2). Using Graham’s
* scan algorithm, we can find Convex Hull in O(nLogn) time.
*
* ### Implementation
*
* Sort points
* We first find the bottom-most point. The idea is to pre-process
* points be sorting them with respect to the bottom-most point. Once the points
* are sorted, they form a simple closed path.
* The sorting criteria is to use the orientation to compare angles without actually
* computing them (See the compare() function below) because computation of actual
* angles would be inefficient since trigonometric functions are not simple to evaluate.
*
* Accept or Reject Points
* Once we have the closed path, the next step is to traverse the path and
* remove concave points on this path using orientation. The first two points in
* sorted array are always part of Convex Hull. For remaining points, we keep track
* of recent three points, and find the angle formed by them. Let the three points
* be prev(p), curr(c) and next(n). If orientation of these points (considering them
* in same order) is not counterclockwise, we discard c, otherwise we keep it.
*
* @author [Lajat Manekar](https://github.com/Lazeeez)
*
*******************************************************************************/
#include <iostream> /// for IO Operations
#include <cassert> /// for std::assert
#include <vector> /// for std::vector
#include "./graham_scan_functions.hpp" /// for all the functions used
* @file
* @brief Implementation of the [Convex
*Hull](https://en.wikipedia.org/wiki/Convex_hull) implementation using [Graham
*Scan](https://en.wikipedia.org/wiki/Graham_scan)
* @details
* In geometry, the convex hull or convex envelope or convex closure of a shape
* is the smallest convex set that contains it. The convex hull may be defined
* either as the intersection of all convex sets containing a given subset of a
* Euclidean space, or equivalently as the set of all convex combinations of
* points in the subset. For a bounded subset of the plane, the convex hull may
* be visualized as the shape enclosed by a rubber band stretched around the
*subset.
*
* The worst case time complexity of Jarvis’s Algorithm is O(n^2). Using
*Graham’s scan algorithm, we can find Convex Hull in O(nLogn) time.
*
* ### Implementation
*
* Sort points
* We first find the bottom-most point. The idea is to pre-process
* points be sorting them with respect to the bottom-most point. Once the points
* are sorted, they form a simple closed path.
* The sorting criteria is to use the orientation to compare angles without
*actually computing them (See the compare() function below) because computation
*of actual angles would be inefficient since trigonometric functions are not
*simple to evaluate.
*
* Accept or Reject Points
* Once we have the closed path, the next step is to traverse the path and
* remove concave points on this path using orientation. The first two points in
* sorted array are always part of Convex Hull. For remaining points, we keep
*track of recent three points, and find the angle formed by them. Let the three
*points be prev(p), curr(c) and next(n). If orientation of these points
*(considering them in same order) is not counterclockwise, we discard c,
*otherwise we keep it.
*
* @author [Lajat Manekar](https://github.com/Lazeeez)
*
*******************************************************************************/
#include <cassert> /// for std::assert
#include <iostream> /// for IO Operations
#include <vector> /// for std::vector

#include "./graham_scan_functions.hpp" /// for all the functions used

/*******************************************************************************
* @brief Self-test implementations
* @returns void
*******************************************************************************/
static void test() {
std::vector<geometry::grahamscan::Point> points = {{0, 3},
{1, 1},
{2, 2},
{4, 4},
{0, 0},
{1, 2},
{3, 1},
{3, 3}};
std::vector<geometry::grahamscan::Point> expected_result = {{0, 3},
{4, 4},
{3, 1},
{0, 0}};
std::vector<geometry::grahamscan::Point> points = {
{0, 3}, {1, 1}, {2, 2}, {4, 4}, {0, 0}, {1, 2}, {3, 1}, {3, 3}};
std::vector<geometry::grahamscan::Point> expected_result = {
{0, 3}, {4, 4}, {3, 1}, {0, 0}};
std::vector<geometry::grahamscan::Point> derived_result;
std::vector<geometry::grahamscan::Point> res;

Expand Down
Loading