HDU5130 (Signal Interference)[计算几何,圆交多边形]

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5130

首先考虑下干扰的关系,设A(x1,y1),B(x2,y2),P(x,y),可以列出式子:
sqrt((x-x2)^2 + (y-y2)^2) = k * sqrt((x-x1)^2 + (y-y1)^2)
化简后可以得到一个圆的方程,然后就转化为圆交多边形模板题了

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
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const double eps=1e-8;
const double inf=1e20;
const int maxp=509;
const double pi=acos(-1.0);
int sgn(double x) {
if(fabs(x)<eps) return 0;
if(x<0) return -1;
else return 1;
}
inline double sqr(double x) { return x*x; }
struct Point {
double x,y;
Point() {}
Point(double _x,double _y) {
x=_x;
y=_y;
}
double distance(Point p) {
return hypot(x-p.x,y-p.y);
}
double len() {
return hypot(x,y);
}
double len2() {
return x*x+y*y;
}
Point trunc(double r) {
double l=len();
if(!sgn(l)) return *this;
r/=l;
return Point(x*r,y*r);
}
bool operator ==(Point b) const {
return sgn(x-b.x)==0 && sgn(y-b.y)==0;
}
bool operator <(Point b) const {
return sgn(x-b.x)==0 ? sgn(y-b.y)<0:x<b.x;
}
Point operator -(const Point& b) const {
return Point(x-b.x,y-b.y);
}
Point operator +(const Point& b) const {
return Point(x+b.x,y+b.y);
}
double operator ^(const Point& b) const {
return x*b.y-y*b.x;
}
double operator *(const Point& b) const {
return x*b.x+y*b.y;
}
Point operator *(const double& k) const {
return Point(x*k,y*k);
}
Point operator /(const double& k) const {
return Point(x/k,y/k);
}
Point rotleft() {
return Point(-y,x);
}
Point rotright() {
return Point(y,-x);
}
double rad(Point a,Point b) {
Point p=*this;
return fabs(atan2(fabs((a-p)^(b-p)),(a-p)*(b-p)));
}

};
struct Line {
Point s,e;
Line() {}
Line(Point _s,Point _e) {
s=_s;
e=_e;
}
bool operator ==(Line v) {
return (s==v.s)&&(e==v.e);
}
double length() {
return s.distance(e);
}
double dispointtoline(Point p) {
return fabs((p-s)^(e-s)) / length();
}
Point lineprog(Point p) {
return s+(((e-s)*((e-s)*(p-s)))/((e-s).len2()));
}
};
struct circle {
Point p;
double r;
circle() { }
circle(Point _p,double _r) {
p=_p;
r=_r;
}
circle(double x,double y,double _r) {
p=Point(x,y);
r=_r;
}
int relation(Point b) {
double dst=b.distance(p);
if(sgn(dst-r)<0) return 2;
else if(sgn(dst-r)==0) return 1;
return 0;
}
int relationcircle(circle v) {
double d=p.distance(v.p);
if(sgn(d-r-v.r)>0) return 5;
if(sgn(d-r-v.r)==0) return 4;
double l=fabs(r-v.r);
if(sgn(d-r-v.r)<0 && sgn(d-l)>0) return 3;
if(sgn(d-l)==0) return 2;
if(sgn(d-l)<0) return 1;
}
int pointcrosscircle(circle v,Point& p1,Point& p2) {
int rel=relationcircle(v);
if(rel==1 || rel==5) return 0;
double d=p.distance(v.p);
double l=(d*d+r*r-v.r*v.r)/(2*d);
double h=sqrt(r*r-l*l);
Point tmp=p+(v.p-p).trunc(l);
p1=tmp+((v.p-p).rotleft().trunc(h));
p2=tmp+((v.p-p).rotright().trunc(h));
if(rel==2 || rel==4)
return 1;
return 2;
}
int relationline(Line v) {
double dst=v.dispointtoline(p);
if(sgn(dst-r)<0) return 2;
else if(sgn(dst-r)==0) return 1;
return 0;
}
int pointcrossline(Line v,Point &p1,Point &p2) {
if(!(*this).relationline(v)) return 0;
Point a=v.lineprog(p);
double d=v.dispointtoline(p);
d=sqrt(r*r-d*d);
if(sgn(d)==0) {
p1=a;
p2=a;
return 1;
}
p1=a+(v.e-v.s).trunc(d);
p2=a-(v.e-v.s).trunc(d);
return 2;
}
double areatriangle(Point a,Point b) {
if(sgn((p-a)^(p-b))==0) return 0.0;
Point q[5];
int len=0;
q[len++]=a;
Line l(a,b);
Point p1,p2;
if(pointcrossline(l,q[1],q[2])==2) {
if(sgn((a-q[1])*(b-q[1]))<0) q[len++]=q[1];
if(sgn((a-q[2])*(b-q[2]))<0) q[len++]=q[2];
}
q[len++]=b;
if(len==4 && sgn((q[0]-q[1])*(q[2]-q[1]))>0) swap(q[1],q[2]);
double res=0;
for(int i=0;i<len-1;i++) {
if(relation(q[i])==0 || relation(q[i+1])==0) {
double arg=p.rad(q[i],q[i+1]);
res+=r*r*arg/2.0;
}
else {
res+=fabs((q[i]-p)^(q[i+1]-p))/2.0;
}
}
return res;
}
};
struct polygon {
int n;
Point p[maxp];
Line l[maxp];
void add(Point q) {
p[n++]=q;
}
double areacircle(circle c) {
double ans=0;
for(int i=0;i<n;i++) {
int j=(i+1)%n;
if(sgn((p[j]-c.p)^(p[i]-c.p))>=0)
ans+=c.areatriangle(p[i],p[j]);
else ans-=c.areatriangle(p[i],p[j]);
}
return fabs(ans);
}
}poly;
int main() {
int cas=0;
int n;
double x,y,r,k;
double x1,y1,x2,y2;
while(~scanf("%d%lf",&n,&k)) {
poly.n=0;
for(int i=1;i<=n;i++) {
scanf("%lf%lf",&x,&y);
poly.add(Point(x,y));
}
scanf("%lf%lf",&x1,&y1);
scanf("%lf%lf",&x2,&y2);
x=(x2-k*k*x1)/(1-k*k);
y=(y2-k*k*y1)/(1-k*k);
r=k*sqrt(sqr(x1-x2)+sqr(y1-y2))/(1-k*k);
printf("Case %d: %.10f\n",++cas,poly.areacircle(circle(x,y,r)));
}
}