zoukankan      html  css  js  c++  java
  • poj 1410 Intersection

    Intersection
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 12162   Accepted: 3157

    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

    Source

    分析:

      1 #include <cstdio>
      2 #include<algorithm>
      3 #include<iostream>
      4 #include<string>
      5 #include<cstring>
      6 #include<vector>
      7 using namespace std;
      8 struct point{
      9     double x,y;
     10 };
     11 double det(point a,point b,point c){
     12     return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
     13 }
     14 point a,b,c,d,e,f;
     15 bool check(point e){
     16     if(e.x>=a.x&&e.y<=a.y&&e.x<=c.x&&e.y>=c.y){
     17         return true;
     18     }
     19     return false;
     20 }
     21 double max(double a,double b){
     22     if(a>b){
     23         return a;
     24     }
     25     return b;
     26 }
     27 double min(double a,double b){
     28     if(a>b){
     29         return b;
     30     }
     31     return a;
     32 }
     33 int main(){
     34     int n;
     35     cin>>n;
     36     while(n--){
     37         double xl,yt,xr,yb;
     38         cin>>e.x>>e.y>>f.x>>f.y>>xl>>yt>>xr>>yb;
     39         if(xl>xr){
     40             double x=xl;
     41             xl=xr;    
     42             xr=x;
     43         }
     44         if(yt<yb){
     45             double y=yt;    
     46             yt=yb;
     47             yb=y;
     48         }
     49         a.x=xl;
     50         a.y=yt;
     51         b.x=xr;
     52         b.y=yt;
     53         c.x=xr;
     54         c.y=yb;
     55         d.x=xl;
     56         d.y=yb;
     57         if(check(e)||check(f)){//线段有至少一个端点在矩形内部或边界上
     58             cout<<"T"<<endl;
     59             continue;
     60         }
     61         double aa,bb,cc,dd;
     62         aa=det(e,a,b)*det(f,a,b),bb=det(a,e,f)*det(b,e,f);
     63         if(aa==0&&bb==0&&(max(e.x,f.x)<min(b.x,a.x)||max(e.y,f.y)<min(b.y,a.y)||max(b.x,a.x)<min(e.x,f.x)||max(b.y,a.y)<min(e.y,f.y))){//特殊情况:线段与矩形四条边界中的任一条共线,但满足次条件表示没有公共交点
     64             cout<<"F"<<endl;
     65             continue;
     66         }
     67         if(aa<=0&&bb<=0){//除去前面的特殊情况,满足次条件表示两线段相交
     68             cout<<"T"<<endl;
     69             continue;
     70         }
     71         /*if(det(e,b,c)*det(f,b,c)<=0&&det(b,e,f)*det(c,e,f)<=0){
     72             cout<<"det(e,b,c): "<<det(e,b,c)<<endl;
     73             cout<<"det(f,b,c): "<<det(f,b,c)<<endl;
     74             cout<<"det(b,e,f): "<<det(b,e,f)<<endl;
     75             cout<<"det(c,e,f): "<<det(c,e,f)<<endl;
     76             cout<<"T"<<endl;
     77             continue;
     78         }*/
     79         aa=det(e,b,c)*det(f,b,c),bb=det(b,e,f)*det(c,e,f);
     80         if(aa==0&&bb==0&&(max(e.x,f.x)<min(b.x,c.x)||max(e.y,f.y)<min(b.y,c.y)||max(b.x,c.x)<min(e.x,f.x)||max(b.y,c.y)<min(e.y,f.y))){
     81             cout<<"F"<<endl;
     82             continue;
     83         }
     84         if(aa<=0&&bb<=0){
     85             cout<<"T"<<endl;
     86             continue;
     87         }
     88         
     89         aa=det(e,c,d)*det(f,c,d),bb=det(c,e,f)*det(d,e,f);
     90         if(aa==0&&bb==0&&(max(e.x,f.x)<min(c.x,d.x)||max(e.y,f.y)<min(c.y,d.y)||max(c.x,d.x)<min(e.x,f.x)||max(c.y,d.y)<min(e.y,f.y))){
     91             cout<<"F"<<endl;
     92             continue;
     93         }
     94         if(aa<=0&&bb<=0){
     95             cout<<"T"<<endl;
     96             continue;
     97         }
     98         
     99         aa=det(e,d,a)*det(f,d,a),bb=det(d,e,f)*det(a,e,f);
    100         if(aa==0&&bb==0&&(max(e.x,f.x)<min(a.x,d.x)||max(e.y,f.y)<min(a.y,d.y)||max(a.x,d.x)<min(e.x,f.x)||max(a.y,d.y)<min(e.y,f.y))){
    101             cout<<"F"<<endl;
    102             continue;
    103         }
    104         if(aa<=0&&bb<=0){
    105             cout<<"T"<<endl;
    106             continue;
    107         }
    108         
    109         cout<<"F"<<endl;
    110     }
    111     return 0;
    112 } 
  • 相关阅读:
    JavaScript递归函数解“汉诺塔”
    《JavaScript DOM编程艺术》学习笔记(三)
    《JavaScript DOM编程艺术》学习笔记(二)
    《JavaScript DOM编程艺术》学习笔记(一)
    《手把手教你实现电商网站开发》课程学习总结
    学校水卡、本地公交卡破解记
    第一篇博客
    mysql5.7.5以上版本使用distinct和order by 报错备忘录
    group_concat长度限制踩坑备忘录
    ArrayList、LinkedList、Vector的区别
  • 原文地址:https://www.cnblogs.com/Deribs4/p/4294794.html
Copyright © 2011-2022 走看看