POJ2540 Hotter Colder 半平面交

半平面交,求可行区域的面积。

题目中求两点连线的中点形成的中垂线。再进行切割

Hotter Colder
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 2079 Accepted: 860

Description

The children's game Hotter Colder is played as follows. Player A leaves the room while player B hides an object somewhere in the room. Player A re-enters at position (0,0) and then visits various other positions about the room. When player A visits a new position, player B announces "Hotter" if this position is closer to the object than the previous position; player B announces "Colder" if it is farther and "Same" if it is the same distance.

Input

Input consists of up to 50 lines, each containing an x,y coordinate pair followed by "Hotter", "Colder", or "Same". Each pair represents a position within the room, which may be assumed to be a square with opposite corners at (0,0) and (10,10).

Output

For each line of input print a line giving the total area of the region in which the object may have been placed, to 2 decimal places. If there is no such region, output 0.00.

Sample Input

10.0 10.0 Colder
10.0 0.0 Hotter
0.0 0.0 Colder
10.0 10.0 Hotter

Sample Output

50.00
37.50
12.50
0.00


#include<iostream>
#include<cmath>
#include<cstdio>
#include<complex>
#include<cstring>
#include<string>
#include<algorithm>
#include<iomanip>
#include<cstdlib>


using namespace std;
#define max(a,b)  (a>b?a:b)
#define min(a,b)  (a<b?a:b)
#define eps 1e-8
#define zero(x)   (((x)>0?(x):-(x))<eps)  
const double pi=acos(-1.0);
struct point { double x,y;};
struct line  { double a,b,c;};

#define MAXN 200
int n,cut_count,cur_count;
point p[MAXN],tp[MAXN],que[MAXN];
point p1,p2;
string s;

int sig(double k)
{
	if(fabs(k)<eps)
		return 0;
	return (k>0)?1:-1;
} 

double xmult(point a,point b,point c)
{
	return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
}

line getline(point p1,point p2)
{
	line temp;
	temp.a=p2.y-p1.y;
	temp.b=p1.x-p2.x;
	temp.c=p2.x*p1.y-p1.x*p2.y;
	return temp;
}

point intersect(point x,point y,line k)
{  
	point temp; 
    double u=fabs(k.a*x.x+k.b*x.y+k.c);  
    double v=fabs(k.a*y.x+k.b*y.y+k.c);
	temp.x=(x.x*v+y.x*u)/(u+v);
	temp.y=(x.y*v+y.y*u)/(u+v);
    return temp;
}  
 
void init()
{
	p[1].x=p[1].y=0;
	p[2].x=0,p[2].y=10;
	p[3].x=p[3].y=10;
	p[4].x=10,p[4].y=0;
	p[0]=p[4];
	p[5]=p[1];
	cut_count=4;
	for(int i=1;i<=4;i++)
		tp[i]=p[i];
	tp[0]=tp[4];
	tp[5]=tp[1];	
}

line get_mid_line(point a,point b)
{
	line teml,midl;
	point temp;
	double dir;
	temp.x=(a.x+b.x)/2;
	temp.y=(a.y+b.y)/2;
	teml=getline(a,b);
	midl.a=teml.b;
	midl.b=-teml.a;
	midl.c=(-midl.a*temp.x)+(-midl.b*temp.y);
	dir=midl.a*a.x+midl.b*a.y+midl.c;
	if(s[0]=='C')
	{
		if(sig(dir)>=0)
			return midl;
		midl.a=-midl.a;
		midl.b=-midl.b;
		midl.c=-midl.c;
		return midl;
	}
	if(s[0]=='H')
	{
		if(sig(dir)<=0)
			return midl;
		midl.a=-midl.a;
		midl.b=-midl.b;
		midl.c=-midl.c;
		return midl;
	}
}


void cut(line edge)
{
	cur_count=0;
	for(int i=1;i<=cut_count;i++)
	{
		///cout<<
		//cout<<edge.a*tp[i].x+edge.b*tp[i].y+edge.c<<endl;
		if(sig(edge.a*tp[i].x+edge.b*tp[i].y+edge.c)>=0)
			que[++cur_count]=tp[i];
		else
		{
			if(sig(edge.a*tp[i-1].x+edge.b*tp[i-1].y+edge.c)>0)
			{
				que[++cur_count]=intersect(tp[i-1],tp[i],edge);
			}
			if(sig(edge.a*tp[i+1].x+edge.b*tp[i+1].y+edge.c)>0)
			{
				que[++cur_count]=intersect(tp[i+1],tp[i],edge);
			}
		}
	}
	for(int i=1;i<=cur_count;i++)
		tp[i]=que[i];
	tp[cur_count+1]=que[1];
	tp[0]=tp[cur_count];
	cut_count=cur_count;
	double area=0;
	for(int i=3;i<=cur_count;i++)
		area+=xmult(tp[1],tp[i-1],tp[i]);
	area=fabs(area)/2.0;
	cout<<setprecision(2)<<setiosflags(ios::fixed)<<area<<endl;
}

int main()
{
	line edge;
	p1.x=p1.y=0;
	init();
	bool flag=0;
	while(cin>>p2.x>>p2.y>>s)
	{
		if(s[0]=='S' || flag)
		{
			flag=1;
			cout<<"0.00"<<endl;
			continue;
		}
		edge=get_mid_line(p1,p2);
		cut(edge);
		p1=p2;
	}
	return 0; 
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值