poj 2540 Hotter Colder 半平面交 不等式求区域面积

在一个区间内寻找某一个点,每次走法会提示距目标是更近、更远还是相同。每次可确定一个目标必在的区域,求这个区域的面积。
 假定寻找者从A点走到B点,做线段AB的中垂线,取距目标近的那一半。用半平面交即可求该区域。
注意Same时区域面积为0;
若有出现面积为0,则之后的都是0;
  1 #include <cmath>
  2 #include <vector>
  3 #include <cstdio>
  4 #include <cstdlib>
  5 #include <cstring>
  6 #include <algorithm>
  7 const double eps = 1e-10;
  8 int sz;
  9 double flag;
 10 struct point
 11 {
 12     double x,y;
 13 }p[60],q[60];
 14 void init()
 15 {    
 16     sz = 4;
 17     p[1].x=p[1].y=0;
 18     p[2].x=10, p[2].y=0;    
 19     p[3].x=p[3].y=10;    
 20     p[4].x=0, p[4].y=10;   
 21     p[0]=p[4], p[5]=p[1];   
 22 }  
 23 
 24 int dcmp(double x)
 25 {    
 26     if (x < -eps)   
 27         return -1;  
 28     else   
 29         return (x > eps);      
 30 }
 31 point intersect(const point &p1, const point &p2, double a, double b, double c) 
 32 { 
 33     point g; 
 34     double u = fabs(a * p1. x + b * p1. y + c);    
 35     double v = fabs(a * p2. x + b * p2. y + c);    
 36     g.x=(p1.x*v + p2.x*u)/(u+v),g.y=(p1.y*v+p2.y*u)/(u+v);    
 37     return g;
 38 }      
 39 void cut(double a, double b, double c)
 40 {    
 41     int s = 0,i;    
 42     for ( i = 1; i <= sz; i++) 
 43     {     
 44         if (dcmp((a * p[i]. x + b * p[i]. y + c)*flag) >=0)    
 45             q[++s] = p[i];  
 46         else  
 47         {      
 48             if (dcmp((a * p[i - 1]. x + b * p[i - 1]. y + c)*flag) > 0)       
 49                 q[++s] = intersect(p[i-1], p[i], a, b, c);      
 50             if (dcmp((a * p[i + 1]. x + b * p[i + 1]. y + c)*flag) > 0)       
 51                 q[++s] = intersect(p[i], p[i+1], a, b, c);     
 52         }    
 53     } 
 54     for ( i = 1; i <= s; i++)  
 55         p[i] = q[i];     
 56     p[s+1] = p[1]; 
 57     p[0] = p[s];    
 58     sz = s;  
 59 }
 60 void solve(point p0,point t1,point t2)
 61 {
 62     double a,b,c;
 63     a=t2.y-t1.y;
 64     b=t1.x-t2.x;
 65     c=t2.x*t1.y-t2.y*t1.x;
 66     flag=a*p0.x+b*p0.y+c;
 67     cut(a,b,c);
 68 }
 69 int main()
 70 {
 71     int i,c=1;
 72     char s[20];
 73     double area;
 74     point p1,p2,t1,t2;
 75     p1.x=0;p1.y=0;
 76     init();
 77     while(scanf("%lf%lf%s",&p2.x,&p2.y,s)!=EOF)
 78     {
 79         t1.x=(p1.x+p2.x)/2;
 80         t1.y=(p1.y+p2.y)/2;
 81         t2.x=t1.x+p1.y-p2.y;
 82         t2.y=t1.y+p2.x-p1.x;
 83         area=0;
 84         if(strcmp("Same",s)==0||!c)
 85         {
 86             printf("0.00\n");
 87             c=0;
 88             continue;
 89         }
 90         if(strcmp("Colder",s)==0)
 91             solve(p1,t1,t2);
 92         else if(strcmp("Hotter",s)==0)
 93             solve(p2,t1,t2);
 94         for(i=1;i<=sz;i++)
 95             area+=p[i].x*p[i+1].y-p[i].y*p[i+1].x;
 96         printf("%.2f\n",fabs(area)/2);
 97         if(fabs(area)/2<eps)
 98             c=0;
 99         p1=p2;
100     }
101     return 0;
102 }
 
       
原文地址:https://www.cnblogs.com/zxj015/p/2740208.html