poj2826(线段相交)

传送门:An Easy Problem?!

题意:用两条线段接雨水,雨水是垂直落下的,问我们用给定的两条线段能接到多少水。

分析:看起来很简单,写起来略麻烦,先排除不能接到水的情况:

1. 两条线段不相交;

  2. 其中任意一条线段水平;

  3. 两条线段重合;

  4. 相交的情况下,最高的端点遮住了次高的端点

最后求线段交点确定三角形并用叉积求面积。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h>

using namespace std;

const double eps = 1e-8;
const double PI = acos(-1.0);
const int N = 100010;
int sgn(double x)
{
    if(fabs(x) < eps)return 0;
    if(x < 0)return -1;
    else return 1;
}
struct Point
{
    double x,y;
    Point(){}
    Point(double _x,double _y)
    {
        x = _x;y = _y;
    }
    Point operator -(const Point &b)const
    {
        return Point(x - b.x,y - b.y);
    }
    //叉积
    double operator ^(const Point &b)const
    {
        return x*b.y - y*b.x;
    }
};
struct Line
{
    Point s,e;
    Line(){}
    Line(Point _s,Point _e)
    {
        s = _s;e = _e;
    }
    //两直线相交求交点
    //第一个值为0表示直线重合,为1表示平行,为0表示相交,为2是相交
    //只有第一个值为2时,交点才有意义
    pair<int,Point> operator &(const Line &b)const
    {
        Point res=s;
        if(sgn((s-e)^(b.s-b.e))==0)
        {
            if(sgn((s-b.e)^(b.s-b.e))==0)
                return make_pair(0,res);
            else return make_pair(1,res);
        }
        double t=((s-b.s)^(b.s-b.e))/((s-e)^(b.s-b.e));
        res.x+=(e.x-s.x)*t;
        res.y+=(e.y-s.y)*t;
        return make_pair(2,res);
    }
};
//判断线段相交
bool inter(Line l1,Line l2)
{
    return
    max(l1.s.x,l1.e.x) >= min(l2.s.x,l2.e.x) &&
    max(l2.s.x,l2.e.x) >= min(l1.s.x,l1.e.x) &&
    max(l1.s.y,l1.e.y) >= min(l2.s.y,l2.e.y) &&
    max(l2.s.y,l2.e.y) >= min(l1.s.y,l1.e.y) &&
    sgn((l2.s-l1.e)^(l1.s-l1.e))*sgn((l2.e-l1.e)^(l1.s-l1.e)) <= 0 &&
    sgn((l1.s-l2.e)^(l2.s-l2.e))*sgn((l1.e-l2.e)^(l2.s-l2.e)) <= 0;
}
int main()
{
    int T;
    Line l1,l2;
    scanf("%d",&T);
    while(T--)
    {
        double a,b,c,d;
        scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
        l1=Line(Point(a,b),Point(c,d));
        scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
        l2=Line(Point(a,b),Point(c,d));
        if(sgn(l1.s.y-l1.e.y)==0||sgn(l2.s.y-l2.e.y)==0||!inter(l1,l2))
        {
            puts("0.00");
            continue;
        }
        if(sgn(l1.s.y-l1.e.y)<0)swap(l1.s,l1.e);
        if(sgn(l2.s.y-l2.e.y)<0)swap(l2.s,l2.e);
        if(inter(Line(l1.s,Point(l1.s.x,100000)),l2)||inter(Line(l2.s,Point(l2.s.x,100000)),l1))
        {
            puts("0.00");
            continue;
        }
        if(sgn(l1.s.y-l2.s.y)<0)
        {
            Point p=(l1&l2).second;
            Point p1=(Line(l1.s,Point(100000,l1.s.y))&l2).second;
            double ans=fabs((l1.s-p)^(p1-p))/2;
            printf("%.2lf\n",ans);
        }
        else
        {
            Point p=(l1&l2).second;
            Point p1=(Line(l2.s,Point(100000,l2.s.y))&l1).second;
            double ans=fabs((l2.s-p)^(p1-p))/2;
            printf("%.2lf\n",ans);
        }
    }
}

时间: 2024-12-14 18:12:20

poj2826(线段相交)的相关文章

HDOJ1086-You can Solve a Geometry Problem too(线段相交)

Problem Description Many geometry(几何)problems were designed in the ACM/ICPC. And now, I also prepare a geometry problem for this final exam. According to the experience of many ACMers, geometry problems are always much trouble, but this problem is ve

POJ 2653 Pick-up sticks [线段相交 迷之暴力]

Pick-up sticks Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 12861   Accepted: 4847 Description Stan has n sticks of various length. He throws them one at a time on the floor in a random way. After finishing throwing, Stan tries to fin

poj 3304 直线与线段相交

Segments Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12161   Accepted: 3847 Description Given n segments in the two dimensional space, write a program, which determines if there exists a line such that after projecting these segments

poj 2653 线段与线段相交

Pick-up sticks Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 11884   Accepted: 4499 Description Stan has n sticks of various length. He throws them one at a time on the floor in a random way. After finishing throwing, Stan tries to fin

hdu 1086(判断线段相交)

传送门:You can Solve a Geometry Problem too 题意:给n条线段,判断相交的点数. 分析:判断线段相交模板题,快速排斥实验原理就是每条线段代表的向量和该线段的一个端点与 另一条线段的两个端点构成的两个向量求叉积,如果线段相交那么另一条线段两个端点必定在该线段的两边,则该线段代表的向量必定会顺时针转一遍逆时针转一遍,叉积必定会小于等于0,同样对另一条线段这样判断一次即可. #include <algorithm> #include <cstdio>

hdu1086(线段相交)

题目意思: 给出n个线段,判断这n条线段中,线段相交的对数. http://acm.hdu.edu.cn/showproblem.php?pid=1086 题目分析: 此题主要写出判断线段相交的函数,然后判断每一对线段即可,时间复杂度O(n*n).详细解释见代码. AC代码: /** *判断AB和CD两线段是否有交点: *同时满足两个条件:('x'表示叉积) * 1.C点D点分别在AB的两侧.(向量(ABxAC)*(ABxAD)<=0) * 2.A点和B点分别在CD两侧.(向量(CDxCA)*(

HDU 1086 You can Solve a Geometry Problem too(判断线段相交)

题目地址:HDU 1086 就这么一道仅仅判断线段相交的题目写了2k多B的代码..是不是有点浪费...但是我觉得似乎哪里也优化不了了.... 判断线段相交就是利用的叉积.假如现在两条线段分别是L1和L2,先求L1和L2两个端点与L1的某个端点的向量的叉积,如果这两个的叉积的乘积小于0的话,说明L1在是在L2两个端点之间的,但此时并不保证一定相交.此时需要用同样的方法去判断L2是否在L1的两个端点之间,如果L2也在L1的两个端点之间的话,那就足以说明L1与L2相交.但是这题还需要判断是否端点也相交

直线与直线相交 直线与线段相交 线段与线段相交

int sgn(double x) { if(fabs(x) < eps) return 0; return x < 0 ? -1:1; } struct Point { double x,y; Point() {} Point(double _x,double _y) { x = _x,y = _y; } Point operator -(const Point &b)const { return Point(x - b.x,y - b.y); } //叉积 double opera

POJ2653 Pick-up sticks(线段相交)

题目链接: http://poj.org/problem?id=2653 题目描述: Pick-up sticks Description Stan has n sticks of various length. He throws them one at a time on the floor in a random way. After finishing throwing, Stan tries to find the top sticks, that is these sticks su