@@ -17,56 +17,57 @@ typedef Point<double> P;
17
17
ostream &operator <<(ostream &os, P p) { return cout << " (" << p.x << " ," << p.y << " )" ; }
18
18
19
19
namespace sjtu {
20
- const double EPS = 1e-8 ;
21
- inline int sign (double a) { return a < -EPS ? -1 : a > EPS; }
20
+ typedef double T;
21
+ const T EPS = 1e-8 ;
22
+ inline int sign (T a) { return a < -EPS ? -1 : a > EPS; }
22
23
struct Point {
23
- double x, y;
24
+ T x, y;
24
25
Point () {}
25
- Point (double _x, double _y) : x(_x), y(_y) {}
26
+ Point (T _x, T _y) : x(_x), y(_y) {}
26
27
Point operator +(const Point &p) const { return Point (x + p.x , y + p.y ); }
27
28
Point operator -(const Point &p) const { return Point (x - p.x , y - p.y ); }
28
- Point operator *(double d) const { return Point (x * d, y * d); }
29
- Point operator /(double d) const { return Point (x / d, y / d); }
29
+ Point operator *(T d) const { return Point (x * d, y * d); }
30
+ Point operator /(T d) const { return Point (x / d, y / d); }
30
31
bool operator <(const Point &p) const {
31
32
int c = sign (x - p.x );
32
33
if (c)
33
34
return c == -1 ;
34
35
return sign (y - p.y ) == -1 ;
35
36
}
36
- double dot (const Point &p) const { return x * p.x + y * p.y ; }
37
- double det (const Point &p) const { return x * p.y - y * p.x ; }
38
- double alpha () const { return atan2 (y, x); }
39
- double distTo (const Point &p) const {
40
- double dx = x - p.x , dy = y - p.y ;
37
+ T dot (const Point &p) const { return x * p.x + y * p.y ; }
38
+ T det (const Point &p) const { return x * p.y - y * p.x ; }
39
+ T alpha () const { return atan2 (y, x); }
40
+ T distTo (const Point &p) const {
41
+ T dx = x - p.x , dy = y - p.y ;
41
42
return hypot (dx, dy);
42
43
}
43
- double alphaTo (const Point &p) const {
44
- double dx = x - p.x , dy = y - p.y ;
44
+ T alphaTo (const Point &p) const {
45
+ T dx = x - p.x , dy = y - p.y ;
45
46
return atan2 (dy, dx);
46
47
}
47
48
// clockwise
48
- Point rotAlpha (const double &alpha, const Point &o = Point(0 , 0 )) const {
49
- double nx = cos (alpha) * (x - o.x ) + sin (alpha) * (y - o.y );
50
- double ny = -sin (alpha) * (x - o.x ) + cos (alpha) * (y - o.y );
49
+ Point rotAlpha (const T &alpha, const Point &o = Point(0 , 0 )) const {
50
+ T nx = cos (alpha) * (x - o.x ) + sin (alpha) * (y - o.y );
51
+ T ny = -sin (alpha) * (x - o.x ) + cos (alpha) * (y - o.y );
51
52
return Point (nx, ny) + o;
52
53
}
53
54
Point rot90 () const { return Point (-y, x); }
54
55
Point unit () { return *this / abs (); }
55
56
void read () { scanf (" %lf%lf" , &x, &y); }
56
- double abs () { return hypot (x, y); }
57
- double abs2 () { return x * x + y * y; }
57
+ T abs () { return hypot (x, y); }
58
+ T abs2 () { return x * x + y * y; }
58
59
void write () { cout << " (" << x << " ," << y << " )" << endl; }
59
60
};
60
61
#define cross (p1, p2, p3 ) ((p2.x - p1.x) * (p3.y - p1.y) - (p3.x - p1.x) * (p2.y - p1.y))
61
62
#define crossOp (p1, p2, p3 ) sign(cross(p1, p2, p3))
62
63
63
64
Point isSS (Point p1, Point p2, Point q1, Point q2) {
64
- double a1 = cross (q1, q2, p1), a2 = -cross (q1, q2, p2);
65
+ T a1 = cross (q1, q2, p1), a2 = -cross (q1, q2, p2);
65
66
return (p1 * a2 + p2 * a1) / (a1 + a2);
66
67
}
67
68
struct Border {
68
69
Point p1, p2;
69
- double alpha;
70
+ T alpha;
70
71
void setAlpha () { alpha = atan2 (p2.y - p1.y , p2.x - p1.x ); }
71
72
void read () {
72
73
p1.read ();
@@ -88,7 +89,7 @@ bool operator<(const Border &a, const Border &b) {
88
89
89
90
bool operator ==(const Border &a, const Border &b) { return sign (a.alpha - b.alpha ) == 0 ; }
90
91
91
- void add (double x, double y, double nx, double ny) {
92
+ void add (T x, T y, T nx, T ny) {
92
93
border[n].p1 = Point (x, y);
93
94
border[n].p2 = Point (nx, ny);
94
95
border[n].setAlpha ();
@@ -123,7 +124,7 @@ void convexIntersection() {
123
124
++qh;
124
125
}
125
126
126
- double calcArea () {
127
+ T calcArea () {
127
128
static Point ps[MAX_N_BORDER];
128
129
int cnt = 0 ;
129
130
@@ -136,7 +137,7 @@ double calcArea() {
136
137
ps[cnt++] = isBorder (que[i], que[next]);
137
138
}
138
139
139
- double area = 0 ;
140
+ T area = 0 ;
140
141
for (int i = 0 ; i < cnt; ++i) {
141
142
area += ps[i].det (ps[(i + 1 ) % cnt]);
142
143
}
@@ -145,7 +146,7 @@ double calcArea() {
145
146
return area;
146
147
}
147
148
148
- double halfPlaneIntersection (vector<Line> cur) {
149
+ T halfPlaneIntersection (vector<Line> cur) {
149
150
n = 0 ;
150
151
for (auto i: cur)
151
152
add (i[0 ].x , i[0 ].y , i[1 ].x , i[1 ].y );
@@ -202,7 +203,7 @@ void testEmpty() {
202
203
assert (sz (res) == 0 );
203
204
}
204
205
void testRandom () {
205
- int lim = 10 ;
206
+ int lim = 1e2 ;
206
207
for (int i = 0 ; i < 1000 ; i++) {
207
208
vector<Line> t;
208
209
for (int i = 0 ; i < 6 ; i++) {
@@ -227,6 +228,9 @@ void testRandom() {
227
228
assert (diff < EPS);
228
229
}
229
230
}
231
+ void testConvex () {
232
+
233
+ }
230
234
231
235
int main () {
232
236
test1 ();
@@ -235,7 +239,7 @@ int main() {
235
239
testPoint ();
236
240
testEmpty ();
237
241
testRandom ();
238
- // Failure case for MIT
242
+ // Failure case for MIT's half plane code
239
243
// vector<Line> t({{P(9, 8), P(9, 1)}, {P(3, 3), P(3, 5)}, {P(7, 6), P(0, 8)}});
240
244
// addInf(t);
241
245
// cout << polygonArea2(halfPlaneIntersection(t)) << endl;
0 commit comments