poj2826 An Easy Problem?!【计算几何】

含【三点坐标计算面积】、【判断两线段是否有交点】、【求线段交点】模板
 
An Easy Problem?!
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions:15921   Accepted: 2459

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

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

题意:

给定四个坐标两条线

现在从上面倒水下来 问这两条线可以接住多少水

思路:

用G++WA了,用C++过了

两条线没有交点的0.00

两条线平行或重合的0.00

需要注意 如果口被封掉的 也是0.00

像这样

之前就在想这样的话需要怎么判断 其实就是如果某条线右边的端点往上找发现被截断了就是口被封住了

之后只需要先求出两线段交点 然后用纵坐标矮一点的那个点水平画线 找到和另一条线的交点

这三个点构成一个三角形 输出其面积

  1 //#include <bits/stdc++.h>
  2 #include<iostream>
  3 #include<cmath>
  4 #include<algorithm>
  5 #include<stdio.h>
  6 #include<cstring>
  7 
  8 using namespace std;
  9 typedef long long int LL;
 10 
 11 const double eps = 1e-8;
 12 
 13 int sgn(double x)
 14 {
 15     if(fabs(x) < eps) return 0;
 16     if(x < 0) return -1;
 17     else return 1;
 18 }
 19 struct point{
 20     double x, y;
 21     point(){}
 22     point(double _x, double _y)
 23     {
 24         x = _x;
 25         y = _y;
 26     }
 27     point operator -(const point &b)const
 28     {
 29         return point(x - b.x, y - b.y);
 30     }
 31     double operator ^(const point &b)const
 32     {
 33         return x * b.y - y * b.x;
 34     }
 35     double operator *(const point &b)const
 36     {
 37         return x * b.x + y * b.y;
 38     }
 39     void input()
 40     {
 41         scanf("%lf%lf", &x, &y);
 42     }
 43 };
 44 
 45 struct line{
 46     point s, e;
 47     line(){}
 48     line(point _s, point _e)
 49     {
 50         s = _s;
 51         e = _e;
 52     }
 53     pair<int, point>operator &(const line &b)const
 54     {
 55         point res = s;
 56         if(sgn((s - e) ^ (b.s - b.e)) == 0){
 57             if(sgn((s - b.e) ^ (b.s - b.e)) == 0){
 58                 return make_pair(0, res);
 59             }
 60             else return make_pair(1, res);
 61         }
 62         double t = ((s - b.s) ^ (b.s - b.e)) / ((s - e) ^ (b.s - b.e));
 63         res.x += (e.x - s.x) * t;
 64         res.y += (e.y - s.y) * t;
 65         return make_pair(2, res);
 66     }
 67 };
 68 
 69 bool inter(line l1, line l2)
 70 {
 71     return
 72         max(l1.s.x, l1.e.x) >= min(l2.s.x, l2.e.x) &&
 73         max(l2.s.x, l2.e.x) >= min(l1.s.x, l1.e.x) &&
 74         max(l1.s.y, l1.e.y) >= min(l2.s.y, l2.e.y) &&
 75         max(l2.s.y, l2.e.y) >= min(l1.s.y, l1.e.y) &&
 76         sgn((l2.s - l1.s) ^ (l1.e - l1.s)) * sgn((l2.e - l1.s) ^ (l1.e - l1.s)) <= 0 &&
 77         sgn((l1.s - l2.s) ^ (l2.e - l1.s)) * sgn((l1.e - l2.s) ^ (l2.e - l2.s)) <= 0;
 78 }
 79 
 80 double area(point a, point b, point c)
 81 {
 82     return fabs((1.0 / 2) * (a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y)));
 83 }
 84 
 85 int t;
 86 line l1, l2;
 87 
 88 int main()
 89 {
 90     scanf("%d", &t);
 91     while(t--){
 92         l1.s.input();l1.e.input();
 93         l2.s.input();l2.e.input();
 94         if(sgn(l1.s.y - l1.e.y) < 0){
 95             swap(l1.s, l1.e);
 96         }
 97         if(sgn(l2.s.y - l2.e.y) < 0){
 98             swap(l2.s, l2.e);
 99         }
100         if(!inter(l1, l2)){
101             printf("0.00
");
102         }
103         else if(inter(line(l1.s, point(l1.s.x, 100000)), l2)){
104             printf("0.00
");
105         }
106         else if(inter(line(l2.s, point(l2.s.x, 100000)), l1)){
107             printf("0.00
");
108         }
109         else{
110             pair<int, point>pr = l1 & l2;
111             if(pr.first != 2){
112                 printf("0.00
");
113             }
114             else{
115                 point a, b;
116                 if(l1.s.y > l1.e.y){
117                     a = l1.s;
118                 }
119                 else{
120                     a = l1.e;
121                 }
122                 if(l2.s.y > l2.e.y){
123                     b = l2.s;
124                 }
125                 else{
126                     b = l2.e;
127                 }
128                 if(a.y < b.y){
129                     b.x = a.x + 1;b.y = a.y;
130                     line l3 = line(a, b);
131                     pair<int, point>ppr = l2 & l3;
132                     b = ppr.second;
133                 }
134                 else{
135                     a.x = b.x + 1; a.y = b.y;
136                     line l3 = line(a, b);
137                     pair<int, point>ppr = l1 & l3;
138                     a = ppr.second;
139                 }
140                 double ans = area(a, b, pr.second);
141                 printf("%.2f
", ans);
142             }
143         }
144     }
145     return 0;
146 }
原文地址:https://www.cnblogs.com/wyboooo/p/9740122.html