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

99 篇文章 0 订阅
59 篇文章 0 订阅
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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值