poj 2826 An Easy Problem?!(线段交,细节题)

//以下为原blog搬迁过来的内容

【题目大意】:给出两条线段,问这两条线段能够接住多少水。


【解题思路】:先大概估算了一下,这道题一共wa了不下20次。题目的大意不难,难在下面几个注意点上:

1)线段不能平行或者重合

                        2)不能够出现斜率为0的线段(10+wa以上才发现)

                        3)线段相交后开口应该向上

                        4)处于高位的线段的在x轴上的投影不能遮住地位线段的投影(两个方向)

5)求面积的时候,利用叉积/2来求,注意要以地位点的纵坐标为主

【代码】:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <string>
#include <cctype>
#include <map>
#include <iomanip>
                   
using namespace std;
                   
#define eps 1e-8
#define pi acos(-1.0)
#define inf 1<<30
#define pb push_back
#define lc(x) (x << 1)
#define rc(x) (x << 1 | 1)
#define lowbit(x) (x & (-x))
#define ll long long

double p,q;
int cnt;

struct Point
{
    double x, y;
    Point() {}
    Point(double a, double b)
    {
        x = a, y = b;
    }
}point[4];

struct Line
{
    Point a, b;
    Line() {}
    Line(Point x, Point y)
    {
        a = x, b = y;
    }
}line[2];

inline int sig(double k)
{
    return k < -eps ? -1 : k > eps;
}

inline double det(double x1, double y1, double x2, double y2)
{
    return x1 * y2 - x2 * y1;
}

inline double dotdet(double x1, double y1, double x2, double y2)
{
    return x1 * x2 + y1 * y2;
}

inline double dot(Point o, Point a, Point b)
{
    return dotdet(a.x - o.x, a.y - o.y, b.x - o.x, b.y - o.y);
}

inline double xmult(Point o, Point a, Point b)
{
    return det(a.x - o.x, a.y - o.y, b.x - o.x, b.y - o.y);
}

inline int between(Point o, Point a, Point b)
{
    return sig(dot(o, a, b));
}

inline bool sameline(Line u, Line v)
{
    if (sig(xmult(u.a, v.a, v.b)) == 0 && sig(xmult(u.b, v.a, v.b)) == 0)  return true;
    return false;
}

inline bool parallel(Line u, Line v)
{//利用斜率判平行,返回true表示平行
    return sig((u.a.x - u.b.x) * (v.a.y - v.b.y) - (v.a.x - v.b.x) * (u.a.y - u.b.y)) == 0;
}

inline int intersectt(Point a, Point b, Point c, Point d, Point &p)
{
    double s1, s2, s3, s4;
    int d1 = sig(s1 = xmult(a, b, c));
    int d2 = sig(s2 = xmult(a, b, d));
    int d3 = sig(s3 = xmult(c, d, a));
    int d4 = sig(s4 = xmult(c, d, b));
    if ((d1^d2) == -2 && (d3^d4) == -2)
    {
        p.x = (c.x * s2 - d.x * s1) / (s2 - s1);
        p.y = (c.y * s2 - d.y * s1) / (s2 - s1);
        return 1;
    }
    if((d1==0&&d2==0)||(d3==0&&d4==0)) return 0;
    if (d1 == 0 && between(c, a, b) <= 0) {p=c; return 2;}
    if (d2 == 0 && between(d, a, b) <= 0) {p=d; return 2;}
    if (d3 == 0 && between(a, c, d) <= 0) {p=a; return 2;}
    if (d4 == 0 && between(b, c, d) <= 0) {p=b; return 2;}
    return 0;
}

inline int intersect(Line u, Line v, Point &p)
{
    return intersectt(u.a, u.b, v.a, v.b, p);
}

inline double get_area(Point a,Point b,Point c)
{
    return fabs((a.x*b.y-a.y*b.x+b.x*c.y-c.x*b.y+c.x*a.y-a.x*c.y)/2);
}

double solve(Line a,Line b)
{
    if ((point[0].y==point[1].y) ||(point[2].y==point[3].y)) return 0;
    if (sameline(a,b)==true) return 0;  //判重合
    if (parallel(a,b)==true) return 0;  //判平行
    Point tmp;
    int k;
    k=intersect(a,b,tmp);               //判相交,顺便求交点
    if (k==0) return 0;
    else
    {
        //判断开口向上
        cnt=0;
        for (int i=0; i<4; i++)
            if (point[i].y>tmp.y) cnt++;
        if (cnt<=1) return 0;
        //判断线段的覆盖
        if (point[1].x==point[3].x) return 0;
        if ((point[1].x<tmp.x&&point[3].x<point[1].x&&xmult(tmp,point[3],point[1])>0)||
            (point[1].x>tmp.x&&point[3].x>point[1].x&&xmult(tmp,point[3],point[1])<0))
            return 0;
        /*
        if (point[1].x<=tmp.x && point[3].x<=tmp.x)
        {
            if (point[1].x>=point[3].x) return 0;
        }
        //右侧线段的覆盖
        if (point[1].x>=tmp.x && point[3].x>=tmp.x)
        {
            if (point[3].x>=point[1].x) return 0;
        }*/
        //求面积,以低的木板作为标准
        double anss;
        Point tmp1;
        tmp1=Point((point[1].y-tmp.y)*(point[3].x-tmp.x)/(point[3].y-tmp.y)+tmp.x,point[1].y);
        anss=get_area(tmp,point[1],tmp1);
        return anss;
    }
}

int main()
{
    int T;
    scanf("%d",&T);
    while (T--)
    {
        scanf("%lf%lf",&p,&q);
        point[0]=Point(p,q);
        scanf("%lf%lf",&p,&q);
        point[1]=Point(p,q);
        scanf("%lf%lf",&p,&q);
        point[2]=Point(p,q);
        scanf("%lf%lf",&p,&q);
        point[3]=Point(p,q);
        if (point[0].y>point[1].y) swap(point[0],point[1]);
        if (point[2].y>point[3].y) swap(point[2],point[3]);
        if (point[1].y>point[3].y) {swap(point[0],point[2]); swap(point[1],point[3]); }
        line[0]=Line(point[0],point[1]);
        line[1]=Line(point[2],point[3]);
        printf("%.2lf\n",solve(line[0],line[1]));
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值