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

    题目大意:

    题目意思很简单,就是说有一个矩阵是实心的,给出一条线段,问线段和矩阵是否相交

    An example: 
    line: start point: (4,9) 
    end point: (11,2) 
    rectangle: left-top: (1,5) 
    right-bottom: (7,1) 

     

    Sample Input

    1
    4 9 11 2 1 5 7 1

    Sample Output

    F
    题解:
    一道鬼题
    本身不难,只要拿四条边与线段做快速排斥和跨立就行
    但有很多细节:
    1.给出的矩形的坐标要判断,不合法要交换
    2.包含在矩形内,不与任何边相交
    3.与边共线,还要判断是否有重合部分
     1 #include<cstdio>
     2 #include<algorithm>
     3 #include<cstring>
     4 #include<iostream>
     5 #include<cmath>
     6 using namespace std;
     7 struct Node
     8 {
     9     double x1,y1,x2,y2;
    10 }line[10001];
    11 int T;
    12 double xe,ye,xs,ys,xl,yt,xr,yb;
    13 double direction(double x,double y,double x1,double y1,double x2,double y2)
    14 {  
    15     double a1=x1-x;  
    16     double b1=y1-y;  
    17     double a2=x2-x;  
    18     double b2=y2-y;  
    19     return a1*b2-a2*b1;  
    20 }  
    21 int on_segment(double x1,double y1,double x2,double y2,double x,double y)
    22 {  
    23     if((min(x1,x2)<=x&&x<=max(x1,x2))&&(min(y1,y2)<=y&&y<=max(y1,y2)))  
    24         return 1;  
    25     return 0;  
    26 }  
    27   
    28 bool exam(Node v,Node t)
    29 {  
    30     double d1,d2,d3,d4;  
    31     d1=direction(t.x1,t.y1,t.x2,t.y2,v.x1,v.y1);  
    32     d2=direction(t.x1,t.y1,t.x2,t.y2,v.x2,v.y2);  
    33     d3=direction(v.x1,v.y1,v.x2,v.y2,t.x1,t.y1);  
    34     d4=direction(v.x1,v.y1,v.x2,v.y2,t.x2,t.y2);  
    35     if(d1*d2<0 && d3*d4<0) return 1;  
    36     if(!d1&&on_segment(t.x1,t.y1,t.x2,t.y2,v.x1,v.y1)) return 1;  
    37     if(!d2&&on_segment(t.x1,t.y1,t.x2,t.y2,v.x2,v.y2)) return 1;  
    38     if(!d3&&on_segment(v.x1,v.y1,v.x2,v.y2,t.x1,t.y1)) return 1;  
    39     if(!d4&&on_segment(v.x1,v.y1,v.x2,v.y2,t.x2,t.y2)) return 1;  
    40     return 0;  
    41 }  
    42 int main()
    43 {
    44     cin>>T;
    45     while (T--)
    46     {
    47         scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&xs,&ys,&xe,&ye,&xl,&yt,&xr,&yb);
    48      if (yt<yb) swap(yt,yb);
    49      if (xl>xr) swap(xl,xr);
    50      Node d;
    51      d.x1=xs;d.y1=ys;
    52      d.x2=xe;d.y2=ye;
    53      line[1].x1=xl;line[1].y1=yt;line[1].x2=xr;line[1].y2=yt;
    54      line[2].x1=xl;line[2].y1=yb;line[2].x2=xr;line[2].y2=yb;
    55      line[3].x1=xl;line[3].y1=yt;line[3].x2=xl;line[3].y2=yb;
    56      line[4].x1=xr;line[4].y1=yt;line[4].x2=xr;line[4].y2=yb;
    57       if (exam(d,line[1])||exam(d,line[2])||exam(d,line[3])||exam(d,line[4]))
    58        {
    59            cout<<"T
    ";
    60        }    
    61        else 
    62        {
    63            if (xl<=min(xs,xe)&&xr>=max(xs,xe)&&yt>=max(ys,ye)&&yb<=min(ys,ye))
    64            cout<<"T
    ";
    65            else 
    66            cout<<"F
    ";
    67        }
    68     }
    69 }
    
    
    
     
  • 相关阅读:
    43 李新佳 实验1
    0909 编译之路
    读后感
    评论
    0302作业
    构建之法第四章
    Latency Compensating Methods in Client/Server Ingame Protocol Design and Optimization
    利用Ptrace在Android平台实现应用程序控制[转]
    Bit Twiddling Hacks[转]
    Unity3D实用工具汇总[转]
  • 原文地址:https://www.cnblogs.com/Y-E-T-I/p/7270345.html
Copyright © 2011-2022 走看看