J - Intersection
来源poj 1410
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
很坑,如果相交或者在矩形里面就是T,否者就是F;
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <iomanip>
#include<cmath>
#include<float.h>
#include<string.h>
#include<algorithm>
#define sf scanf
#define pf printf
#define mm(x,b) memset((x),(b),sizeof(x))
#include<vector>
#include<queue>
#include<stack>
#include<map>
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
typedef long long ll;
const ll mod=1e9+100;
const double eps=1e-8;
using namespace std;
const double pi=acos(-1.0);
const int inf=0xfffffff;
struct point
{
int x,y;
};
int direction(point p1,point p2,point p3)//p1是向量起点,p2是终点,p3是判断点,>0则在左边<0在右侧
{
return (p1.x-p3.x)*(p2.y-p3.y)-(p1.y-p3.y)*(p2.x-p3.x);
}
int main()
{
int n;
cin>>n;
while(n--)
{
point a[4],t1,t2;
int x1,x2,y1,y2;
sf("%d%d%d%d",&t1.x,&t1.y,&t2.x,&t2.y);
sf("%d%d%d%d",&x1,&y1,&x2,&y2);
if(x1>x2)
swap(x1,x2);
if(y1<y2)
swap(y1,y2);
a[0].x=x1;a[0].y=y1;
a[1].x=x1;a[1].y=y2;
a[2].x=x2;a[2].y=y2;
a[3].x=x2;a[3].y=y1;
if(t1.x>x1&&t2.x>x1&&t1.y<y1&&t2.y<y1&&t1.x<x2&&t2.x<x2&&t1.y>y2&&t2.y>y2)
pf("T\n");
else if((t1.x<x1&&t2.x<x1)||(t1.x>x2&&t2.x>x2)||(t1.y>y1&&t2.y>y1)||(t1.y<y2&&t2.y<y2))
pf("F\n");
else if((direction(t1,t2,a[0])>0&&direction(t1,t2,a[1])>0&&direction(t1,t2,a[2])>0&&direction(t1,t2,a[3])>0)||(direction(t1,t2,a[0])<0&&direction(t1,t2,a[1])<0&&direction(t1,t2,a[2])<0&&direction(t1,t2,a[3])<0))
pf("F\n");
else
pf("T\n");
}
return 0;
}
J - Intersection的更多相关文章
- 【BZOJ 1038】【ZJOI 2008】瞭望塔
http://www.lydsy.com/JudgeOnline/problem.php?id=1038 半平面交裸题,求完半平面后在折线段上的每个点竖直向上和半平面上的每个点竖直向下求距离,统计最小 ...
- Fishnet(暴力POJ 1408)
Fishnet Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 1911 Accepted: 1227 Descripti ...
- 用C#实现字符串相似度算法(编辑距离算法 Levenshtein Distance)
在搞验证码识别的时候需要比较字符代码的相似度用到"编辑距离算法",关于原理和C#实现做个记录. 据百度百科介绍: 编辑距离,又称Levenshtein距离(也叫做Edit Dist ...
- pthon/零起点(一、集合)
pthon/零起点(一.集合) set( )集合,集合是无序的,集合是可变的,集合是可迭代的 set()强型转成集合数据类型 set()集合本身就是去掉重复的元素 集合更新操作案列: j={1,2,3 ...
- [猜你喜欢]冠军“yes,boy!”分享,含竞赛源代
[猜你喜欢]冠军“yes,boy!”分享,含竞赛源代码 DataCastle运营 发表于 2016-7-20 17:31:52 844 3 5 我是Yes,boy! ,来自东北大学计算 ...
- java csv list cant not repeat
require: /** * before: * file A1.csv {1,2,3,4,5} * file A2.csv {2,3,9,10,11} * file B1.csv {5,12,13, ...
- Solution of NumberOfDiscIntersections by Codility
question:https://codility.com/programmers/lessons/4 this question is seem like line intersections qu ...
- 几何问题 poj 1408
参考博客: 用向量积求线段焦点证明: 首先,我们设 (AD向量 × AC向量) 为 multi(ADC) : 那么 S三角形ADC = multi(ADC)/2 . 由三角形DPD1 与 三角形CPC ...
- POJ 1408:Fishnet
Fishnet Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 1921 Accepted: 1234 Descripti ...
随机推荐
- apache2.4多站点配置
原来是跑单站,现在想跑多站,配置不算复杂,记录一下: 用默认的httpd.conf修改,去掉两个vhost的注释 servername指定任意一个合法的域名 如果是python,配置wsgi 修改ex ...
- OpenSUSE 服务器系统部署
1.准备 1.1 下载系统 下载地址:https://software.opensuse.org/distributions/leap 目前的最新版本为leap,推荐使用种子下载速度较快. 1.2 配 ...
- mac上配置mysql与redis server,并结合Pydev准备某爬虫环境
mysql下安装mysql server mysql下安装redis server:https://www.jianshu.com/p/3bdfda703552 mac下安装配置redis:https ...
- 调用 setState 之后发生了什么?
(1)代码中调用 setState 函数之后,React 会将传入的参数对象与组件当前的状态合并,然后触发所谓的调和过程(Reconciliation).(2)经过调和过程,React 会以相对高效的 ...
- Vue加载组件、动态加载组件的几种方式
https://cn.vuejs.org/v2/guide/components.html https://cn.vuejs.org/v2/guide/components-dynamic-async ...
- PEM文件
原文链接: http://blog.sina.com.cn/s/blog_489f88710100a59w.html OpenSSL 使用 PEM 文件格式存储证书和密钥.PEM 实质上是 Base6 ...
- 6.翻译系列:EF 6 Code-First中数据库初始化策略(EF 6 Code-First系列)
原文链接:http://www.entityframeworktutorial.net/code-first/database-initialization-strategy-in-code-firs ...
- python 列表排序方法sort、sorted技巧篇
Python list内置sort()方法用来排序,也可以用python内置的全局sorted()方法来对可迭代的序列排序生成新的序列. 1)排序基础 简单的升序排序是非常容易的.只需要调用sorte ...
- GC调优在Spark应用中的实践[转]
作者:仲浩 出处:<程序员>电子刊5月B 摘要:Spark立足内存计算,常常需要在内存中存放大量数据,因此也更依赖JVM的垃圾回收机制.与此同时,它也兼容批处理和流式处理,对于程序 ...
- BitSet的用法
1,BitSet类 大小可动态改变, 取值为true或false的位集合.用于表示一组布尔标志. 此类实现了一个按需增长的位向量.位 set 的每个组件都有一个 boolean 值.用非负的整数 ...