# [POJ 1410] Intersection【计算几何】

• 2017-12-10
• 0
• 0

## Problem:

 Time Limit: 1000MS Memory Limit: 10000K

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.

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 ybottomwhere (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.

Sample Input

```1
4 9 11 2 1 5 7 1```

Sample Output

`F`

Source

Southwestern European Regional Contest 1995

## Solution #1:

i) 线段与矩形任一边界相交。Implement: 两条线段相交；

ii)线段完全在矩形内。Implement: 点是否在矩形内。

## Code #1: O(n) [176K, 0MS]

```#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;

int n;

#define Vector Point
struct Point{
double x, y;

Point() {}
Point(double x, double y): x(x), y(y) {}

inline Vector operator - (const Point p2) const {return Vector(x - p2.x, y - p2.y);}

inline bool Strictly_in_Rect(const Point r, const Point s){
if(x > min(r.x, s.x) && x < max(r.x, s.x) && y > min(r.y, s.y) && y < max(r.y, s.y)) return 1;
return 0;
}
} rectA, rectB, rectC, rectD;

struct Segment{
Point u, v;

Segment() {}
Segment(Point u, Point v): u(u), v(v) {}
} seg;

inline double Cross(const Vector vec1, const Vector vec2) {return vec1.x * vec2.y - vec1.y * vec2.x;}

inline bool is_Intersected(const Segment s1, const Segment s2){
if(min(s1.u.x, s1.v.x) <= max(s2.u.x, s2.v.x)
&& max(s1.u.x, s1.v.x) >= min(s2.u.x, s2.v.x)
&& min(s1.u.y, s1.v.y) <= max(s2.u.y, s2.v.y)
&& max(s1.u.y, s1.v.y) >= min(s2.u.y, s2.v.y)
&& Cross(s1.v - s1.u, s2.u - s1.u) * Cross(s2.v - s1.u, s1.v - s1.u) >= 0
&& Cross(s2.v - s2.u, s1.u - s2.u) * Cross(s1.v - s2.u, s2.v - s2.u) >= 0) return 1;
return 0;
}
int main(){
scanf("%d", &n);
while(n--){
scanf("%lf%lf%lf%lf%lf%lf%lf%lf", &seg.u.x, &seg.u.y, &seg.v.x, &seg.v.y, &rectA.x, &rectA.y, &rectC.x, &rectC.y);
rectB.x = rectA.x, rectB.y = rectC.y, rectD.x = rectC.x, rectD.y = rectA.y;  // Build the rectangle
if((seg.u.Strictly_in_Rect(rectA, rectC) && seg.v.Strictly_in_Rect(rectA, rectC))
|| is_Intersected(seg, Segment(rectA, rectB))
|| is_Intersected(seg, Segment(rectB, rectC))
|| is_Intersected(seg, Segment(rectC, rectD))
|| is_Intersected(seg, Segment(rectD, rectA))) puts("T");
else puts("F");
}
return 0;
}
```

## Code #2: O(n) with smaller constant [176K, 0MS]

```#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;

int n;

#define Vector Point
struct Point{
double x, y;

Point() {}
Point(double x, double y): x(x), y(y) {}

inline Vector operator - (const Point p2) const {return Vector(x - p2.x, y - p2.y);}
} p, q, r, s;

inline double Cross(const Vector vec1, const Vector vec2) {return vec1.x * vec2.y - vec1.y * vec2.x;}

inline bool Seg_Rect_Intersect(Point p, Point q, Point r, Point s){
if(min(p.x, q.x) <= max(r.x, s.x) && max(p.x, q.x) >= min(r.x, s.x)
&& min(p.y, q.y) <= max(r.y, s.y) && max(p.y, q.y) >= min(r.y, s.y)
&& (Cross(q - p, r - p) * Cross(s - p, q - p) >= 0
|| Cross(q - p, Point(r.x, s.y) - p) * Cross(Point(s.x, r.y) - p, q - p) >= 0)) return 1;
return 0;
}

int main(){
scanf("%d", &n);
while(n--){
scanf("%lf%lf%lf%lf%lf%lf%lf%lf", &p.x, &p.y, &q.x, &q.y, &r.x, &r.y, &s.x, &s.y);
if(Seg_Rect_Intersect(p, q, r, s)) puts("T");
else puts("F");
}
return 0;
}
```

#### 评论

darkleafin.cf
(该域名已过期且被抢注。。)
darkleafin.github.io

49750

https://github.com/Darkleafin

OPEN AT 2017.12.10

Please refresh the page if the code cannot be displayed normally.

https://visualgo.net/en

- Theme by Qzhai