poj 2826 An Easy Problem?! (线段相交判定)

An Easy Problem?!
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12946 Accepted: 1988

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,  x 1y 1x 2y 2x 3y 3x 4y 4. ( x 1y 1), ( x 2y 2) are the endpoints of one board, and ( x 3y 3), ( x 4y 4) 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

Source

POJ Monthly--2006.04.28, Dagger@PKU_RPWT

[Submit]   [Go Back]   [Status]   [Discuss]

题目大意:两条线段组成一个图形,能容纳多少雨水。提示一下,水是从天空垂直落下的。

题解:线段相交判定。

首先要考虑两个线段没有交点的情况。

其次需要注意斜率不存在,和各种分母为0的情况。

精度控制一定要选好

还要考虑一条直线直接覆盖住一条直线的情况

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define N 100
#define eps 1e-12
#define inf 1000000000
using namespace std;
int n;
struct point{
	double x,y;
	point (double X=0,double Y=0){
		x=X,y=Y;
	}
	point operator - (point b){
		return point (x-b.x,y-b.y);
	}
	point operator + (point b){
		return point (x+b.x,y+b.y);
	}
	point operator * (double t){
		return point (x*t,y*t);
	}
}a[N],b[N];
typedef point vector;
int dcmp(double x)
{
	if (fabs(x)<eps) return 0;
	return x<0?-1:1;
}
double cross(vector a,vector b)
{
	return a.x*b.y-a.y*b.x;
}
bool segment(point a1,point a2,point b1,point b2)
{
	if (min(a1.x,a2.x)>max(b1.x,b2.x)||min(a1.y,a2.y)>max(b1.y,b2.y)||min(b1.x,b2.x)>max(a1.x,a2.x)||min(b1.y,b2.y)>max(a1.y,a2.y)) return 0;
	double c1=cross(a2-a1,b1-a1),c2=cross(a2-a1,b2-a1),
	    c3=cross(b2-b1,a1-b1),c4=cross(b2-b1,a2-b1);
    return dcmp(c1)*dcmp(c2)<=0&&dcmp(c3)*dcmp(c4)<=0;
}
point getans(point a1,point a2,point b1,point b2)
{
	vector u=a1-b1,v=a2-a1,w=b2-b1;
	if (dcmp(cross(v,w))==0)  return point(-inf,-inf);
	double t=cross(w,u)/cross(v,w);
	return a1+v*t;
}
int main()
{
   scanf("%d",&n);
   for (int l=1;l<=n;l++){
   	   for (int i=1;i<=2;i++) 
   	    scanf("%lf%lf",&a[i].x,&a[i].y);
   	   for (int i=1;i<=2;i++)
   	    scanf("%lf%lf",&b[i].x,&b[i].y);
   	   if (!segment(a[1],a[2],b[1],b[2])) {
   	   	    printf("0.00\n");
   	   	    continue;
		  }
	   if (a[2].y<a[1].y) swap(a[2],a[1]);
	   if (b[2].y<b[1].y) swap(b[2],b[1]);
	   if (a[2].y==a[1].y||b[2].y==b[1].y) {
	   	 printf("0.00\n");
	   	 continue;
	   }
	   point now=getans(a[1],a[2],b[1],b[2]);
	   if (now.x==-inf&&now.y==-inf) {
	   	printf("0.00\n");
	   	continue;
	   }
	   double h=min(b[2].y-now.y,a[2].y-now.y);
	   if (dcmp(h)<=0) {
	   	  printf("0.00\n");
	   	  continue;
	   }
	   if (b[2].y>a[2].y) 
	    for (int i=1;i<=2;i++)
	     swap(a[i],b[i]);
	   double k=0,b1=0,t=0,dis=0;
	   if (a[1].x!=a[2].x) {
	      k=(a[2].y-a[1].y)/(a[2].x-a[1].x);
	      double k1=(b[2].y-b[1].y)/(b[2].x-b[1].x);
	      if (dcmp(k)==0||dcmp(k1)==0) {
	      	printf("0.00\n");
	      	continue;
		  }
	      b1=a[1].y-k*a[1].x;
	      t=(b[2].y-b1)/k;
	      point c; c.x=b[2].x; c.y=1000000;
	      if (segment(b[2],c,a[1],a[2])) {
	      	printf("0.00\n");
	      	continue;
		  }
	      dis=fabs(b[2].x-t);
	   }
	   else dis=fabs(b[2].x-a[2].x);
	   printf("%.2lf\n",fabs(dis*h/2));
   }
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值