-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPointSet.cpp
More file actions
318 lines (265 loc) · 7.11 KB
/
Copy pathPointSet.cpp
File metadata and controls
318 lines (265 loc) · 7.11 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include <tgmath.h>
#include "PointSet.h"
Point PointSet::refPnt = Point();
PointSet::PointSet(): PointSet(DEFAULT_SET_SIZE)
{
}
PointSet::PointSet(int capacity)
{
_points = new Point*[capacity];
_arrSize = capacity;
fillArray(_points);
}
/**
* copy constructor
* @param Original
* @return
*/
PointSet::PointSet(const PointSet& original) : PointSet(original._arrSize)
{
*this = original;
}
PointSet::~PointSet()
{
for (int i = 0; i < _curFilled; i++)
{
/*std::cout << "nums: " << _curFilled << "\n";
std::cout << "at place: " << i <<":" << _points[i]->toString() << "\n";*/
delete(_points[i]);
}
delete[] _points;
_arrSize = 0;
_curFilled = 0;
}
std::string PointSet::toString()
{
std::stringstream pointsStr;
for(int j = 0 ; j < _curFilled; j++)
{
pointsStr << _points[j]->toString()<<"\n";
}
return pointsStr.str();
}
bool PointSet::add(Point const &newPoint)
{
if(isPointInSet(newPoint) == NOT_IN_SET)
{
if (_arrSize - _curFilled == 0)
{
enlargeSet();
}
_points[_curFilled] = new Point(newPoint);
_curFilled++;
return true;
}
return false;
}
bool PointSet::remove(Point const &pointToRemove)
{
int location = isPointInSet(pointToRemove);
if ( location != NOT_IN_SET)
{
delete _points[location]; //TODO: should this be just a destructor?
if( _curFilled < (int)(MINIMIZE_FACTOR * _arrSize))
{
minimizeSet();
}
if (location != _curFilled - 1)
{
_points[location] = _points[_curFilled - 1]; // moving a point from the end to fill the gap
}
_curFilled--;
return true;
}
return false;
}
int PointSet::size() const
{
return _curFilled;
}
bool PointSet::operator!=(const PointSet& other) const
{
return !(operator==(other));
}
bool PointSet::operator==(const PointSet& other) const
{
// number of set members is different, obviously groups are not equal.
if (this->_curFilled != other._curFilled)
{
return false;
}
int match = 0;
for (int i = 0; i < size(); i++)
{
if (other.isPointInSet(*_points[i]))
{
match++;
}
}
return match == _curFilled;
}
PointSet PointSet::operator-(const PointSet& other) const
{
PointSet remainder = subtractOrIntersect(SUBTRACT_GROUP, other);
return remainder;
}
PointSet PointSet::operator&(const PointSet& other) const
{
PointSet intersection = subtractOrIntersect(INTERSECT_GROUP, other);
return intersection;
}
PointSet& PointSet::operator=(PointSet other)
{
if (this == &other)
{
return *this;
} else
{
using std::swap;
swap(this->_curFilled, other._curFilled);
swap(this->_arrSize, other._arrSize);
swap(this->_points, other._points);
}
return *this;
}
int PointSet::isPointInSet(const Point& curPnt) const
{
for(int j = 0; j < size() ; j++)
{
if (curPnt == *_points[j])
{
return j;
}
}
return NOT_IN_SET;
}
PointSet PointSet::subtractOrIntersect(int flag, const PointSet& other) const
{
PointSet returnedGroup = PointSet(_curFilled);
for (int i = 0; i <_curFilled; i++)
{
if (other.isPointInSet(*_points[i]) == NOT_IN_SET)
{
if (flag == SUBTRACT_GROUP)
{
returnedGroup.add(*_points[i]);
}
}
else if (other.isPointInSet(*_points[i]) != NOT_IN_SET)
{
if (flag == INTERSECT_GROUP)
{
returnedGroup.add(*_points[i]);
}
}
}
return returnedGroup;
}
void PointSet::enlargeSet()
{
_arrSize = _arrSize*ENLARGMENT_FACTOR;
Point** enlargedArr = new Point*[_arrSize];
fillArray(enlargedArr);
std::swap_ranges(_points, _points+_curFilled, enlargedArr);
delete[] _points;
_points = enlargedArr;
}
void PointSet::minimizeSet()
{
_arrSize = (int)(_arrSize*MINIMIZE_FACTOR);
Point** miniArr = new Point*[_arrSize];
fillArray(miniArr);
std::swap_ranges(_points, _points+_curFilled, miniArr);
delete[] _points;
_points = miniArr;
}
void PointSet::fillArray(Point** arrToFill)
{
for (int i = 0; i <_arrSize;i++)
{
arrToFill[i] = nullptr;
}
}
PointSet* PointSet::convexSearch()
{
int numOfMembers = _curFilled;
Point** retPnts = new Point*[numOfMembers + 1];
std::copy(_points, _points+numOfMembers, retPnts+1); // leaving the first place empty.
// Finding the point with minimum y-coor value in order to make it the reference point of the
// convex hull
for (int i = 2; i < numOfMembers+1; i++ )
{
if((retPnts[i]->getYVal() < retPnts[1]->getYVal()) || ((retPnts[i]->getYVal() ==
retPnts[1]->getYVal()) && (retPnts[i]->getXVal() < retPnts[1]->getXVal())))
{
std::swap(retPnts[i], retPnts[1]);
}
}
updateRef(*retPnts[1]);
// refPnt = *retPnts[1]; //TODO
//sorting points in increasing angle in relation to our minimum y point
std::sort(retPnts+1, retPnts+numOfMembers+1, angleComp);
retPnts[0] = retPnts[numOfMembers];
int M = 1;
for (int i = 2; i <= numOfMembers; i++)
{
while (ccw(*retPnts[M-1], *retPnts[M], *retPnts[i]) <= 0)
{
if (M > 1)
{
M--;
}
// all points are collinear, this is our breaking condition
else if (i == numOfMembers)
{
break;
} else
{
i++;
}
}
M++;
std::cout<<"adding point:"<<retPnts[i]->toString()<<"\n";
std::swap(retPnts[i], retPnts[M]);
}
// creating the convex hull to be returned and putting the relevant points in it.
PointSet* retSet = new PointSet(M);
delete[] retSet->_points;
retSet->_points = retPnts;
retSet->_curFilled = M;
return retSet;
}
int PointSet::ccw(Point const &pnt1, Point const &pnt2, Point const &pnt3)
{
return (pnt2.getXVal() - pnt1.getXVal()) * (pnt3.getYVal() - pnt1.getYVal()) - (pnt2.getYVal()
- pnt1.getYVal()) * (pnt3.getXVal() - pnt1.getXVal());
}
bool PointSet::angleComp(const Point* a,const Point* b)
{
int direction = ccw(refPnt, *a, *b);
// the points are not collinear, we can determine which has a bigger angle.
if (direction != 0)
{
return direction > 0;
}
// points are collinear, we determine by distance
bool ans = distance(refPnt, *a) < distance(refPnt, *b);
return ans;
}
int PointSet::distance(Point const &a, Point const &b)
{
return std::sqrt(std::pow(b.getXVal()-a.getXVal(), 2) + std::pow(b.getYVal() - a.getYVal(),2));
}
bool PointSet::forPrintComp(const Point* pnt1, const Point* pnt2)
{
return ((pnt1->getXVal() < pnt2->getXVal()) || ((pnt1->getXVal() == pnt2->getXVal()) &&
(pnt1->getYVal() < pnt2->getYVal())));
}
void PointSet::sortForPrint()
{
std::sort(_points, _points + _curFilled, forPrintComp);
}
void PointSet::updateRef(Point &ref)
{
refPnt = ref;
}