0%

洛谷 P4196 [CQOI2006]凸多边形 (半平面交)

题目链接:P4196 [CQOI2006]凸多边形

题意

给定 $n$ 个凸多边形,求它们相交的面积。

思路

半平面交

半平面交的模板题。

代码

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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
const db eps = 1e-10;
const db pi = acos(-1.0);
const ll inf = 0x3f3f3f3f3f3f3f3f;
const ll maxn = 1e3 + 10;

inline int dcmp(db x) {
if(fabs(x) < eps) return 0;
return x > 0? 1: -1;
}

class Point {
public:
double x, y;
Point(double x = 0, double y = 0) : x(x), y(y) {}
inline void input() {
scanf("%lf%lf", &x, &y);
}
bool operator<(const Point &a) const {
return (!dcmp(x - a.x))? dcmp(y - a.y) < 0: x < a.x;
}
bool operator==(const Point &a) const {
return dcmp(x - a.x) == 0 && dcmp(y - a.y) == 0;
}
db dis2(const Point a) {
return pow(x - a.x, 2) + pow(y - a.y, 2);
}
db dis(const Point a) {
return sqrt(dis2(a));
}

db dis2() {
return x * x + y * y;
}
db dis() {
return sqrt(dis2());
}
Point operator+(const Point a) {
return Point(x + a.x, y + a.y);
}
Point operator-(const Point a) {
return Point(x - a.x, y - a.y);
}
Point operator*(double p) {
return Point(x * p, y * p);
}
Point operator/(double p) {
return Point(x / p, y / p);
}
db dot(const Point a) {
return x * a.x + y * a.y;
}
db cross(const Point a) {
return x * a.y - y * a.x;
}
db ang(Point a) {
return acos((a.dis() * dis()) / dot(a));
}
};
typedef Point Vector;

Point p[maxn], ip[maxn];

class Line {
public:
Point s, e;
db angle;
Line() {}
Line(Point s, Point e) : s(s), e(e) {}
inline void input() {
s.input();e.input();
}
bool operator<(const Line &a) const {
Line l = a;
if(dcmp(angle - l.angle) == 0) {
return l.toLeftTest(s) == 1;
}
return angle < l.angle;
}
void get_angle() {
angle = atan2(e.y - s.y, e.x - s.x);
}
int toLeftTest(Point p) {
if((e - s).cross(p - s) > 0) return 1;
else if((e - s).cross(p - s) < 0) return -1;
return 0;
}
int linecrossline(Line l) {
if(dcmp((e - s).cross(l.e - l.s)) == 0) {
if(dcmp((l.s - e).cross(l.e - s)) == 0) {
return 0;
}
return 1;
}
return 2;
}
Point crosspoint(Line l) {
db a1 = (l.e - l.s).cross(s - l.s);
db a2 = (l.e - l.s).cross(e - l.s);
db x = (s.x * a2 - e.x * a1) / (a2 - a1);
db y = (s.y * a2 - e.y * a1) / (a2 - a1);
if(dcmp(x) == 0) x = 0;
if(dcmp(y) == 0) y = 0;
return Point(x, y);
}
};

Line l[maxn], q[maxn];

db half_plane(int cnt) {
sort(l + 1, l + 1 + cnt);
// for(int i = 1; i <= cnt; ++i) {
// cout << l[i].s.x << " " << l[i].s.y << " ";
// cout << l[i].e.x << " " << l[i].e.y << endl;
// }
int tmp = 1;
for(int i = 2; i <= cnt; ++i) {
if(dcmp(l[i].angle - l[tmp].angle) == 1) l[++tmp] = l[i];
}
cnt = tmp;
int head = 1, tail = 2;
q[1] = l[1], q[2] = l[2];
for(int i = 3; i <= cnt; ++i) {
while(head < tail && l[i].toLeftTest(q[tail].crosspoint(q[tail - 1])) == -1) {
--tail;
}
while(head < tail && l[i].toLeftTest(q[head].crosspoint(q[head + 1])) == -1) {
++head;
}
q[++tail] = l[i];
}

while(head < tail && q[head].toLeftTest(q[tail].crosspoint(q[tail - 1])) == -1) {
--tail;
}
while(head < tail && q[tail].toLeftTest(q[head].crosspoint(q[head + 1])) == -1) {
++head;
}

if(tail - head + 1 <= 2) {
return 0.0;
}

tmp = 0;
for(int i = head; i < tail; ++i) {
ip[++tmp] = q[i].crosspoint(q[i + 1]);
}
ip[++tmp] = q[head].crosspoint(q[tail]);
db ans = 0;
for(int i = 3; i <= tmp; ++i) {
ans += (ip[i - 1] - ip[1]).cross(ip[i] - ip[1]);
}
return ans * 0.5;
}

int main() {
int n;
scanf("%d", &n);
int cnt = 0;
for(int i = 1; i <= n; ++i) {
int m;
scanf("%d", &m);
for(int i = 0; i < m; ++i) {
p[i].input();
if(i) {
l[++cnt].e = p[i];
l[cnt].s = p[i - 1];
l[cnt].get_angle();
}
}
l[++cnt].e = p[0];
l[cnt].s = p[m - 1];
l[cnt].get_angle();
}
printf("%.3lf\n", half_plane(cnt));
return 0;
}

欢迎关注我的其它发布渠道