hdu 1410(直线与矩形相交)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 13528 | Accepted: 3521 |
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
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
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 今天又懂了一个~~非规范相交.
规范相交模板(两条线段只有一个交点):
///叉积
double mult(Point a, Point b, Point c)
{
return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
} ///a, b为一条线段两端点c, d为另一条线段的两端点 相交返回true, 不相交返回false
bool isCross(Point a, Point b, Point c, Point d)
{
if (max(a.x,b.x)<min(c.x,d.x))return false;
if (max(a.y,b.y)<min(c.y,d.y))return false;
if (max(c.x,d.x)<min(a.x,b.x))return false;
if (max(c.y,d.y)<min(a.y,b.y))return false;
if (mult(c, b, a)*mult(b, d, a)<)return false;
if (mult(a, d, c)*mult(d, b, c)<)return false;
return true;
}
非规范相交模板(可以理解为重合)
const double eps = 1e-;
double cross(Point a,Point b,Point c){
return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
}
int dblcmp(double m) {
if (fabs(m) < eps) return ;
return m > ? : -;
}
bool isCross(Point a,Point b,Point c,Point d){
if (dblcmp(max(a.x, b.x)-min(c.x, d.x)) >= && dblcmp(max(c.x, d.x)-min(a.x, b.x)) >=
&& dblcmp(max(a.y, b.y)-min(c.y, d.y)) >= && dblcmp(max(c.y, d.y)-min(a.y, b.y)) >=
&& dblcmp(cross(a, d, c)*cross(b, d, c)) <= && dblcmp(cross(c, b, a)*cross(d, b, a)) <= )
return true;
return false;
}
题目很坑,有可能输入的不是左上角和右下角...
判断很简单,就是四条边都拿过去判断一下..然后判断一下线段是不是在矩形内。
///判断线段与矩形是否相交
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
const double eps = 1e-;
struct Point{
double x,y;
};
struct Line{
Point a,b;
}line[];
double cross(Point a,Point b,Point c){
return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
}
int dblcmp(double m) {
if (fabs(m) < eps) return ;
return m > ? : -;
}
bool isCross(Point a,Point b,Point c,Point d){
if (dblcmp(max(a.x, b.x)-min(c.x, d.x)) >= && dblcmp(max(c.x, d.x)-min(a.x, b.x)) >=
&& dblcmp(max(a.y, b.y)-min(c.y, d.y)) >= && dblcmp(max(c.y, d.y)-min(a.y, b.y)) >=
&& dblcmp(cross(a, d, c)*cross(b, d, c)) <= && dblcmp(cross(c, b, a)*cross(d, b, a)) <= )
return true;
return false;
}
int main()
{
int tcase;
scanf("%d",&tcase);
while(tcase--){
Line l;
double lx,ly,rx,ry;
scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&l.a.x,&l.a.y,&l.b.x,&l.b.y,&lx,&ly,&rx,&ry);
if(lx>rx) swap(lx,rx);
if(ly<ry) swap(ly,ry);
line[].a.x = lx,line[].a.y =ly,line[].b.x = rx,line[].b.y=ly;
line[].a.x = lx,line[].a.y =ly,line[].b.x = lx,line[].b.y=ry;
line[].a.x = lx,line[].a.y =ry,line[].b.x = rx,line[].b.y=ry;
line[].a.x = rx,line[].a.y =ly,line[].b.x = rx,line[].b.y=ry;
int flag = false;
for(int i=;i<=;i++){
if(isCross(l.a,l.b,line[i].a,line[i].b)){
flag = true;
break;
}
}
if(max(l.a.x,l.b.x)<rx&&min(l.a.x,l.b.x)>lx&&max(l.a.y,l.b.y)<ly&&min(l.a.y,l.b.y)>ry) flag = true;
if(flag) printf("T\n");
else printf("F\n");
}
return ;
}
hdu 1410(直线与矩形相交)的更多相关文章
- hdu 3304(直线与线段相交)
Segments Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12042 Accepted: 3808 Descrip ...
- 线段和矩形相交 POJ 1410
// 线段和矩形相交 POJ 1410 // #include <bits/stdc++.h> #include <iostream> #include <cstdio& ...
- poj 1410 Intersection (判断线段与矩形相交 判线段相交)
题目链接 Intersection Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12040 Accepted: 312 ...
- POJ 1039 Pipe(直线和线段相交判断,求交点)
Pipe Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8280 Accepted: 2483 Description ...
- C:矩形相交、相包含、相离关系判断
矩形相交 包含 问题.参考 假定矩形是用一对点表达的(minx, miny) (maxx, maxy),那么两个矩形 rect1{(minx1, miny1)(maxx1, maxy1)} ...
- 判断直线与线段相交 POJ 3304 Segments
题意:在二维平面中,给定一些线段,然后判断在某直线上的投影是否有公共点. 转化,既然是投影,那么就是求是否存在一条直线L和所有的线段都相交. 证明: 下面给出具体的分析:先考虑一个特殊的情况,即n=1 ...
- poj 3304(直线与线段相交)
传送门:Segments 题意:线段在一个直线上的摄影相交 求求是否存在一条直线,使所有线段到这条直线的投影至少有一个交点 分析:可以在共同投影处作原直线的垂线,则该垂线与所有线段都相交<==& ...
- POJ 3304 Segments[直线与线段相交]
Segments Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13514 Accepted: 4331 Descrip ...
- Codeforces Round #524 (Div. 2) C. Masha and two friends(矩形相交)
C. Masha and two friends time limit per test 1 second memory limit per test 256 megabytes input stan ...
随机推荐
- 浅析Python中的main函数
Python作为一门较为灵活的解释型脚本语言,其中定义的main()函数只有当该Python脚本直接作为执行程序时才会执行: 当该python脚本被作为模块(module)引入(import)时,其中 ...
- System and Device power management.
Advanced Configuration and Power Management Interface(ACPI)是由Intel,Microsoft等厂家订的一套Spec,规范了OS,APP对于电 ...
- el-checkbox根据是否被选中执行不同的操作
直接给el-checkbox绑定点击事件是没有效果的,因为它会被解析成其他形式的html,el-checkbox只是一个类名,因此,使用ts和jquery动态绑定事件: mounted() { $(& ...
- background 背景图铺满界面
background <body background="/image/1.png" style=" background-repeat:no-repeat ; b ...
- flask-sqlalchemy 用法总结
Flask-SQLAlchemy是一个Flask扩展,能够支持多种数据库后台,我们可以不需要关心SQL的处理细节,操作数据库,一个基本关系对应一个类,而一个实体对应类的实例对象.Flask是一个轻量级 ...
- Luogu 3435 POI2006OKR-Periods of Words(kmp)
显然答案应该是Σi-next[next[……next[i]]] (next[next[……next[i]]]>0).递推即可. #include<iostream> #include ...
- hdu 2874 Connections between cities (并查集+LCA)
Connections between cities Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- 如何在自家厨房里制作LSD
如何在自家厨房里制作LSD -------------------------------------------------------------------------------- D-麦角酸 ...
- ZOJ 3229 Shoot the Bullet | 有源汇可行流
题目: 射命丸文要给幻想乡的居民照相,共照n天m个人,每天射命丸文照相数不多于d个,且一个人n天一共被拍的照片不能少于g个,且每天可照的人有限制,且这些人今天照的相片必须在[l,r]以内,求是否有可行 ...
- Codeforces Round #520 (Div. 2) B. Math
B. Math time limit per test:1 second memory limit per test:256 megabytes Description: JATC's math te ...