24
24
// -------------------------------------------------------------------
25
25
// -------------------------------------------------------------------
26
26
27
+ #ifndef TWODCONVEXHULL
28
+ #define TWODCONVEXHULL 1
29
+
27
30
#include < iostream>
28
31
#include < stack>
29
32
#include < vector>
30
33
#include < stdlib.h>
31
34
32
35
#include " vec3.h"
33
36
34
- #ifndef TWODCONVEXHULL
35
- #define TWODCONVEXHULL 1
36
-
37
- using std::vector;
38
- using std::stack;
39
-
40
37
struct Point
41
38
{
42
39
double x, y;
43
40
int index;
44
41
};
45
-
42
+
46
43
// A utility function to find next to top in a stack
47
- Point nextToTop (stack<Point > &S)
44
+ Point nextToTop (std:: stack<Point > &S)
48
45
{
49
46
Point p = S.top ();
50
47
S.pop ();
51
48
Point res = S.top ();
52
49
S.push (p);
53
50
return res;
54
51
}
55
-
52
+
56
53
void swap (Point &p1, Point &p2)
57
54
{
58
55
Point temp = p1;
59
56
p1 = p2;
60
57
p2 = temp;
61
58
}
62
-
59
+
63
60
double distSq (const Point & p1, const Point & p2)
64
61
{
65
62
return (p1.x - p2.x ) * (p1.x - p2.x ) +
@@ -101,8 +98,9 @@ int compare(const void *vp1, const void *vp2)
101
98
102
99
// Prints convex hull of a set of n points.
103
100
// TODO: points gets overwritten...
104
- stack<Point > convexHull (Point points[], const int n )
101
+ std:: stack<Point > convexHull (std::vector< Point >& points)
105
102
{
103
+ const int n = points.size ();
106
104
// Find the bottommost point
107
105
int ymin = points[0 ].y , min = 0 ;
108
106
for (int i = 1 ; i < n; i++)
@@ -116,7 +114,7 @@ stack<Point> convexHull(Point points[], const int n)
116
114
}
117
115
118
116
// Place the bottom-most point at first position
119
- swap (points[0 ], points[min]);
117
+ std:: swap (points[0 ], points[min]);
120
118
121
119
// Sort n-1 points with respect to the first point.
122
120
Point p0;
@@ -143,7 +141,7 @@ stack<Point> convexHull(Point points[], const int n)
143
141
m++; // Update size of modified array
144
142
}
145
143
146
- stack<Point > S;
144
+ std:: stack<Point > S;
147
145
if (m < 3 ) return S;
148
146
149
147
S.push (points[0 ]);
@@ -165,9 +163,9 @@ stack<Point> convexHull(Point points[], const int n)
165
163
}
166
164
167
165
// dim refers to the dimension that needs to be ignored
168
- vector<int > getConvexHull (const vector<vec3> &inPoints, const int dim)
166
+ std:: vector<int > getConvexHull (const std:: vector<vec3> &inPoints, const int dim)
169
167
{
170
- Point * myPoints = new Point [ inPoints.size ()] ;
168
+ std::vector< Point > myPoints ( inPoints.size ()) ;
171
169
for (int i = 0 ; i < inPoints.size (); i++)
172
170
{
173
171
int index = 0 ;
@@ -182,19 +180,17 @@ vector<int> getConvexHull(const vector<vec3> &inPoints, const int dim)
182
180
}
183
181
184
182
// TODO: myPoints gets overwritten...
185
- stack< Point > myStack = convexHull (myPoints, inPoints. size () );
183
+ auto myStack = convexHull (myPoints);
186
184
187
- vector<int > ret;
185
+ std:: vector<int > ret;
188
186
while (! myStack.empty () )
189
187
{
190
188
Point p = myStack.top ();
191
189
ret.push_back (p.index );
192
190
myStack.pop ();
193
191
}
194
- delete [] myPoints;
195
192
196
193
return ret;
197
194
}
198
195
199
196
#endif
200
-
0 commit comments