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

来源:互联网 发布:下载我的淘宝网 编辑:程序博客网 时间:2024/05/20 13:07

An Easy Problem?!
Time Limit: 1000MS Memory Limit: 65536KTotal 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, x1y1x2y2x3y3x4y4. (x1y1), (x2y2) are the endpoints of one board, and (x3y3), (x4y4) 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

20 1 1 01 0 2 10 1 2 11 0 1 2

Sample Output

1.000.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 1000000000using 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
原创粉丝点击