poj 1410 Intersection(判矩形和线段相交。。细节多)

news/2024/7/10 23:08:06 标签: struct, c, blog
cle class="baidu_pl">
cle_content" class="article_content clearfix">
content_views" class="htmledit_views">

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

【题目大意】:给出一条线段和一个矩形࿰c;判断线段和矩形是否相交。

 

【解题思路】:判给出的线段与矩形的每条线段是否相交(包括相交和非规范相交)。

1、内含也算相交(WA一次)       2、看了discuss࿰c;发现题目给出矩形的点并不一定是左上角的点和右下角的点(WA2次)

 

【代码】:

<code class="language-cpp">#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

class="tags" href="/tags/STRUCT.html" title=struct>struct Point
{
    double x, y;
    Point() {}
    Point(double a, double b)
    {
        x = a, y = b;
    }
}point[20];

class="tags" href="/tags/STRUCT.html" title=struct>struct Line
{
    Point a, b;
    Line() {}
    Line(Point x, Point y)
    {
        a = x, b = y;
    }
}line,line1[20];

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 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 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 int between(Point o, Point a, Point b)
{
    return sig(dot(o, a, b));
}

inline int intersect1(Point a, Point b, Point c, Point d) {
    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)
    {
        return 1;
    }
    if (d1 == 0 && between(c, a, b) <= 0) return 2;
    if (d2 == 0 && between(d, a, b) <= 0) return 2;
    if (d3 == 0 && between(a, c, d) <= 0) return 2;
    if (d4 == 0 && between(b, c, d) <= 0) return 2;
    return 0;
}

inline int intersect(Line u, Line v)
{
    return intersect1(u.a, u.b, v.a, v.b);
}

int main()
{
    int T;
    double p,q,m,n,p1,q1,m1,n1;
    scanf("%d",&T);
    while (T--)
    {
        scanf("%lf%lf",&p1,&q1);

        scanf("%lf%lf",&m1,&n1);
        point[1]=Point(m1,n1);
        point[0]=Point(p1,q1);
        line=Line(point[0],point[1]);
        scanf("%lf%lf%lf%lf",&p,&q,&m,&n);
        if (p>m && q<n)
        {
            swap(p,m);
            swap(q,n);
        }
        else
        if (p>m && q>n) swap(m,p);
        else
        if (p<m && q<n) swap(q,n);
        if ((p1>=p && p1<=m && q1<=q && q1>=n && m1>=p && m1<=m && n1<=q && n1>=n))
        {
            printf("T\n");
            continue;
        }
        point[2]=Point(p,q);
        point[3]=Point(m,q);
        point[4]=Point(p,n);
        point[5]=Point(m,n);
        line1[0]=Line(point[2],point[3]);
        line1[1]=Line(point[2],point[4]);
        line1[2]=Line(point[3],point[5]);
        line1[3]=Line(point[4],point[5]);
        int k1,k2,k3,k4;
        k1=intersect(line,line1[0]);
        k2=intersect(line,line1[1]);
        k3=intersect(line,line1[2]);
        k4=intersect(line,line1[3]);
        //cout << k1 << " " << k2 << " " << k3 << " " << k4 << endl;
        if (k1==0 && k2==0 && k3==0 && k4==0)
        {
            printf("F\n");
        }
        else printf("T\n");

    }
    return 0;
}
code>


cle>

http://www.niftyadmin.cn/n/971078.html

相关文章

基于弹性堆栈(ELK堆栈)的日志分析、存储及展示

ELK简介 “ELK”是三个开源项目的首字母缩写&#xff1a;Elasticsearch&#xff0c;Logstash和Kibana。Elasticsearch是一个搜索和分析引擎。Logstash是一个服务器端数据处理管道&#xff0c;它同时从多个源中提取数据&#xff0c;对其进行转换&#xff0c;然后将其发送到像Ela…

SDAU暑假训练第15天----------做题(2018/8/13)

上午做了几个题&#xff0c;做起来有困难&#xff0c;一个题改好多次&#xff0c;可能还是不够熟练吧。 下午打了场比赛&#xff0c;比赛题变难了&#xff08;据说选自DIV2&#xff09; 待会选几个题把题解发上来吧&#xff0c;虽然好多网上都有&#xff0c;但是下午的收获还…

【字符串】Segment Occurrences

Codeforces Round 48 (Rated for Div. 2) 大意&#xff1a;输入字符串s和t、各自的长度和q个询问&#xff0c;询问由第l到r个字母构成的s的子串中&#xff0c;包含t的个数 一开始的傻屌代码&#xff0c;TLE上天了 #include <iostream> #include <string> #include…

小菜鸡进阶之路_Second week之元组、列表、集合、字典对比.

这一周主要学习数据类型,要记的东西特别多,特别是元组、列表、集合、字典这一部分有很多地方相似而不相同,特别容易混淆.所以我做了一个简单的汇总, 希望大家批评指正. 转载于:https://www.cnblogs.com/huge-666/p/9521293.html

二维背包

第五讲 二维费用的背包问题 问题 二维费用的背包问题是指&#xff1a;对于每件物品&#xff0c;具有两种不同的费用&#xff1b;选择这件物品必须同时付出这两种代价&#xff1b;对于每种代价都有一个可付出的最大值&#xff08;背包容量&#xff09;。问怎样选择物品可以得到最…

Oracle 9i安装图解

需要注意的一个问题Oracle的安装路径不能是中文 转载于:https://www.cnblogs.com/buzaixian/archive/2009/09/20/1570330.html

poj 1696 Space Ant(叉积的性质,做极角排序)

//以下为原blog搬迁过来的内容 【题目大意】&#xff1a;有一只M11&#xff0c;它每天必须靠走路到某些点吃植物维系生命&#xff0c;但是由于身体条件的限制&#xff0c;它不能向右转&#xff0c;也不能走交叉的路&#xff0c;问你怎么走能吃到最多的路径。&#xff08;起点是…

四个提升服务器数据安全的方法

四个提升服务器数据安全的方法 方法一&#xff1a;定期备份数据 数据备份的意义就在于&#xff0c;当受到网络攻击、病毒入侵、电源故障或者操作失误等事故的发生后&#xff0c;可以完整、快速、简捷、可靠地恢复原有系统&#xff0c;在一定的范围内保障系统的正常运行。一些对…