-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconvex_hull.h
136 lines (130 loc) · 3.52 KB
/
convex_hull.h
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
#ifndef CONVEX_HULL_H_INCLUDED
#define CONVEX_HULL_H_INCLUDED
#include <iostream>
#include <fstream>
#include <limits.h>
using namespace std;
struct TPoint
{
int x, y;
friend ostream& operator << (ostream& os, const TPoint& p);
bool operator < (const TPoint& p)const
{
if ( x<p.x )
return true;
else if (x==p.x )
if (y<p.y)
return true;
return false;
}
};
struct GreaterPoint : binary_function<bool, TPoint, TPoint>
{
bool operator () (const TPoint& p1, const TPoint& p2)const
{
if ( p1.x>p2.x )
return true;
else if (p1.x==p2.x )
if (p1.y>p2.y)
return true;
return false;
}
};
ostream& operator << (ostream& os, const TPoint& p)
{
os << "(" << p.x << ", " << p.y << ") ";
return os;
}
template <typename T>
struct TypeTrait
{
typedef const T& ParamType;
};
template <typename T>
struct TypeTrait<T*>
{
typedef const T& ParamType;
};
//template <class PointType>
//void PrintPoint(typename TypeTrait<PointType>::ParamType p)
void PrintPoint(const TPoint& p)
{
std::cout << p;
return;
}
void PrintPointPtr(TPoint* p)
{
std::cout << *p;
return;
}
typedef pair<TPoint,TPoint> PointPair;
void PrintPointPair(const PointPair& p)
{
const TPoint& p1 = p.first;
const TPoint& p2 = p.second;
std::cout << "(" << p1.x << ", " << p1.y << ")-"
<< "(" << p2.x << ", " << p2.y << ") \n";
}
int CountPmax(const TPoint& p1, const TPoint& p2, const TPoint& p3)
{
return (p1.x*p2.y + p3.x*p1.y + p2.x*p3.y - p3.x*p2.y - p2.x*p1.y - p1.x*p3.y);
}
typedef vector<TPoint*> VecPointPtr;
typedef pair<TPoint*,TPoint*> PointPairPtr;
void CountUppack(const PointPairPtr& pp, const VecPointPtr& points, \
VecPointPtr& selPoints)
{
if (points.empty())
return;
VecPointPtr points1, points2;
TPoint* pointMax;
int max = INT_MIN;
for (int i=0; i<points.size(); i++)
{
int temp = CountPmax(*(pp.first), *(pp.second), *points[i]);
if (temp>max)
{
max = temp;
pointMax = points[i];
}
}
selPoints.push_back(pointMax);
for (int i=0; i<points.size(); i++)
{
if (CountPmax(*(pp.first), *pointMax, *points[i])>0)
points1.push_back(points[i]);
if (CountPmax(*pointMax, *(pp.second), *points[i])>0)
points2.push_back(points[i]);
}
CountUppack(PointPairPtr(pp.first, pointMax), points1, selPoints);
CountUppack(PointPairPtr(pointMax, pp.second), points2, selPoints);
}
void CountDownpack(const PointPairPtr& pp, const VecPointPtr& points, \
VecPointPtr& selPoints)
{
if (points.empty())
return;
VecPointPtr points1, points2;
TPoint* pointMax;
int min = INT_MAX;
for (int i=0; i<points.size(); i++)
{
int temp = CountPmax(*(pp.first), *(pp.second), *points[i]);
if (temp<min)
{
min = temp;
pointMax = points[i];
}
}
selPoints.push_back(pointMax);
for (int i=0; i<points.size(); i++)
{
if (CountPmax(*(pp.first), *pointMax, *points[i])<0)
points1.push_back(points[i]);
if (CountPmax(*pointMax, *(pp.second), *points[i])<0)
points2.push_back(points[i]);
}
CountDownpack(PointPairPtr(pp.first, pointMax), points1, selPoints);
CountDownpack(PointPairPtr(pointMax, pp.second), points2, selPoints);
}
#endif // CONVEX_HULL_H_INCLUDED