码迷,mamicode.com
首页 > 其他好文 > 详细

Poj_1410 Intersection -线段类

时间:2016-08-13 11:17:57      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

题意:直线是否与矩形(多边形)相交。

坑点:1、给定的矩形的点只是对角线的点,并没有规定是哪一对。

        2、矩形:不仅仅与四边相交,里面的面积也算。

        3、我竟然把输出搞错,wa无数次。

/************************************************
Author        :DarkTong
Created Time  :2016/8/12 15:09:58
File Name     :1_modify.cpp
*************************************************/

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>

#define INF 0x3f3f3f3f
#define esp 1e-9
typedef long long LL;
using namespace std;

typedef long long db;
const double eps = 1e-8;
const double pi = acos(-1.0);
/*************************************************************** *二维点基本运算
\***************************************************************/
int dcmp(db x){ if(x==0) return 0; else return x>0?1:-1; }
inline db sqr(db x){ return x*x;}

struct Point
{
    db x, y;
    Point(db x=0, db y=0):x(x), y(y){}
    //输入
    void Inputs(){ scanf("%lld%lld", &x, &y);}
    //向量运算
    friend Point operator+(const Point &a, const Point &b)
    {
        return Point(a.x+b.x, a.y+b.y);
    }
    friend Point operator-(const Point &a, const Point &b)
    {
        return Point(a.x-b.x, a.y-b.y);
    }
    friend Point operator*(const db &b, const Point &a)
    {
        return Point(a.x*b, a.y*b);
    }
    friend Point operator*(const Point &a, const db &b)
    {
        return Point(a.x*b, a.y*b);
    }
    friend Point operator/(const Point &a, const db &b)
    {
        return Point(a.x/b, a.y/b);
    }
    //逻辑运算
    friend bool operator==(const Point &a, const Point &b)
    {
        return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0;
    }
    const bool operator < (const Point &a) const
    {
        if(dcmp(x-a.x)) return y<a.y;
        else return x<a.x;
    }
};
//点积&叉积
db det(const Point &a, const Point &b){ return a.x*b.y - a.y*b.x;}    //意义:判平行,得平行四边形面积
db dot(const Point &a, const Point &b){ return a.x*b.x + a.y*b.y;}
struct Line
{
    Point a, b;
    Line(){}
    Line(Point a, Point b):a(a), b(b){}
};
//点是否在线段上(包括端点)
bool PointOnSegment(Point p, Point s, Point t)
{
    return dcmp(det(p-s, t-s))==0&&dcmp(dot(p-s, p-t))<=0;
}
//判平行,不判共线
bool parallel(Line a, Line b)
{
    return !dcmp(det(a.a-a.b, b.a-b.b));
}
/*************************************************************/
Point a, b, dl, ul, dr, ur;
bool isok(Line s, Line t)
{
    if(PointOnSegment(t.a, s.a, s.b)) return true;
    if(PointOnSegment(t.b, s.a, s.b)) return true;
    if(parallel(s, t)) return false;
    if(det(s.b-s.a, t.a-s.a)*det(s.b-s.a, t.b-s.a)<0
        &&det(t.b-t.a, s.a-t.a)*det(t.b-t.a, s.b-t.a)<0) return true;
//    cout<<"tax:"<<t.a.x<<" tay:"<<t.a.y<<"tb.x:"<<t.b.x<<" tby:"<<t.b.y<<" sax:"<<s.a.x<<" say:"<<s.a.y<<" sbx:"<<s.b.x<<" sby:"<<s.b.y<<endl;
//    cout<<"t"<<endl;
    return false;
}
bool solve()
{
    if(dl.x<=a.x&&a.x<=ur.x&&dl.y<=a.y&&a.y<=ur.y) return true;
    if(dl.x<=b.x&&b.x<=ur.x&&dl.y<=b.y&&b.y<=ur.y) return true;
    if(isok(Line(a, b),Line(ul, ur))) return true;
    if(isok(Line(a, b),Line(ur, dr))) return true;
    if(isok(Line(a, b),Line(dr, dl))) return true;
    if(isok(Line(a, b),Line(dl, ul))) return true;
    return false;
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    LL x1, y1, x2, y2;
    int T;
    scanf("%d", &T);
    while(T--)
    {
        a.Inputs();
        b.Inputs();
        cin>>x1>>y1>>x2>>y2;
        dl = Point(min(x1, x2), min(y1, y2));
        ul = Point(min(x1, x2), max(y1, y2));
        dr = Point(max(x1, x2), min(y1, y2));
        ur = Point(max(x1, x2), max(y1, y2));
        printf("%s\n", solve()?"T":"F");
    }
    return 0;
}

Poj_1410 Intersection -线段类

标签:

原文地址:http://www.cnblogs.com/DarkTong/p/5767489.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!