Computational Geometry Algorithms Library  1.0
Computational Geometry Algorithms Library Documentation
JarvisMarch.h
Go to the documentation of this file.
1 
2 //
3 // Created by calvrix on 13/02/17.
4 //
5 #ifndef COMPUTGEOALGOS_JARVISMARCH_H
6 #define COMPUTGEOALGOS_JARVISMARCH_H
7 
8 #include <iostream>
9 #include <bits/stdc++.h>
10 #include "origin.h"
11 using namespace std;
12 
13 vector<int> indices_j;
15 void swap(double &p, double &q) {
16  double tray = p;
17  p = q;
18  q = tray;
19 }
20 
22 
24 void swapElements(pair<double, double> &p, pair<double, double> &q) {
25  swap(p.first, q.first);
26  swap(p.second, q.second);
27 }
28 
29 
31 
39 double execJarvisMarch(vector<pair<double, double> > Points) {
40  int len, i, current, min = 0, hullLength = 0;
41  len = int(Points.size());
42  double x_min = Points[0].first; // a **double** value which gets the point with lowest y-coordinate
43  pair<double, double> leftMost;
44  //clock_t j_taken;
45  //j_taken = clock();
46  for (int i = 1; i < len; i++)
47  {
48  if (orderedSort(Points[i], Points[min]))
49  x_min = Points[i].first, min = i;
50  }
51 
52  P0 = leftMost = Points[min]; //P0 denotes the latest point permanently added to the hull
53  indices_j.pb(min);
54  swapElements(Points[hullLength++], Points[min]); //Swap the left-most point with the point in the set with index 0
55  /*
56  * Form the Hull by processing the remaining points
57  */
58  vector<pair<double , double> > convex_hull; // a **vector data structure** for storing the convex hull points
59  do {
60  convex_hull.pb(P0);
61  for (i = hullLength, current = i; i < len; i++) {
62  //Find the next point by comparing angles made with the last edge on the hull
63  if (orderByPolar(Points[i], Points[current])) {
64  current = i;
65  }
66  }
67  //Since the above check starts from an index = hullLength, check if the start of the hull is the next point
68  if (orderByPolar(Points[0], Points[current])) {
69  current = 0;
70  }
71  P0 = Points[current];
72  swapElements(Points[hullLength++], Points[current]); //Swap the latest added point with the point in the set having index equal to the hull length
73  } while (P0 != leftMost);
74  ofstream out_file;
75  out_file.open("output.ch");
76  out_file << "CH\n";
77  out_file << len << " " << convex_hull.size() << "\n";
78  for (int i = 0; i < len; i++) {
79  out_file << Points[i].first << " " << Points[i].second << " 0\n";
80  }
81  for (i = 0; i < convex_hull.size(); i++) {
82  out_file << ((i + 1)%len) << " ";
83  }
84  printVectorData(hullLength-1,convex_hull,"");
85  //j_taken = clock()-j_taken;
86  return 0;
87 }
88 #endif
bool orderByPolar(pair< double, double > &p1, pair< double, double > &p2)
Function to order with respect to polar Coordinates.
Definition: origin.h:87
void swap(double &p, double &q)
Generic Swap Function.
Definition: JarvisMarch.h:15
vector< int > indices_j
Definition: JarvisMarch.h:13
pair< double,double > P0
a double value which stores the centre about which polar ordering is to be done
Definition: origin.h:80
void swapElements(pair< double, double > &p, pair< double, double > &q)
Point Swap Function.
Definition: JarvisMarch.h:24
vector< pair< double, double > > convex_hull(vector< pair< double, double > > points)
Upper Hull and Lower Hull Algorithm
Definition: Andrews.h:14
void printVectorData(int len, vector< pair< double, double > > v, string s)
Print Function.
Definition: origin.h:103
double execJarvisMarch(vector< pair< double, double > > Points)
Jarvis March Algorithm Implementation
Definition: JarvisMarch.h:39
bool orderedSort(pair< double, double > &f, pair< double, double > &s)
Comparator Function.
Definition: origin.h:52