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

Go to the source code of this file.

Functions

void swap (double &p, double &q)
 Generic Swap Function. More...
 
void swapElements (pair< double, double > &p, pair< double, double > &q)
 Point Swap Function. More...
 
double execJarvisMarch (vector< pair< double, double > > Points)
 Jarvis March Algorithm Implementation More...
 

Variables

vector< int > indices_j
 

Function Documentation

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

Jarvis March Algorithm Implementation

  1. First Step is to sort points with respect to their x coordinates.
  2. Start from the left-most point.
  3. Choose and add the point that makes the largest obtuse interior angle with the latest edge on the hull.
  4. Swap the point with the corresponding nth point in the point set to avoid repeating already chosen points
39  {
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 }
bool orderByPolar(pair< double, double > &p1, pair< double, double > &p2)
Function to order with respect to polar Coordinates.
Definition: origin.h:87
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
bool orderedSort(pair< double, double > &f, pair< double, double > &s)
Comparator Function.
Definition: origin.h:52
void swap ( double &  p,
double &  q 
)

Generic Swap Function.

15  {
16  double tray = p;
17  p = q;
18  q = tray;
19 }
void swapElements ( pair< double, double > &  p,
pair< double, double > &  q 
)

Point Swap Function.

Swaps the coordinates of the points passed. Used for efficiency by reducing number of checks

24  {
25  swap(p.first, q.first);
26  swap(p.second, q.second);
27 }
void swap(double &p, double &q)
Generic Swap Function.
Definition: JarvisMarch.h:15

Variable Documentation

vector<int> indices_j