您的位置:首页 > 其它

POJ 2540 Hotter Colder

2015-02-01 11:41 393 查看
求中垂线的半平面交

注意这里出现一个'Same'之后就一直输出0.00了

#include<stdio.h>
#include<math.h>
using namespace std;
const double eps=1e-8;
struct Point {
double x,y;
Point () {};
Point (double xx,double yy) {x=xx;y=yy;};
double operator^(const Point b)const{
return x*b.y-y*b.x;
}
Point operator-(const Point b)const{
return Point(x-b.x,y-b.y);
}
};
struct Line {
Point s,e;
Line(){};
Line(Point ss,Point ee){s=ss;e=ee;}
Point operator &(const Line b)const {
Point res=s;
double t=((s-b.e)^(b.s-b.e))/((s-e)^(b.s-b.e));
res.x+=(e.x-s.x)*t;
res.y+=(e.y-s.y)*t;
return res;
}
};
int n,cnt;
Point s,e,p[105],q[105];
Line t;
char str[100];

Line getline(Point s,Point e){
Point mid,t;
mid.x=(s.x+e.x)/2;  mid.y=(s.y+e.y)/2;
t.x=e.y-mid.y;
t.y=mid.x-e.x;
t.x+=mid.x;
t.y+=mid.y;
return Line(mid,t);
}
void cut(Line t){
int curCnt=0;
for(int i=1;i<=cnt;i++) {
double s=(t.e-t.s)^(p[i]-t.s);
if (str[0]=='H') s=-s;
if (s<=eps) q[++curCnt]=p[i];
else {
s=(t.e-t.s)^(p[i-1]-t.s);
if (str[0]=='H') s=-s;
if (s<-eps) q[++curCnt]=t&Line(p[i],p[i-1]);
s=(t.e-t.s)^(p[i+1]-t.s);
if (str[0]=='H') s=-s;
if (s<-eps) q[++curCnt]=t&Line(p[i],p[i+1]);
}
}
cnt=curCnt;
q[0]=q[cnt];    q[cnt+1]=q[1];
for(int i=0;i<=cnt+1;i++) p[i]=q[i];
//printf("%d\n",cnt);
double area=0;
for(int i=1;i<=cnt;i++) {
area+=p[i]^p[i+1];
//printf("%f   %f\n",p[i].x,p[i].y);
}
area=fabs(area/2);
if(area>eps) printf("%.2f\n",area);
else printf("0.00\n");

}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
s.x=0;  s.y=0;
p[1].x=0;   p[1].y=0;
p[2].x=10;   p[2].y=0;
p[3].x=10;   p[3].y=10;
p[4].x=0;   p[4].y=10;
p[5]=p[1];  p[0]=p[4];
cnt=4;
while(scanf("%lf%lf%s",&e.x,&e.y,str)!=EOF){
Line t;
t=getline(s,e);
if (str[0]=='S') cnt=0;
cut(t);

//printf("%f %f %f %f\n",t.s.x,t.s.y,t.e.x,t.e.y);
s=e;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: