TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
graham_scan_algorithm.cpp
1/******************************************************************************
2 * @file
3 * @brief Implementation of the [Convex
4 * Hull](https://en.wikipedia.org/wiki/Convex_hull) implementation using [Graham
5 * Scan](https://en.wikipedia.org/wiki/Graham_scan)
6 * @details
7 * In geometry, the convex hull or convex envelope or convex closure of a shape
8 * is the smallest convex set that contains it. The convex hull may be defined
9 * either as the intersection of all convex sets containing a given subset of a
10 * Euclidean space, or equivalently as the set of all convex combinations of
11 * points in the subset. For a bounded subset of the plane, the convex hull may
12 * be visualized as the shape enclosed by a rubber band stretched around the
13 * subset.
14 *
15 * The worst case time complexity of Jarvis’s Algorithm is O(n^2). Using
16 * Graham’s scan algorithm, we can find Convex Hull in O(nLogn) time.
17 *
18 * ### Implementation
19 *
20 * Sort points
21 * We first find the bottom-most point. The idea is to pre-process
22 * points be sorting them with respect to the bottom-most point. Once the points
23 * are sorted, they form a simple closed path.
24 * The sorting criteria is to use the orientation to compare angles without
25 * actually computing them (See the compare() function below) because
26 * computation of actual angles would be inefficient since trigonometric
27 * functions are not simple to evaluate.
28 *
29 * Accept or Reject Points
30 * Once we have the closed path, the next step is to traverse the path and
31 * remove concave points on this path using orientation. The first two points in
32 * sorted array are always part of Convex Hull. For remaining points, we keep
33 * track of recent three points, and find the angle formed by them. Let the
34 * three points be prev(p), curr(c) and next(n). If the orientation of these
35 * points (considering them in the same order) is not counterclockwise, we
36 * discard c, otherwise we keep it.
37 *
38 * @author [Lajat Manekar](https://github.com/Lazeeez)
39 *
40 *******************************************************************************/
41#include <cassert>
42#include <iostream>
43#include <vector>
44
45#include "./graham_scan_functions.hpp"
46
47/*******************************************************************************
48 * @brief Self-test implementations
49 * @returns void
50 *******************************************************************************/
51static void test() {
52 std::vector<geometry::grahamscan::Point> points = {
53 {0, 3}, {1, 1}, {2, 2}, {4, 4}, {0, 0}, {1, 2}, {3, 1}, {3, 3}};
54 std::vector<geometry::grahamscan::Point> expected_result = {
55 {0, 3}, {4, 4}, {3, 1}, {0, 0}};
56 std::vector<geometry::grahamscan::Point> derived_result;
57 std::vector<geometry::grahamscan::Point> res;
58
59 derived_result = geometry::grahamscan::convexHull(points, points.size());
60
61 std::cout << "1st test: ";
62 for (int i = 0; i < expected_result.size(); i++) {
63 assert(derived_result[i].x == expected_result[i].x);
64 assert(derived_result[i].y == expected_result[i].y);
65 }
66 std::cout << "passed!" << std::endl;
67}
68
69/*******************************************************************************
70 * @brief Main function
71 * @returns 0 on exit
72 *******************************************************************************/
73int main() {
74 test(); // run self-test implementations
75 return 0;
76}
void test()
int main()
Main function.