Computational Geometry Algorithms Library  1.0
Computational Geometry Algorithms Library Documentation
GrahamScans.h
Go to the documentation of this file.
1 //
2 // Created by shuttle3468 on 7/2/17.
3 //
4 #ifndef COMPUTGEOALGOS_GRAHAMSCANS_H
5 #define COMPUTGEOALGOS_GRAHAMSCANS_H
6 
7 #include <iostream>
8 #include <bits/stdc++.h>
9 #include "origin.h"
10 
11 using namespace std;
12 pair<double , double > next_to_top(stack<pair<double , double > > S) {
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 }
20 
26 double execGrahamScans(vector<pair<double, double> > Points) {
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 }
92 #endif
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
double execGrahamScans(vector< pair< double, double > > Points)
Graham Scans Algorithm Implementation
Definition: GrahamScans.h:26
vector< pair< double, double > > convex_hull(vector< pair< double, double > > points)
Upper Hull and Lower Hull Algorithm
Definition: Andrews.h:14