题目链接:POJ 1410

Description

You are to write a program that has to decide whether a given line segment intersects a given rectangle.

An example:

line: start point: (4,9)

end point: (11,2)

rectangle: left-top: (1,5)

right-bottom: (7,1)

Figure 1: Line segment does not intersect rectangle

The line is said to intersect the rectangle if the line and the rectangle have at least one point in common. The rectangle consists of four straight lines and the area in between. Although all input values are integer numbers, valid intersection points do not have to lay on the integer grid.

Input

The input consists of n test cases. The first line of the input file contains the number n. Each following line contains one test case of the format:

xstart ystart xend yend xleft ytop xright ybottom

where (xstart, ystart) is the start and (xend, yend) the end point of the line and (xleft, ytop) the top left and (xright, ybottom) the bottom right corner of the rectangle. The eight numbers are separated by a blank. The terms top left and bottom right do not imply any ordering of coordinates.

Output

For each test case in the input file, the output file should contain a line consisting either of the letter "T" if the line segment intersects the rectangle or the letter "F" if the line segment does not intersect the rectangle.

Sample Input

1
4 9 11 2 1 5 7 1

Sample Output

F

Source

Southwestern European Regional Contest 1995

Solution

题意

给定一个矩形和一条线段,判断线段是否与矩形相交或者在矩形内部。

思路

判断线段是否与矩形每条边相交。至于线段是否在矩形内,判断是否线段两个端点在矩形内即可。

计算几何模板来在 kuangbin 的模板。

Code

#include <cstdio>
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef double db;
const db eps = 1e-10;
const db pi = acos(-1.0);
const ll inf = 0x3f3f3f3f3f3f3f3f;
const ll maxn = 1e5 + 10; inline int sgn(db x) {
if(fabs(x) < eps) return 0;
return x > 0? 1: -1;
} class Point {
public:
double x, y;
Point(double x = 0, double y = 0) : x(x), y(y) {}
void input() {
scanf("%lf%lf", &x, &y);
}
bool operator ==(Point b) const {
return sgn(x-b.x) == 0 && sgn(y-b.y) == 0;
}
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;
}
//点积
double operator *(const Point &b) const {
return x*b.x + y*b.y;
}
}; class Line {
public:
Point s, e;
db angle;
Line() {}
Line(Point s, Point e) : s(s), e(e) {}
inline void input() {
scanf("%lf%lf%lf%lf", &s.x, &s.y, &e.x, &e.y);
}
//`两线段相交判断`
//`2 规范相交`
//`1 非规范相交`
//`0 不相交`
int segcrossseg(Line v){
int d1 = sgn((e - s) ^ (v.s - s));
int d2 = sgn((e - s) ^ (v.e - s));
int d3 = sgn((v.e - v.s) ^ (s - v.s));
int d4 = sgn((v.e - v.s) ^ (e - v.s));
if( (d1 ^ d2) == -2 && (d3 ^ d4) == -2 ) return 2;
return (d1 == 0 && sgn((v.s - s)*(v.s - e)) <= 0) ||
(d2 == 0 && sgn((v.e - s)*(v.e - e)) <= 0) ||
(d3 == 0 && sgn((s - v.s) * (s - v.e)) <= 0) ||
(d4 == 0 && sgn((e - v.s) * (e - v.e)) <= 0);
}
// 点在线段上的判断
bool pointonseg(Point p) {
return sgn((p - s) ^ (e - s)) == 0 && sgn((p - s) * (p - e)) <= 0;
}
}; struct Rec {
const static int n = 4;
Point p[4];
Line l[4];
void getline(){
for(int i = 0; i < n; ++i) {
l[i] = Line(p[i], p[(i + 1) % n]);
}
}
//`判断点和任意多边形的关系`
//` 3 点上`
//` 2 边上`
//` 1 内部`
//` 0 外部`
int relationpoint(Point q) {
for(int i = 0; i < n; ++i) {
if(p[i] == q) return 3;
}
getline();
for(int i = 0; i < n; ++i) {
if(l[i].pointonseg(q)) return 2;
}
int cnt = 0;
for(int i = 0; i < n; ++i) {
int j = (i + 1) % n;
int k = sgn((q - p[j])^(p[i] - p[j]));
int u = sgn(p[i].y - q.y);
int v = sgn(p[j].y - q.y);
if(k > 0 && u < 0 && v >= 0) cnt++;
if(k < 0 && v < 0 && u >= 0) cnt--;
}
return cnt != 0;
}
}; int main() {
int T;
scanf("%d", &T);
while(T--) {
Point a, b;
a.input(), b.input();
Line l = Line(a, b);
Rec rec;
a.input(), b.input();
rec.p[0] = Point(min(a.x, b.x), min(a.y, b.y));
rec.p[1] = Point(max(a.x, b.x), min(a.y, b.y));
rec.p[2] = Point(max(a.x, b.x), max(a.y, b.y));
rec.p[3] = Point(min(a.x, b.x), max(a.y, b.y));
if(l.segcrossseg(Line(rec.p[0], rec.p[1]))) {
printf("T\n");
continue;
}
if(l.segcrossseg(Line(rec.p[1], rec.p[2]))) {
printf("T\n");
continue;
}
if(l.segcrossseg(Line(rec.p[2], rec.p[3]))) {
printf("T\n");
continue;
}
if(l.segcrossseg(Line(rec.p[3], rec.p[0]))) {
printf("T\n");
continue;
}
if(rec.relationpoint(l.s) || rec.relationpoint(l.e)) {
printf("T\n");
continue;
}
printf("F\n");
}
return 0;
}

