[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: 点是否在矩形内。

以上两项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;
}

Solution #2: ~~ 优化算法 by Willem ~~

若线段和矩形未通过快速排斥试验(Quick Rejection Test),则两者不可能相交。

反之,在通过QRT后,线段所在矩形一定与矩形有公共部分

此时若线段所在直线与矩形任一对角线相交,则线段一定与矩形区域相交。

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


常年不在线的QQ:
49750

不定期更新的GitHub:
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