Computational Geometry Algorithms Library  1.0
Computational Geometry Algorithms Library Documentation
Functions
Andrews.h File Reference
#include "origin.h"
Include dependency graph for Andrews.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

vector< pair< double, double > > convex_hull (vector< pair< double, double > > points)
 Upper Hull and Lower Hull Algorithm More...
 
set< pair< double, double > > execAndrews (vector< pair< double, double > > Points)
 Complete Execution of Andrews Monotone Chain More...
 

Function Documentation

vector<pair<double,double > > convex_hull ( vector< pair< double, double > >  points)

Upper Hull and Lower Hull Algorithm

  1. In case of upper hull formation we move from last element to the first and vice versa for lower hull.
  2. Now we remove those points that are either anti-clockwise or collinear because being anticlockwise we are actually using an interior point in such cases.
  3. After forming the hull remove the last point of each list (it's the same as the first point of the other list)
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 }
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
#define CLOCKWISE
Definition: origin.h:16
set<pair<double, double> > execAndrews ( vector< pair< double, double > >  Points)

Complete Execution of Andrews Monotone Chain

  1. Sort the array by their x-coordinate. If that's equal then compare y-coordinate this is compared in origin.h header file.
  2. The next part involves getting upper and lower convex hull
  3. The final set that is formed is just to remove any existing duplicate points in the vector
60  {
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 }
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
bool orderedSort(pair< double, double > &f, pair< double, double > &s)
Comparator Function.
Definition: origin.h:52