poj 1410 Intersection (线段相交判定)

Intersection
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 14287   Accepted: 3735

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) 

poj 1410 Intersection (线段相交判定)_第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.

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

[Submit]   [Go Back]   [Status]   [Discuss]


题目大意:给出一个矩形和一条线段,判断两者是否有交点。

题解:四条边与给出的线段判断是否相交,然后特判线段是否在矩形的内部。

#include
#include
#include
#include
#include
#define eps 1e-7
using namespace std;
struct point
{
	double x,y;
	point (double X=0,double Y=0) {
		x=X,y=Y;
	}
}tmp,tmp1,a[10];
typedef point vector;
point operator -(point a,point b){
	return vector (a.x-b.x,a.y-b.y);
}
int n;
int dcmp(double x)
{
	if (fabs(x)max(b1.x,b2.x)||min(a1.y,a2.y)>max(b1.y,b2.y)||min(b1.x,b2.x)>max(a1.x,a2.x)||min(b1.y,b2.y)>max(a1.y,a2.y)) return 0;
	double c1=cross(a2-a1,b1-a1),c2=cross(a2-a1,b2-a1),
	    c3=cross(b2-b1,a1-b1),c4=cross(b2-b1,a2-b1);
    return dcmp(c1)*dcmp(c2)<=0&&dcmp(c3)*dcmp(c4)<=0;
}

int pd(point now)
{
	double x=now.x; double y=now.y;
	return x>=a[1].x&&x<=a[3].x&&y<=a[1].y&&y>=a[3].y;
}
int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++){
	    scanf("%lf%lf",&tmp.x,&tmp.y);
	    scanf("%lf%lf",&tmp1.x,&tmp1.y);
		scanf("%lf%lf",&a[1].x,&a[1].y);
		scanf("%lf%lf",&a[3].x,&a[3].y);
		if (a[1].x>a[3].x) swap(a[1].x,a[3].x);
		if (a[1].y


你可能感兴趣的:(计算几何)