寒假集训.Intersection

本文介绍了一种算法,用于判断给定的线段是否与矩形相交。通过检查线段是否穿过矩形的对角线,并判断线段两端是否位于矩形同一侧来实现。
Intersection
Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Description

Download as PDF


 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)

 figure27
Figure: 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:

displaymath45

where (xstartystart) is the start and (xendyend) the end point of the line and (xleftytop) the top left and (xrightybottom) the bottom right corner of the rectangle. The eight numbers are separated by a blank. The terms tex2html_wrap_inline55 and tex2html_wrap_inline57 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
 
   
先判断线段所在直线是否穿过某一矩形对角线(1),若不穿过,则线段与矩形必不相交,若穿过在判断线段两个端点是否在矩形的一侧,因为有了条件(1)则不在一侧时必与矩形相交,否则不相交。
    
#include 
      
       
using namespace std;
int main(){
    int n,xs,ys,xe,ye,xleft,ytop,xr,yb;
    cin>>n;
    for(int i=0;i
       
        >xs>>ys>>xe>>ye>>xleft>>ytop>>xr>>yb;
        int a=ys-ye,b=xe-xs,c=xs*ye-xe*ys;
        if( (a*xleft+b*ytop+c>=0&&a*xr+b*yb+c<=0)||(a*xleft+b*ytop+c<=0&&a*xr+b*yb+c>=0)||(a*xleft+b*yb+c>=0&&a*xr+b*ytop+c<=0)||(a*xleft+b*yb+c<=0&&a*xr+b*ytop+c>=0)){
                if(xleft>xr)
                swap(xleft,xr);
                if(ytop
        
         xr&&xe>xr)||(ys>ytop&&ye>ytop)||(ys
         
          
         
        
       
      
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值