Computational Geometry Algorithms Library  1.0
Computational Geometry Algorithms Library Documentation
Andrews.h
Go to the documentation of this file.
1 //
2 // Created by shuttle3468 on 8/2/17.
3 //
4 #include "origin.h"
5 
7 
14 vector<pair<double,double > > convex_hull(vector<pair<double, double> > points)
15 {
16  int n = points.size();
17  int counter = 0;
18  vector<pair<double, double> > hull;
19 
20  for (int i = 0; i < n; ++i) {
21  while (counter >= 2 && orientation(hull[counter-2], hull[counter-1], points[i]) != CLOCKWISE)
22  {
23  cout << "popping out\n";
24  counter--;
25  hull.pop_back();
26  indices.pop_back();
27  }
28  hull.pb(points[i]);
29  counter++;
30  indices.pb(i);
31  }
32  cout << "-------------------COUNTER VALUE AFTER LOWER HULL:" << counter <<endl;
33  // Build upper hull
34  for (int i = n-2, t = counter+1; i >= 0; i--) {
35  while (counter >= t && orientation(hull[counter-2], hull[counter-1], points[i]) != CLOCKWISE)
36  {
37  cout << "popping out\n";
38  counter--;
39  hull.pop_back();
40  indices.pop_back();
41  }
42  hull.pb(points[i]);
43  counter++;
44  indices.pb(i);
45  }
46  cout << "-------------------COUNTER VALUE AFTER UPPER HULL:" << counter <<endl;
47  hull.resize(counter-1);
48  return hull;
49 }
50 
52 
60 set<pair<double, double> > execAndrews(vector<pair<double, double> > Points) {
61  cout << "Executing Andrews Algorithm\n---\n";
62  vector<pair<double, double> > tentative_hull;
63  vector<pair<double, double> > L_upper,L_lower;
64  int len;
65  len = int(Points.size());
66  /*
67  * Sort the Points By x-coordinate
68  */
69  sort(Points.begin(),Points.end(),orderedSort);
70 
71  tentative_hull = convex_hull(Points);
72  /*
73  * Final Set of Convex Points
74  */
75  set<pair<double ,double> > f_convex_hull;
76  int LU_len = tentative_hull.size();
77  //int LL_len = L_lower.size();
78  ofstream out_file;
79  out_file.open("output.ch");
80  out_file << "CH\n";
81  out_file << len << " " << LU_len << "\n";
82  cout << "Elements in convex hull:" << LU_len <<endl;
83  for(int i=0;i < len;i++) {
84  out_file << Points[i].first << " " << Points[i].second << " 0\n";
85  }
86  for(int i=0;i<LU_len;i++) {
87  f_convex_hull.insert(tentative_hull[i]);
88  }
89  indices.erase(indices.begin());
90  indices.pop_back();
91  vector<int>::iterator it_set;
92  for(it_set = indices.begin(); it_set!=indices.end(); it_set++) {
93  out_file << *it_set << " ";
94  }
95  out_file << *(indices.end());
96  out_file.close();
97  return f_convex_hull;
98 }
99 
set< pair< double, double > > execAndrews(vector< pair< double, double > > Points)
Complete Execution of Andrews Monotone Chain
Definition: Andrews.h:60
int orientation(pair< double, double > a, pair< double, double > b, pair< double, double > c)
Orientation.
Definition: origin.h:33
vector< int > indices
Definition: origin.h:19
vector< pair< double, double > > convex_hull(vector< pair< double, double > > points)
Upper Hull and Lower Hull Algorithm
Definition: Andrews.h:14
#define CLOCKWISE
Definition: origin.h:16
bool orderedSort(pair< double, double > &f, pair< double, double > &s)
Comparator Function.
Definition: origin.h:52