POJ 1410 Intersection (计算几何)的更多相关文章

  1. [POJ 1410] Intersection(线段与矩形交)

    题目链接:http://poj.org/problem?id=1410 Intersection Time Limit: 1000MS   Memory Limit: 10000K Total Sub ...

  2. POJ 1410 Intersection(计算几何)

    题目大意:题目意思很简单,就是说有一个矩阵是实心的,给出一条线段,问线段和矩阵是否相交解题思路:用到了线段与线段是否交叉,然后再判断线段是否在矩阵里面,这里要注意的是,他给出的矩阵的坐标明显不是左上和 ...

  3. POJ 1410 Intersection(判断线段交和点在矩形内)

    Intersection Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9996   Accepted: 2632 Desc ...

  4. POJ 1410 Intersection(线段相交&amp;&amp;推断点在矩形内&amp;&amp;坑爹)

    Intersection 大意:给你一条线段,给你一个矩形,问是否相交. 相交:线段全然在矩形内部算相交:线段与矩形随意一条边不规范相交算相交. 思路:知道详细的相交规则之后题事实上是不难的,可是还有 ...

  5. POJ 1410 Intersection (线段和矩形相交)

    题目: Description You are to write a program that has to decide whether a given line segment intersect ...

  6. poj 1410 Intersection (判断线段与矩形相交 判线段相交)

    题目链接 Intersection Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12040   Accepted: 312 ...

  7. POJ 1410 Intersection --几何,线段相交

    题意: 给一条线段,和一个矩形,问线段是否与矩形相交或在矩形内. 解法: 判断是否在矩形内,如果不在,判断与四条边是否相交即可.这题让我发现自己的线段相交函数有错误的地方,原来我写的线段相交函数就是单 ...

  8. 简单几何(线段相交) POJ 1410 Intersection

    题目传送门 题意:一个矩形和一条线段,问是否有相交 分析:考虑各种情况.坑点:给出的矩形的两个端点是无序的,还有线段完全在矩形内也算相交 /****************************** ...

  9. POJ 1410 Intersection 数据错误

    题目要求判断一条线段和一个矩形是否相交,或者是否在矩形里面(题目好像没说?) 思路就是直接暴力判断和矩形四条边是否相交,和线段的坐标是否在矩形的坐标范围即可. 然后题目的数据,(xleft,ytop) ...

随机推荐

  1. Mybatis-技术专区-中的条件查询createCriteria example里面的条件

    之前用Mybatis框架反向的实体,还有实体里面的Example,之前只是知道Example里面放的是条件查询的方法,可以一直不知道怎么用,到今天才开始知道怎么简单的用. 在我们前台查询的时候会有许多 ...

  2. vue.js(8)--v-for的使用

    v-for遍历数组.对象数组.对象.迭代次数 <!DOCTYPE html> <html lang="en"> <head> <meta ...

  3. JS书目推荐(私教推荐)

    下面几本书是私教推荐的,从入门到提高,从易到难,想找电子版的可以去下面这个网站找找,挺多书籍的 鸠摩搜书https://www.jiumodiary.com/ JavaScript编程精解 (第二版) ...

  4. 关于Object.create方法

    ES6最新的Object.create语法是 创造一个对象 可以传参,参数为一个对象,得到的结果是一个克隆的对象, 实际上 这是基于原型的克隆 分析如下: var a={b:1}; var a1 = ...

  5. tensorflow的reshape操作tf.reshape()

    在处理图像数据的时候总会遇到输入图像的维数不符合的情况,此时tensorflow中reshape()就很好的解决了这个问题. 更为详细的可以参考官方文档说明: numpy.reshape reshap ...

  6. 295-Xilinx Kintex-7 X7K325T的半高PCIe x4双路万兆光纤收发卡

    Xilinx Kintex-7 X7K325T的半高PCIe x4双路万兆光纤收发卡 一.板卡概述       本板卡系我公司自主研发,采用Xilinx公司的XC7K325T-2FFG676I芯片作为 ...

  7. Shell04--循环语句

    目录 Shell04---循环语句 1. 循环语句for基本概述 2. 循环语句for场景示例 3. 循环语句while基本概述 4. 循环语句while场景示例 5. 内置跳出循环语句指令 Shel ...

  8. DNS域名系统,简述工作原理

    DNS工作原理: 当DNS客户端需要在程序中使用名称时,它会查询DNS服务器来解析该名称.客户端发送的每条查询信息包括三条信息:指定的DNS域名,指定的查询类型,DNS域名的指定类别.基于UDP服务, ...

  9. 解释ARP协议和RARP协议

    解释ARP(地址解析协议) 首先,每个主机都会在自己的ARP缓冲区中建立一个ARP列表,以表示IP地址和MAC地址之间的对应关系. 当源主机要发送数据时,首先检查ARP列表中是否有对应IP地址的目的主 ...

  10. 同步mysql

    ElasticSearch同步MySql 标签: elasticsearchmysql 2016-07-01 09:07 4636人阅读 评论(8) 收藏 举报  分类: Elasticsearch( ...