-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathConvexHullJarvisMarch.cpp
More file actions
75 lines (62 loc) · 1.58 KB
/
Copy pathConvexHullJarvisMarch.cpp
File metadata and controls
75 lines (62 loc) · 1.58 KB
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
#include "ConvexHullJarvisMarch.h"
#include <iostream>
using namespace std;
Number orientation(SimplePoint2D p, SimplePoint2D q, SimplePoint2D r)
{
Number turn = (q.y - p.y) * (r.x -q.x) - (q.x - p.x) * (r.y - q.y);
return turn;
}
Region2D ConvexHullJarvisMarch(Point2D pointset){
vector<SimplePoint2D> points;
for(Point2D::Iterator ptr = pointset.begin(); ptr != pointset.end(); ptr++)
{
points.push_back(*ptr);
}
if(pointset.count() < 3)
{
return Region2D();
}
vector<SimplePoint2D> hull;
int left = 0;
for(int i = 1; i < points.size(); i++)
{
if(points[i].x <= points[left].x)
{
left = i;
}
}
int p = left;
int q = (p + 1) % points.size();
int finish = 0;
while(finish < 1)
{
hull.push_back(points[p]);
for(int j = 0; j < points.size(); j++)
{
if(orientation(points[p], points[j], points[q]) > Number("0"))
{
q = j;
}
}
p = q;
if(p == left)
{
finish++;
}
q = (p + 1) % points.size();
}
for(int k=0; k<hull.size(); k++){
cout<<"("<<hull[k].x<<", "<<hull[k].y<<")"<<endl;
}
vector<Segment2D> hullSegments;
for(int n=0; n<hull.size(); n++){
if(n == hull.size()-1){
hullSegments.push_back(Segment2D(hull[n],hull[0]));
}
else
{
hullSegments.push_back(Segment2D(hull[n],hull[n+1]));
}
}
return Region2D(hullSegments);
}