POJ-2826 求交点与面积

Description
It’s raining outside. Farmer Johnson’s bull Ben wants some rain to water his flowers. Ben nails two wooden boards on the wall of his barn. Shown in the pictures below, the two boards on the wall just look like two segments on the plane, as they have the same width.

Your mission is to calculate how much rain these two boards can collect.
Input
The first line contains the number of test cases.
Each test case consists of 8 integers not exceeding 10,000 by absolute value, x1, y1, x2, y2, x3, y3, x4, y4. (x1, y1), (x2, y2) are the endpoints of one board, and (x3, y3), (x4, y4) are the endpoints of the other one.
Output
For each test case output a single line containing a real number with precision up to two decimal places - the amount of rain collected.
Sample Input
2
0 1 1 0
1 0 2 1
0 1 2 1
1 0 1 2
Sample Output
1.00
0.00
解题思路:
方便起见,每个线段端点 s 在端点 e 上方
0.00的情况:
1.其中一线段斜率为0
2.两线段斜率符号相同,靠近y轴的线段挡住了另一条线段
其他情况:
以两条线段 s y坐标较小的作为高计算面积,用叉积
最后要加个EPS,没过的看一下poj,discuss的数据
代码:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
using namespace std;
const double EP=1e-8;
struct POINT{
	double x,y;
	POINT(double x=0,double y=0):x(x),y(y){}
}cross,p;
struct LINESEG{
	POINT s,e;
	LINESEG(POINT s=0,POINT e=0):s(s),e(e){}
};
struct LINE{
	double a,b,c;
   	LINE(double a=1,double b=-1,double c=0):a(a),b(b),c(c){}
};
double multiply(POINT sp,POINT ep,POINT op){
	return(sp.x-op.x)*(ep.y-op.y)-(ep.x-op.x)*(sp.y-op.y);
}
LINE makeline(POINT p1,POINT p2){//直线一般式
	LINE tl;
 	int sign=1;
 	tl.a=p2.y-p1.y;
 	if(tl.a<0){
  		sign=-1;
  		tl.a=sign*tl.a;
 	}
 	tl.b=sign*(p1.x-p2.x);
 	tl.c=sign*(p1.y*p2.x-p1.x*p2.y);
 	return tl;
}
bool online(LINESEG l,POINT p){//在线段上
 	return((fabs(multiply(l.e,p,l.s))<=EP)&&(((p.x-l.s.x)*(p.x-l.e.x)<=0)&&((p.y-l.s.y)*(p.y-l.e.y)<=0)));
}
bool lineintersect(LINE l1,LINE l2,POINT &p){//直线相交返回交点
	double d=l1.a*l2.b-l2.a*l1.b;
 	if(fabs(d)<=EP)return false;
 	p.x=(l2.c*l1.b-l1.c*l2.b)/d;
 	p.y=(l2.a*l1.c-l1.a*l2.c)/d;
 	return true;
}
bool intersection(LINESEG l1,LINESEG l2,POINT &inter){//线段相交返回交点
	LINE ll1,ll2;
 	ll1=makeline(l1.s,l1.e);
 	ll2=makeline(l2.s,l2.e);
 	if(lineintersect(ll1,ll2,inter))return online(l1,inter)&&online(l2,inter);
  	else return false;
}
double slope(LINE l){//斜率
	if(fabs(l.a)<1e-20)return 0;
 	if(fabs(l.b)<1e-20)return 1e9;
	return -(l.a/l.b);
}
int main(){
	int t;scanf("%d",&t);
	while(t--){
		double sx1,sy1,ex1,ey1;scanf("%lf%lf%lf%lf",&sx1,&sy1,&ex1,&ey1);
		double sx2,sy2,ex2,ey2;scanf("%lf%lf%lf%lf",&sx2,&sy2,&ex2,&ey2);
		POINT sp1=POINT(sx1,sy1),ep1=POINT(ex1,ey1),sp2=POINT(sx2,sy2),ep2=POINT(ex2,ey2);
		if(sy1<ey1)swap(sp1,ep1);if(sy2<ey2)swap(sp2,ep2);
		LINESEG seg1=LINESEG(sp1,ep1);
		LINESEG seg2=LINESEG(sp2,ep2);
		if(!intersection(seg1,seg2,cross)||fabs(sy1-ey1)<=EP||fabs(sy2-ey2)<=EP)puts("0.00");
		else{

			if(multiply(seg2.s,seg1.s,cross)<0)swap(seg1,seg2);
			LINE u=makeline(seg1.s,seg1.e),v=makeline(seg2.s,seg2.e);
			if(slope(u)*slope(v)>=0&&fabs(seg1.s.x)>=fabs(seg2.s.x)){
				puts("0.00");
				continue;
			}
			if(seg1.s.y>seg2.s.y)swap(seg1,seg2);
			lineintersect(makeline(seg2.s,seg2.e),makeline(seg1.s,POINT(100000,seg1.s.y)),p);
			printf("%.2f\n",fabs(multiply(seg1.s,p,cross))/2.0+EP);
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值