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 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

题目链接

判断线段与矩形是否相交。

分别判断线段与矩形的四条边是否相交,若都不相交则判断线段是否在矩形内部(判断线段上任意一点即可)。

矩形坐标给出的顺序并不固定,需自行判断。

AC代码:

#include <iostream>
#include <cstdio>
using namespace std;

struct Point {
	double X, Y;

	Point operator - (const Point &B) const {
		return Point {X - B.X, Y - B.Y};
	}
	
	double operator * (const Point &B) const {
		return X * B.X + Y * B.Y;
	}

	double operator ^ (const Point &B) const {
		return X * B.Y - Y * B.X;
	}
};

struct Segment {
	Point S, T;

	double operator ^ (const Segment &B) const {
		return (T - S) ^ (B.T - B.S);
	}
};

int T;
bool Flag;
double XStart, YStart, XEnd, YEnd, XLeft, YTop, XRight, YBottom;

bool IsIntersect(Segment A, Segment B) {
	return	
		max(A.S.X, A.T.X) >= min(B.S.X, B.T.X) &&
		max(B.S.X, B.T.X) >= min(A.S.X, A.T.X) &&
		max(A.S.Y, A.T.Y) >= min(B.S.Y, B.T.Y) &&
		max(B.S.Y, B.T.Y) >= min(A.S.Y, A.T.Y) &&
		(A ^ Segment {A.S, B.S}) * (A ^ Segment {A.S, B.T}) <= 0 &&
		(B ^ Segment {B.S, A.S}) * (B ^ Segment {B.S, A.T}) <= 0;
}

int main(int argc, char *argv[]) {
	scanf("%d", &T);
	for (int Case = 1; Case <= T; ++Case) {
		Flag = false;
		scanf("%lf%lf%lf%lf%lf%lf%lf%lf", &XStart, &YStart, &XEnd, &YEnd, &XLeft, &YTop, &XRight, &YBottom);
		if (XLeft > XRight) {
			swap(XLeft, XRight);
		}
		if (YBottom > YTop) {
			swap(YTop, YBottom);
		}
		Segment Judge = Segment {Point {XStart, YStart}, Point {XEnd, YEnd}};
		Segment Left = Segment {Point {XLeft, YBottom}, Point {XLeft, YTop}}, Right = Segment {Point {XRight, YBottom}, Point{XRight, YTop}};
		Segment Top = Segment {Point {XLeft, YTop}, Point {XRight, YTop}}, Bottom = Segment {Point {XLeft, YBottom}, Point {XRight, YBottom}};
		if (IsIntersect(Left, Judge) || IsIntersect(Right, Judge) || IsIntersect(Top, Judge) || IsIntersect(Bottom, Judge)) {
			Flag = true;
		}
		if (!Flag) {
			if (XStart >= XLeft && XStart <= XRight && YStart >= YBottom && YStart <= YTop) {
				Flag = true;
			}
		}
		if (Flag) {
			printf("T\n");
		}
		else {
			printf("F\n");
		}
	}
	return 0;
}
全部评论

相关推荐

勤奋努力的椰子这就开摆:美团骑手在美团工作没毛病
投递美团等公司10个岗位
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务