Intersection (计算几何)
来源:互联网 发布:淘宝网店域名怎么改 编辑:程序博客网 时间:2024/05/21 00:54
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.
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.
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.
简单说就是先判断线段是不是在矩形内,再分别判断和四条边是否相交...
判断相交貌似一直是比较棘手的问题啊,不过这种线线相交还好...
#include<cstdio>#include<cstring>#include<cmath>#define eps 1e-8#define zero(x) (((x)>0?(x):-(x))<eps)struct point{ double x, y;};double xmult(point p1, point p2, point p0){ return (p1.x - p0.x)*(p2.y - p0.y) - (p2.x - p0.x)*(p1.y - p0.y);}double dmult(point p1, point p2, point p0){ return (p1.x - p0.x)*(p2.x - p0.x) + (p1.y - p0.y)*(p2.y - p0.y);}int dots_inline(point p1, point p2, point p3){ return zero(xmult(p1, p2, p3));}int dot_online_in(point p, point l1, point l2){ return zero(xmult(p, l1, l2)) && (l1.x - p.x)*(l2.x - p.x)<eps && (l1.y - p.y)*(l2.y - p.y)<eps;}int same_side(point p1, point p2, point l1, point l2){ return xmult(l1, p1, l2)*xmult(l1, p2, l2)>eps;}int intersect_in(point u1, point u2, point v1, point v2){ if (!dots_inline(u1, u2, v1) || !dots_inline(u1, u2, v2)) return !same_side(u1, u2, v1, v2) && !same_side(v1, v2, u1, u2); return dot_online_in(u1, v1, v2) || dot_online_in(u2, v1, v2) || dot_online_in(v1, u1, u2) || dot_online_in(v2, u1, u2);}int main(){ point x, y; point d1, d2, d3, d4; int cas, i, j, k; scanf("%d", &cas); while (cas--) { scanf("%lf%lf%lf%lf%lf%lf%lf%lf", &x.x, &x.y, &y.x, &y.y, &d4.x, &d4.y, &d2.x, &d2.y); d1.x = d4.x, d1.y = d2.y; d3.x = d2.x, d3.y = d4.y; if ((x.x >= d2.x&&x.x <= d4.x) || (x.x >= d4.x&&x.x <= d2.x)) if ((x.y >= d2.y&&x.y <= d4.y) || (x.y >= d4.y&&x.y <= d2.y)) { printf("T\n"); continue; } if ((y.x >= d2.x&&y.x <= d4.x) || (y.x >= d4.x&&y.x <= d2.x)) if ((y.y >= d2.y&&y.y <= d4.y) || (y.y >= d4.y&&y.y <= d2.y)) { printf("T\n"); continue; } if (intersect_in(x, y, d1, d2) || intersect_in(x, y, d3, d2) || intersect_in(x, y, d3, d4) || intersect_in(x, y, d1, d4)) printf("T\n"); else printf("F\n"); } return 0;}
0 0
- Intersection (计算几何)
- [POJ1410]Intersection(计算几何)
- poj 1410 Intersection(计算几何)
- POJ 1410 Intersection(计算几何)
- HDU 5120 Intersection (计算几何)
- HDU 5120 Intersection(计算几何)
- POJ410 Intersection 计算几何
- HDU 5120 Intersection(计算几何+容斥)
- light oj 1211 - Intersection of Cubes (计算几何)
- POJ 1410 Intersection <计算几何(线段相交判断)>
- 几何 两元相交的面积计算问题 HDU 5120 Intersection(几何模板)
- HDU 5120 Intersection (计算几何)2014ICPC 北京站现场赛
- hdoj Intersection 5120 (数学计算几何) 求两个相交圆的面积
- [CG]Intersection of Line Segments(0163)(计算几何,求线段是否相交)
- cf#21-B-Intersection-计算几何-求直线交点个数
- POJ 1410Intersection 计算几何 判断线段与矩形位置
- hdu 5120 Intersection(几何 容斥)
- uva 10321 - Polygon Intersection(几何)
- 【Android】检测app是否安装、安装并打开的方法
- 设计模式_建造者模式
- Myeclipse验证js语法报错
- POJ 2328 Guessing Game
- top 25 论文
- Intersection (计算几何)
- 解题报告: 商品推荐走马灯
- log4j:WARN Please initialize the log4j system properly解决办法
- div、css入门练习
- CodeForces 402A
- FZU2184 逆序数还原
- 每天学习一点新东西——vim篇(二)
- Enum类型
- Zookeeper(一)安装