Computational Geometry Algorithms Library  1.0
Computational Geometry Algorithms Library Documentation
Functions
GrahamScans.h File Reference
#include <iostream>
#include <bits/stdc++.h>
#include "origin.h"
Include dependency graph for GrahamScans.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

pair< double, double > next_to_top (stack< pair< double, double > > S)
 
double execGrahamScans (vector< pair< double, double > > Points)
 Graham Scans Algorithm Implementation More...
 

Function Documentation

double execGrahamScans ( vector< pair< double, double > >  Points)

Graham Scans Algorithm Implementation

  1. First Step is to sort points with respect to the polar coordinates.
  2. After getting the closed path, the next step is to get all points in the path and remove concave points on this.
  3. We accept and reject based on orientation of 3 points selected.
26  {
27  //clock_t time_taken;
28  //time_taken = clock();
29  int len, i, min = 0;
30  len = int(Points.size());
31  double y_min = Points[0].second; // a **double** value which gets the point with lowest y-coordinate
32  pair<double, double> temp;
33  for (int i = 1; i < len; i++)
34  {
35  double y = Points[i].second;
36  if ((y < y_min) || (y_min == y &&
37  Points[i].first < Points[min].first))
38  y_min = Points[i].second, min = i;
39  }
40 
41  temp = Points[min];
42  Points[min] = Points[0];
43  Points[0] = temp;
44  ofstream out_file;
45  out_file.open("output.ch");
46  out_file << "CH\n";
47  out_file << len << " ";
48  //printVectorData(len,Points, "Get Values after minimum y-coordinate\n");
49  P0 = Points[0]; // P0 denotes Central Point for Comparision
50  Points.erase(Points.begin());
51  sort(Points.begin(), Points.end(), orderByPolar);
52 
53  len = int(Points.size());
54  //printVectorData(len,Points, "Ordered by Polar Angles");
55  stack<pair<double , double > > convex_hull; // A **stack data structure** which stores the coordinates. Form the Hull by processing the remaining points
56  stack<int> indices_g;
57  convex_hull.push(P0);
58  convex_hull.push(Points[0]);
59  convex_hull.push(Points[1]);
60  indices_g.push(0);
61  indices_g.push(1);
62  indices_g.push(2);
63  for (i = 2; i < len; i++) {
64  while (orientation(next_to_top(convex_hull), convex_hull.top(), Points[i]) != 3) {
65  if (convex_hull.size() < 3) { break;}
66  convex_hull.pop();
67  indices_g.pop();
68  }
69  convex_hull.push(Points[i]);
70  indices_g.push(i + 1);
71  }
72  cout << convex_hull.size() << endl;
73  out_file << convex_hull.size() << "\n";
74  out_file << P0.first << " " << P0.second << endl;
75  for(int i=0;i < len;i++) {
76  out_file << Points[i].first << " " << Points[i].second << " 0\n";
77  }
78  while (!indices_g.empty()) {
79  out_file << indices_g.top() << " ";
80  indices_g.pop();
81  }
82  while (!convex_hull.empty())
83  {
84  pair<double , double > point = convex_hull.top();
85  cout << point.first << " " << point.second << endl;
86  convex_hull.pop();
87  }
88  //time_taken = clock() - time_taken;
89  out_file.close();
90  return 0;
91 }
pair< double, double > next_to_top(stack< pair< double, double > > S)
Definition: GrahamScans.h:12
bool orderByPolar(pair< double, double > &p1, pair< double, double > &p2)
Function to order with respect to polar Coordinates.
Definition: origin.h:87
int orientation(pair< double, double > a, pair< double, double > b, pair< double, double > c)
Orientation.
Definition: origin.h:33
pair< double,double > P0
a double value which stores the centre about which polar ordering is to be done
Definition: origin.h:80
vector< pair< double, double > > convex_hull(vector< pair< double, double > > points)
Upper Hull and Lower Hull Algorithm
Definition: Andrews.h:14
pair<double , double > next_to_top ( stack< pair< double, double > >  S)
12  {
13  pair<double , double > point = S.top();
14  S.pop();
15  pair<double , double > res = S.top();
16  S.push(point);
17  return res;
18 }