POJ 1410 Intersection(线段相交&&判断点在矩形内&&坑爹)

Intersection

大意:给你一条线段,给你一个矩形,问是否相交。

    相交:线段完全在矩形内部算相交;线段与矩形任意一条边不规范相交算相交。

思路:知道具体的相交规则之后题其实是不难的,但是还有个坑点就是题目里明明说给的是矩形左上角跟右下角的点,但实际上不是,需要重新判断一下...真坑。

 1 struct Point
 2 {
 3     double x, y;
 4 } A, B, C, D;
 5 struct Line
 6 {
 7     Point a, b;
 8 } L;
 9
10 int n;
11
12 double xmult(Point p1, Point p2, Point p)
13 {
14     return (p1.x-p.x)*(p2.y-p.y)-(p2.x-p.x)*(p1.y-p.y);
15 }
16
17 ///若共线,返回1;不共线,返回0。
18 int dot_inLine(Point p1, Point p2, Point p3){
19     return zero(xmult(p1, p2, p3));
20 }
21 ///判两点在线段同侧,点在线段上返回0
22 int same_side(Point p1, Point p2, Line l){
23     return xmult(l.a, p1, l.b)*xmult(l.a, p2, l.b) > eps;
24 }
25 ///判点是否在线段上,包括端点
26 int dot_onLine_in(Point p, Line l){
27     return zero(xmult(p, l.a, l.b)) && (l.a.x-p.x)*(l.b.x-p.x) < eps && (l.a.y-p.y)*(l.b.y-p.y) < eps;
28 }
29 int intersect_in(Line u, Line v){
30     if (!dot_inLine(u.a, u.b, v.a)
31         || !dot_inLine(u.a, u.b, v.b))
32         return !same_side(u.a, u.b,v) && !same_side(v.a, v.b,u);
33     return dot_onLine_in(u.a, v) || dot_onLine_in(u.b, v)
34         || dot_onLine_in(v.a, u) || dot_onLine_in(v.b, u);
35 }
36
37 bool is_Inter(Point A, Point B, Point C, Point D, Point t)
38 {
39     if(xmult(t, A, B) > eps && xmult(t, B, C) > eps && xmult(t, C, D) > eps && xmult(t, D, A) > eps)
40         return true;
41     if(xmult(t, A, B) < eps && xmult(t, B, C) < eps && xmult(t, C, D) < eps && xmult(t, D, A) < eps)
42         return true;
43     if(t.x >= A.x && t.x <= B.x && t.y >= C.y && t.y <= B.y && (zero(xmult(t, A, B)) || zero(xmult(t, B, C)) || zero(xmult(t, C, D)) || zero(xmult(t, D, A))))
44        return true;
45     return false;
46 }
47
48 int T;
49
50 void Solve()
51 {
52     scanf("%d", &T);
53     while(T--)
54     {
55         scanf("%lf%lf%lf%lf%lf%lf%lf%lf", &L.a.x, &L.a.y, &L.b.x, &L.b.y, &C.x, &C.y, &D.x, &D.y);
56         A.x = min(C.x, D.x);
57         A.y = max(C.y, D.y);
58         C.x = max(C.x, D.x);
59         C.y = min(C.y, D.y);
60         B.x = C.x, B.y = A.y;
61         D.x = A.x, D.y = C.y;
62         if(is_Inter(A, B, C, D, L.a) && is_Inter(A, B, C, D, L.b))
63         {
64             printf("T\n");
65         }
66         else if(intersect_in((Line){A, B}, L) || intersect_in((Line){B, C,}, L) || intersect_in((Line){C, D}, L) || intersect_in((Line){D, A}, L))
67         {
68             printf("T\n");
69         }
70         else
71         {
72             printf("F\n");
73         }
74     }
75 }

POJ 1410

POJ 1410 Intersection(线段相交&&判断点在矩形内&&坑爹)

时间: 2024-08-05 00:05:41

POJ 1410 Intersection(线段相交&&判断点在矩形内&&坑爹)的相关文章

poj 2653 (线段相交判断)

http://poj.org/problem?id=2653 Pick-up sticks Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 9531   Accepted: 3517 Description Stan has n sticks of various length. He throws them one at a time on the floor in a random way. After finishi

Treasure Hunt - POJ 1066(线段相交判断)

题目大意:在一个正方形的迷宫里有一些交错墙,墙的两端都在迷宫的边缘墙上面,现在得知迷宫的某个位置有一个宝藏,所以需要砸开墙来获取宝藏(只能砸一段墙的中点),问最少要砸开几面墙. 分析:这个题意刚开始理解错了,以为只能砸整面墙的中点,而实际上使一段墙的中点,也就是两个交点之间的墙,这样问题就变得比较容易了,中点的意义也就不存在了,因为所有的墙都是连接的边缘,所以如果从起点到终点连线有这堵墙,那么这堵墙一定无法绕过去,所以枚举所有的边缘点到终点有几面墙即可,注意不管怎样,边缘墙一定是要砸的. 代码如

poj 1410 线段相交判断

http://poj.org/problem?id=1410 Intersection Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11329   Accepted: 2978 Description You are to write a program that has to decide whether a given line segment intersects a given rectangle. An ex

POJ 1410 Intersection(线段相交&amp;amp;&amp;amp;推断点在矩形内&amp;amp;&amp;amp;坑爹)

Intersection 大意:给你一条线段,给你一个矩形,问是否相交. 相交:线段全然在矩形内部算相交:线段与矩形随意一条边不规范相交算相交. 思路:知道详细的相交规则之后题事实上是不难的,可是还有个坑点就是题目里明明说给的是矩形左上角跟右下角的点,但实际上不是,须要又一次推断一下...真坑. struct Point { double x, y; } A, B, C, D; struct Line { Point a, b; } L; int n; double xmult(Point p1

POJ 1410 Intersection --几何,线段相交

题意: 给一条线段,和一个矩形,问线段是否与矩形相交或在矩形内. 解法: 判断是否在矩形内,如果不在,判断与四条边是否相交即可.这题让我发现自己的线段相交函数有错误的地方,原来我写的线段相交函数就是单纯做了两次跨立实验,在下图这种情况是错误的: 这样的话线段与右边界的两次跨立实验(叉积<=0)都会通过,但是并不相交. 所以要加快速排斥. 还有就是这题题目说给出的不一定是左上角,右下角依次的顺序.所以干脆重新自己定义左上角,右下角. 代码: #include <iostream> #inc

zoj 1010 (线段相交判断+多边形求面积)

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=10 Area Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Jerry, a middle school student, addicts himself to mathematical research. Maybe the problems he has thought are

ACM1558两线段相交判断和并查集

Segment set Problem Description A segment and all segments which are connected with it compose a segment set. The size of a segment set is the number of segments in it. The problem is to find the size of some segment set. Input In the first line ther

poj 1410 Intersection (判断线段与矩形相交 判线段相交)

题目链接 Intersection Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12040   Accepted: 3125 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:

POJ 1410 Intersection (线段和矩形相交)

题目: 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 doe