POJ 1410--Intersection(判断线段和矩形相交)
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 16322 | Accepted: 4213 |
Description
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
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
Sample Input
1
4 9 11 2 1 5 7 1
Sample Output
F
Source
- 题目的判断是否一条线段和矩形相交,可以想到直接判断给定线段是否和矩形的四条边相交即可,但是有一个问题,题目定义的矩形"The rectangle consists of four straight lines and the area in between",包括了其中的面积,就因为这个wa了几发Orz,我的等级还是不够啊。
- 最后只要判断给定线段是否和矩形的四条边相交,以及线段是否在矩形内,线段是否在矩形内部可以用线段的端点是否在矩形内部来判断。
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int n;
double xs, ys, xe, ye, xl, yl, xr, yr;
const double eps = 1.0e8;
typedef struct point {
double x;
double y;
point(double a, double b) {
x = a;
y = b;
}
point() { }
}point;
typedef struct edge {
point start;
point end;
edge(point a, point b) {
start = a;
end = b;
}
edge() { }
edge(edge &t) {
start = t.start;
end = t.end;
}
}edge;
point t[];
edge line;
edge rec[]; inline double dabs(double a) { return a < ? -a : a; }
inline double max(double a, double b) { return a > b ? a : b; }
inline double min(double a, double b) { return a < b ? a : b; }
double multi(point p1, point p2, point p0) {
return (p2.y - p0.y)*(p1.x - p0.x) - (p2.x - p0.x)*(p1.y - p0.y);
}
bool Across(edge v1, edge v2) {
if (max(v1.start.x, v1.end.x) >= min(v2.start.x, v2.end.x) &&
max(v1.start.y, v1.end.y) >= min(v2.start.y, v2.end.y) &&
max(v2.start.x, v2.end.x) >= min(v1.start.x, v1.end.x) &&
max(v2.start.y, v2.end.y) >= min(v1.start.y, v1.end.y) &&
multi(v2.start, v1.end, v1.start)*multi(v1.end, v2.end, v2.start) >= &&
multi(v1.start, v2.end, v2.start)*multi(v2.end, v1.end, v1.start) >=
)
return true;
return false;
}
int main(void) {
while (cin >> n) {
while (n-- > ) {
int flag = ;
cin >> xs >> ys >> xe >> ye >> xl >> yl >> xr >> yr;
line = edge(point(xs, ys), point(xe, ye));
t[] = point(xl, yl), t[] = point(xr, yl);
t[] = point(xr, yr), t[] = point(xl, yr);
for (int i = ; i < ; i++) {
rec[i] = edge(t[i], t[(i + )%]);
}
for (int i = ; i < ; i++) {
if (Across(line, rec[i]))
{
flag = ;
break;
}
}
if(line.start.x>=min(xl,xr)&&line.start.x<=max(xr,xl)&&line.start.y>=min(yl,yr)&&line.start.y<=max(yl,yr) ||
line.end.x >= min(xl, xr) && line.end.x <= max(xr, xl) && line.end.y >= min(yl, yr) && line.end.y <= max(yl, yr))
flag = ;//判断是否点在矩形内部
if (flag == )
cout << "T" << endl;
else
cout << "F" << endl;
}
}
return ;
}
POJ 1410--Intersection(判断线段和矩形相交)的更多相关文章
- poj 1410 Intersection (判断线段与矩形相交 判线段相交)
题目链接 Intersection Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12040 Accepted: 312 ...
- POJ 1410 Intersection (线段和矩形相交)
题目: Description You are to write a program that has to decide whether a given line segment intersect ...
- [POJ 1410] Intersection(线段与矩形交)
题目链接:http://poj.org/problem?id=1410 Intersection Time Limit: 1000MS Memory Limit: 10000K Total Sub ...
- POJ 1410 Intersection(线段相交&&推断点在矩形内&&坑爹)
Intersection 大意:给你一条线段,给你一个矩形,问是否相交. 相交:线段全然在矩形内部算相交:线段与矩形随意一条边不规范相交算相交. 思路:知道详细的相交规则之后题事实上是不难的,可是还有 ...
- 线段和矩形相交 POJ 1410
// 线段和矩形相交 POJ 1410 // #include <bits/stdc++.h> #include <iostream> #include <cstdio& ...
- poj 1410 Intersection 线段相交
题目链接 题意 判断线段和矩形是否有交点(矩形的范围是四条边及内部). 思路 判断线段和矩形的四条边有无交点 && 线段是否在矩形内. 注意第二个条件. Code #include & ...
- 判断线段和直线相交 POJ 3304
// 判断线段和直线相交 POJ 3304 // 思路: // 如果存在一条直线和所有线段相交,那么平移该直线一定可以经过线段上任意两个点,并且和所有线段相交. #include <cstdio ...
- poj1410(判断线段和矩形是否相交)
题目链接:https://vjudge.net/problem/POJ-1410 题意:判断线段和矩形是否相交. 思路:注意这里的相交包括线段在矩形内,因此先判断线段与矩形的边是否相交,再判断线段的两 ...
- Intersection--poj1410(判断线段与矩形的关系)
http://poj.org/problem?id=1410 题目大意:给你一个线段和矩形的对角两点 如果相交就输出'T' 不想交就是'F' 注意: 1,给的矩形有可能不是左上 右下 所以要先判 ...
随机推荐
- 《Java开发实战经典》读书笔记
Java常用的内存区域: (1) 栈内存空间:保存所有的对象名称. (2) 堆内存空间:保存每个对象的具体属性内容. (3) 全局数据区:保存static类型的属性. ( ...
- ACM-线段树区间更新+离散化
区间更新与单点更新最大的不同就在于Lazy思想: http://blog.sina.com.cn/s/blog_a2dce6b30101l8bi.html 可以看这篇文章,讲得比较清楚 在具体使用上, ...
- 序列化及json&pickle的使用
一.序列化 序列化是指把内存里的数据类型转变成字符串.以使其能存储到硬盘或通过网络传输到远程.——硬盘或网络传输时只能接受bytes. Python中用于序列化的两个模块: json:用于字符串和Py ...
- arcgis server 10.1 发布动态图层展示海量及频繁更新的数据步骤
Arcgis server 发布动态图层及调用动态图层 做这个动态图层功能的原由是 有一个30万的数据需要通过arcgis GP工具转成shp然后渲染加载进地图,原来的做法是遍历生成shp面要素,读 ...
- 03_CronTrigger
[Cron表达式] Quartz使用类似于Linux下的Cron表达式定义的时间规则,Cron表达式由6到7个空格分隔的时间字段组成. [ 字符说明 ] * :可以用在所有字段中,表示对应时间域内的每 ...
- Android FlycoDialog 简单实用的自定义Android弹窗对话框之Dialog篇
效果图镇楼 FlycoDialog是一款非常棒的弹窗对话框处理框架,今天在这里主要讲一下他的自定义弹出对话框的功能,这里以第二幅效果图为例,图片已经放在博客最下方,X号自己随便找一个东西代替吧. ...
- 某虚拟定位APP从破解到原理分析
工具环境ida7.0iphone 6ios 10.2 0x00:基本情况 1. 该app可以修改模拟手机地理位置(gps.基站.WIFI),拥有全局定位.指定应用定位.模拟扫街等功能,只能在已越狱的I ...
- 夜色的 cocos2d-x 开发笔记 01
现在我们来实现在屏幕上出现一只飞机的效果. 首先我们要建立一个场景,显示在屏幕上,创建一个类,RunScence,现在你的项目目录应该是这个样子的. 之前没学过C++,.h文件我理解就是一个声明文件, ...
- dwr2.0版本的demo
谈起DWR,这个东西在上学的时候接触过,但工作之后就再也没有用过. 对DWR的印象是不手写AJAX,使用JavaScript调用java后台的代码,就如同调用前台代码一样. ...
- Twitter Typeahead plugin Example
原文网址: http://dhtmlexamples.com/2012/02/27/using-the-typeahead-plugin-with-twitter-bootstrap-2-and-jq ...