码迷,mamicode.com
首页 > 其他好文 > 详细

poj2540Hotter Colder(半平面交)

时间:2014-08-04 10:27:07      阅读:294      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   for   art   

链接

根据距离可以列得直线方程,附上初始矩形的四个顶点,依次用直线切割。

bubuko.com,布布扣
  1 #include<iostream>
  2 #include <stdio.h>
  3 #include <math.h>
  4 #include<cstring>
  5 #include<algorithm>
  6 #define eps 1e-8
  7 using namespace std;
  8 const int MAXN=1550;
  9 int m;
 10 double r,tx,ty,ans;
 11 int cCnt,curCnt;//此时cCnt为最终切割得到的多边形的顶点数、暂存顶点个数
 12 struct point
 13 {
 14     double x,y;
 15     point(double x=0,double y=0):x(x),y(y) {}
 16 };
 17 point points[MAXN],p[MAXN],q[MAXN];//读入的多边形的顶点(顺时针)、p为存放最终切割得到的多边形顶点的数组、暂存核的顶点
 18 void getline(point x,point y,double &a,double &b,double   &c) //两点x、y确定一条直线a、b、c为其系数
 19 {
 20     a = y.y - x.y;
 21     b = x.x - y.x;
 22     c = y.x * x.y - x.x * y.y;
 23 }
 24 void initial()
 25 {
 26     for(int i = 1; i <= m; ++i)p[i] = points[i];
 27     p[m+1] = p[1];
 28     p[0] = p[m];
 29     cCnt = m;//cCnt为最终切割得到的多边形的顶点数,将其初始化为多边形的顶点的个数
 30 }
 31 point intersect(point x,point y,double a,double b,double c) //求x、y形成的直线与已知直线a、b、c、的交点
 32 {
 33     //cout<<a<<" "<<b<<" "<<c<<endl;
 34     double u = fabs(a * x.x + b * x.y + c);
 35     double v = fabs(a * y.x + b * y.y + c);
 36     // cout<<u<<" "<<v<<endl;
 37     point pt;
 38     pt.x=(x.x * v + y.x * u) / (u + v);
 39     pt.y=(x.y * v + y.y * u) / (u + v);//cout<<pt.x<<" -"<<pt.y<<" "<<x.x<<" "<<y.x<<endl;
 40     return  pt;
 41 }
 42 void cut(double a,double b ,double c)
 43 {
 44     curCnt = 0;
 45     for(int i = 1; i <= cCnt; ++i)
 46     {
 47         if(a*p[i].x + b*p[i].y + c > -eps)q[++curCnt] = p[i];
 48         else
 49         {
 50             if(a*p[i-1].x + b*p[i-1].y + c > eps)
 51             {
 52                 q[++curCnt] = intersect(p[i-1],p[i],a,b,c);
 53             }
 54             if(a*p[i+1].x + b*p[i+1].y + c > eps) //原理同上
 55             {
 56                 q[++curCnt] = intersect(p[i],p[i+1],a,b,c);
 57             }
 58         }
 59         //cout<<q[curCnt].x<<" --"<<q[curCnt].y<<" "<<i<<" "<<p[i].x<<" "<<p[i].y<<endl;
 60     }
 61     for(int i = 1; i <= curCnt; ++i)p[i] = q[i];
 62     p[curCnt+1] = q[1];
 63     p[0] = p[curCnt];
 64     cCnt = curCnt;
 65 }
 66 int dcmp(double x)
 67 {
 68     if(fabs(x)<eps) return 0;
 69     return x<0?-1:1;
 70 }
 71 void solve(double x,double y,int flag)
 72 {
 73     double a,b,c;
 74     if(flag == 1)
 75     {
 76         a = -2*x+2*tx,b = -2*y+2*ty,c = x*x+y*y-tx*tx-ty*ty;
 77     }
 78     else if(flag==2)
 79     {
 80         a = 2*x-2*tx,b = 2*y-2*ty,c = tx*tx+ty*ty-x*x-y*y;
 81     }
 82 //    if(dcmp(a)==0&&dcmp(b)==0&&dcmp(c)<=0)
 83 //    {
 84 //        ans = 0;
 85 //        return ;
 86 //    }
 87     cut(a,b,c);
 88 }
 89 int main()
 90 {
 91     points[1] = point(0,0);
 92     points[2] = point(0,10);
 93     points[3] = point(10,10);
 94     points[4] = point(10,0);
 95     p[5] = p[1];
 96     m = 4;
 97     tx = 0,ty=0;
 98     char s[10];
 99     double x,y;
100     initial();
101     while(scanf("%lf%lf%s",&x,&y,s)!=EOF)
102     {
103         int flag;
104         if(strcmp(s,"Colder")==0||strcmp(s,"Same")==0)solve(x,y,1);
105         if(strcmp(s,"Hotter")==0||strcmp(s,"Same")==0)solve(x,y,2);
106         tx = x,ty=y;
107         double area = 0;
108         for(int i = 1; i <= cCnt; ++i)
109         {
110             //cout<<p[i].x<<" "<<p[i].y<<endl;
111             area += p[i].x * p[i + 1].y - p[i + 1].x * p[i].y;
112         }
113         area = fabs(area / 2.0);
114         ans = area;
115         printf("%.2f\n",ans);
116     }
117 }
View Code

 

poj2540Hotter Colder(半平面交),布布扣,bubuko.com

poj2540Hotter Colder(半平面交)

标签:style   blog   http   color   os   io   for   art   

原文地址:http://www.cnblogs.com/shangyu/p/3889305.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!