您的位置:首页 > 其它

poj Intersection 1410 (数学几何 求线段方程)

2015-10-29 21:14 337 查看
Intersection

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 13147 Accepted: 3426
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

题意

给定一条线段的起点和终点,再给定一个矩形的起点和终点(对角线两点),问线段是否和矩形所包含的区域有交点。

思路:

先求出线段所在直线的方程a*x+b*y+c=0;(用到最原始的求直线方程的公式(可能是小学或者初中学的,但当时就是不知道咋写.......))。

然后判断线段是否与矩形所在区域有交点。具体看代码。。。

#include<stdio.h>
#include<string.h>
struct zz
{
int x;
int y;
}lb,le,sb,se;
int main()
{
int t,num1,num2;
int a,b,c,s;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d%d%d%d%d%d",&lb.x,&lb.y,&le.x,&le.y,&sb.x,&sb.y,&se.x,&se.y);
{
a=lb.y-le.y;
b=le.x-lb.x;
c=le.y*lb.x-lb.y*le.x;
if(sb.x>se.x)
{
s=sb.x;sb.x=se.x;se.x=s;
}
if(sb.y<se.y)
{
s=sb.y;sb.y=se.y;se.y=s;
}
num1=(a*sb.x+b*sb.y+c)*(a*se.x+b*se.y+c);
num2=(a*sb.x+b*se.y+c)*(a*se.x+b*sb.y+c);
if(num1>0&&num2>0)
{
printf("F\n");
continue;
}
if((lb.x<sb.x&&le.x<sb.x)||(lb.x>se.x&&le.x>se.x)||(lb.y>sb.y&&le.y>sb.y)||(lb.y<se.y&&le.y<se.y))
printf("F\n");
else
printf("T\n");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: