POJ 1410 Intersection(一线段是否与矩阵相交)

Intersection
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 11585 Accepted: 3014

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
给你一条线段的两个端点坐标,和一个 矩形的两个对角坐标,问这条线段是否与矩形相交(在矩形内部也算)。。
所以首先判定线段是否在矩形内部,若在内部,直接输出T,否则的话在判断是否与矩形的一条对角线 相交,若相交输出T ,否则在判断与另一条对角线是否相交,如果不相交直接输出F
#include <cstdio>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <algorithm>
#include <math.h>
using namespace std;
struct node
{
    int  x1,y1,x2,y2;
}a[4];

int yice(node a,node b)   //两点式方程 的变形,详见HDU 1086的分析
{
    double  s1,s2;
    s1=b.x1*(a.y2-a.y1) + a.x2*a.y1-a.x1*a.y2 - b.y1*(a.x2-a.x1);
    s2=b.x2*(a.y2-a.y1) + a.x2*a.y1-a.x1*a.y2 - b.y2*(a.x2-a.x1);
    if(s1*s2<=0)
        return 1;
    else return 0;
}
int main()
{
    int n;
    scanf("%d",&n);
    while(n--)
     {

    scanf("%d%d%d%d",&a[1].x1,&a[1].y1,&a[1].x2,&a[1].y2);
    scanf("%d%d%d%d",&a[2].x1,&a[2].y1,&a[2].x2,&a[2].y2);

     int maxx1,maxx2;
     int maxy1,maxy2;   //判断是否在矩形内部
     if(a[2].x1<a[2].x2)
     {
         maxx1=a[2].x1;
         maxx2=a[2].x2;
     }
     else
     {
          maxx1=a[2].x2;
         maxx2=a[2].x1;
     }
     if(a[2].y1<a[2].y2)
     {
         maxy1=a[2].y1;
         maxy2=a[2].y2;
     }
     else
     {
          maxy1=a[2].y2;
         maxy2=a[2].y1;
     }
     if(a[1].x1>=maxx1&&a[1].x1<=maxx2 && a[1].x2>=maxx1&&a[1].x2<=maxx2 && a[1].y2>=maxy1&&a[1].y2<=maxy2 &&a[1].y1>=maxy1&&a[1].y1<=maxy2)
     {
         printf("T\n");
         continue;
     }
      if(yice(a[1],a[2])&&yice(a[2],a[1]))    //判断是否有交点
          {

           printf("T\n");
           continue;
          }
        int f2=0;

         struct node c;   //另一条对角线
          c.x1=a[2].x1;
          c.y1=a[2].y2;
          c.x2=a[2].x2;
          c.y2=a[2].y1;
         if(yice(a[1],c)&&yice(c,a[1]))
                f2=1;
            if(f2)
              printf("T\n");
         else   printf("F\n");
            
     }
    return 0;
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值