POJ 2540 Hotter Colder

求中垂线的半平面交

注意这里出现一个'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;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值