uva191 Intersection
来源:互联网 发布:苹果的mac pro好么 编辑:程序博客网 时间:2024/04/30 14:08
Description
Intersection
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: 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:
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 and 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
14 9 11 2 1 5 7 1
Sample Output
F
坑点在于,尼玛右上角不是右上角,左下角不是左下角,QNMLGB
#define DeBUG#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>#include <vector>#include <stack>#include <queue>#include <string>#include <set>#include <sstream>#include <map>#include <bitset>using namespace std ;#define zero {0}#define INF 0x3f3f3f3f#define EPS 1e-9typedef long long LL;const double PI = acos(-1.0);//#pragma comment(linker, "/STACK:102400000,102400000")inline int sgn(double x){ return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);}#define N 100005struct Point{ double x, y; Point () {} Point(double a, double b) { x = a; y = b; }};typedef Point Vec;Vec operator - (Vec a, Vec b)//点减法{ return Vec(a.x - b.x, a.y - b.y);}bool operator == (Point a, Point b)//点相等判断{ return sgn(a.x - b.x) == 0 && sgn(a.y - b.y) == 0;}inline double crossDet(Vec a, Vec b)//叉乘{ return a.x * b.y - a.y * b.x;}inline double dotDet(Vec a, Vec b)//点乘{ return a.x * b.x + a.y * b.y;}inline bool onSeg(Point x, Point a, Point b)//判断点在线段ab上,加上||x==a||x==b在端点也算{ return sgn(crossDet(a - x, b - x)) == 0 && sgn(dotDet(a - x, b - x)) < 0 || x == a || x == b;}// 0 : not intersect// 1 : proper intersect// 2 : improper intersectint segIntersect(Point a, Point c, Point b, Point d){ Vec v1 = b - a, v2 = c - b, v3 = d - c, v4 = a - d; int a_bc = sgn(crossDet(v1, v2)); int b_cd = sgn(crossDet(v2, v3)); int c_da = sgn(crossDet(v3, v4)); int d_ab = sgn(crossDet(v4, v1)); if (a_bc * c_da > 0 && b_cd * d_ab > 0) return 1; if (onSeg(b, a, c) && c_da) return 2; if (onSeg(c, b, d) && d_ab) return 2; if (onSeg(d, c, a) && a_bc) return 2; if (onSeg(a, d, b) && b_cd) return 2; return 0;}bool inrec(Point a, Point tl, Point br){ if (a.x >= tl.x && a.x <= br.x && a.y <= tl.y && a.y >= br.y) return true; return false;}int main(){#ifdef DeBUGs freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);#endif int T; scanf("%d", &T); while (T--) { Point topleft; Point bottomright; Point start, end; scanf("%lf%lf%lf%lf%lf%lf%lf%lf", &start.x, &start.y, &end.x, &end.y, &topleft.x, &topleft.y, &bottomright.x, &bottomright.y); if(topleft.x>bottomright.x) swap(topleft.x,bottomright.x); if(topleft.y<bottomright.y) swap(topleft.y,bottomright.y); Point p[5]; p[0].x = topleft.x; p[0].y = topleft.y; p[1].x = bottomright.x; p[1].y = topleft.y; p[2].x = bottomright.x; p[2].y = bottomright.y; p[3].x = topleft.x; p[3].y = bottomright.y; p[4] = p[0]; bool flag = false; for (int i = 0; i < 4; i++) { if (segIntersect(p[i], p[i + 1], start, end) != 0) { flag = true; } } if (inrec(start, topleft, bottomright) || inrec(end, topleft, bottomright)) flag = true; if (flag) printf("T\n"); else printf("F\n"); } return 0;}
- uva191 Intersection
- uva191 Intersection
- uva191 Intersection(线段之间相交)
- Intersection
- Intersection
- Intersection
- Intersection
- Intersection graph & Intersection number
- poj1410 - Intersection
- 18:Intersection
- POJ1410 Intersection
- poj1410 Intersection
- 1879: Intersection
- poj1410 Intersection
- intersection set
- poj1410 Intersection
- ZOJ3862:Intersection
- HDU_5120 Intersection
- git 错误操作恢复
- 实现在同一方法中获取当前方法中新赋值的session值解决方法
- 复制用户设置
- 硬件常识:移动硬盘
- 原博客地址
- uva191 Intersection
- 网页设计中7个令人抓狂的错误是什么?
- 设置颜色
- 机试-cdkey
- 虚拟机驱动调试设置
- Linux驱动程序接口
- 输出移动小人的程序
- 设置TextView控件的背景透明度和字体透明度
- bt采集蜘蛛,开源版