-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjarvis_march.hpp
78 lines (60 loc) · 1.7 KB
/
jarvis_march.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#ifndef jarvis_march_hpp
#define jarvis_march_hpp
#include <vector>
#include <string>
#include <utility>
#include "convex_hull_base.hpp"
#include "point.hpp"
#include "vector2d.hpp"
namespace csce {
template<typename T>
class jarvis_march : public csce::convex_hull_base<T> {
public:
jarvis_march(int _nthreads) : convex_hull_base<T>(_nthreads){}
std::string name() const {
return "Jarvis' March";
}
virtual std::vector<csce::point<T>> compute_hull(std::vector<csce::point<T>>& points) {
return this->performShortestPathCalculation(points);
}
~jarvis_march() {}
private:
std::vector<csce::point<T>> performShortestPathCalculation(const std::vector<csce::point<T>>& points) const {
std::vector<csce::point<T>> resultsOfShortestPath;
int topMostPoint = 0;
for(int i = 0; i < points.size(); i++){
if(points[i].y > points[topMostPoint].y){
topMostPoint = i;
}
}
int tempPoint1 = topMostPoint;
int tempPoint2 = 0;
int orientationValue = 0;
do{
tempPoint2 = (tempPoint1 + 1) % points.size();
for(int i = 0; i < points.size(); i++){
orientationValue = operation(points[tempPoint1],points[i],points[tempPoint2]);
if(orientationValue == 2){
tempPoint2 = i;
}
}
resultsOfShortestPath.push_back(points[tempPoint2]);
tempPoint1 = tempPoint2;
}
while(tempPoint1 != topMostPoint);
return resultsOfShortestPath;
}
int operation(csce::point<T> d, csce::point<T> e, csce::point<T> f) const {
csce::vector2d<T> a(e,d);
csce::vector2d<T> b(e,f);
bool is_counterclockwise = a.ccw(b);
if(is_counterclockwise == true){
return 2;
}
else {
return 0;
}
}
};
}
#endif