POJ - 1410 - Intersection(計算幾何)
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
題目連結
計算幾何
這是一個計算幾何的模板題目,可以看一下計算幾何的板子,裡面也講得特別詳細,主要是通過快速排斥和跨歷實驗來判斷兩條線段是否相交。
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define eps 1e-7
using namespace std;
int n;
//結構體記錄點
struct Node
{
double x, y;
Node(double x = 0, double y = 0) : x(x), y(y) {}
Node operator - (const Node& obj) const
{
return Node(x - obj.x, y - obj.y);
}
//叉乘
double operator * (const Node& obj) const
{
return x * obj.y - y * obj.x;
}
}line_start, line_end, vertex[6];
int accuracy(double x) //精度問題處理
{
if (fabs(x) < eps) return 0;
return x < 0 ? -1 : 1;
}
//判斷是否相交
int inter(Node point1_line1, Node point2_line1, Node point1_line2, Node point2_line2)
{
if(min(point1_line1.x, point2_line1.x) > max(point1_line2.x, point2_line2.x) || min(point1_line1.y, point2_line1.y) > max(point1_line2.y, point2_line2.y))
return 0;
if(min(point1_line2.x, point2_line2.x) > max(point1_line1.x, point2_line1.x) || min(point1_line2.y, point2_line2.y) > max(point1_line1.y, point2_line1.y))
return 0;
double val1 = (point2_line1 - point1_line1) * (point1_line2 - point1_line1);
double val2 = (point2_line1 - point1_line1) * (point2_line2 - point1_line1);
double val3 = (point2_line2 - point1_line2) * (point1_line1 - point1_line2);
double val4 = (point2_line2 - point1_line2) * (point2_line1 - point1_line2);
return accuracy(val1) * accuracy(val2) <= 0 && accuracy(val3) * accuracy(val4) <= 0;
}
判斷點是否在矩形內部
int inside(Node now)
{
double x = now.x; double y = now.y;
return x >= vertex[1].x && x <= vertex[3].x && y <= vertex[1].y && y >= vertex[3].y;
}
int main()
{
scanf("%d", &n);
while(n--)
{
scanf("%lf%lf", &line_start.x, &line_start.y);
scanf("%lf%lf", &line_end.x, &line_end.y);
scanf("%lf%lf", &vertex[1].x, &vertex[1].y);
scanf("%lf%lf", &vertex[3].x, &vertex[3].y);
if(vertex[1].x > vertex[3].x) swap(vertex[1].x, vertex[3].x);
if(vertex[1].y < vertex[3].y) swap(vertex[1].y, vertex[3].y);
vertex[2].x = vertex[3].x; vertex[2].y = vertex[1].y;
vertex[4].x = vertex[1].x; vertex[4].y = vertex[3].y;
int ans = 0;
ans += inter(vertex[1], vertex[2], line_start, line_end);
ans += inter(vertex[2], vertex[3], line_start, line_end);
ans += inter(vertex[3], vertex[4], line_start, line_end);
ans += inter(vertex[4], vertex[1], line_start, line_end);
if(inside(line_start) || inside(line_end)) ans++;
if(ans) printf("T\n");
else printf("F\n");
}
return 0;
}