zoukankan      html  css  js  c++  java
  • POJ 1410 计算几何 线段矩形相交

    题意:
    判断线段是否与矩形相交。

    ps:矩形坐标不是按顺序给出的,需要自行判断

    友情提示,线段在矩形内部也输出T

    题解:

    叉积判断是否规范相交,再用点积判断部分重合的情况~

    计算几何一定要严谨啊!!!!

     PS:此代码不算严谨,严谨的请移步:http://www.cnblogs.com/proverbs/archive/2013/02/23/2923776.html

    这个代码就不做修改了

    View Code
     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 #include <algorithm>
     5 #include <cstdlib> 
     6 
     7 using namespace std;
     8 
     9 struct P
    10 {
    11     int x,y;
    12 }st,ed,lu,ld,ru,rd;
    13 
    14 int n;
    15 
    16 inline void read()
    17 {
    18     scanf("%d%d%d%d%d%d%d%d",&st.x,&st.y,&ed.x,&ed.y,&ld.x,&ru.y,&ru.x,&ld.y);
    19     if(ld.x>ru.x) swap(ld.x,ru.x);
    20     if(ld.y>ru.y) swap(ru.y,ld.y);
    21     //cout<<ld.x<<"   "<<ld.y<<"      "<<ru.x<<"    "<<ru.y<<endl;
    22     lu.x=ld.x; lu.y=ru.y; rd.x=ru.x; rd.y=ld.y;
    23 }
    24 
    25 inline int cross(const P &o,const P &a,const P &b)
    26 {
    27     int fx=a.x-o.x,fy=a.y-o.y,px=b.x-o.x,py=b.y-o.y;
    28     return fx*py-fy*px;
    29 }
    30 
    31 inline int mult(const P &o,const P &a,const P &b)
    32 {
    33     return (a.x-o.x)*(b.x-o.x)+(a.y-o.y)*(b.y-o.y);
    34 }
    35 
    36 inline bool check(const P &a1,const P &a2,const P &b1,const P &b2)
    37 {
    38     int fg1=cross(a1,a2,b1)*cross(a1,a2,b2);
    39     int fg2=cross(b1,b2,a1)*cross(b1,b2,a2);
    40     if(fg1<0&&fg2<0) return true;
    41     else if(fg1==0&&fg2==0&&mult(b1,a1,a2)<0) return true;//若共线则通过点积判断点在线段上
    42     //计算几何一定要严谨! 
    43     return false;
    44 }
    45 
    46 inline bool inside()
    47 {
    48     if((st.x>=ld.x&&st.y>=ld.y&&st.x<=ru.x&&st.y<=ru.y)||(ed.x>=ld.x&&ed.y>=ld.y&&ed.x<=ru.x&&ed.y<=ru.y)) return true;
    49     return false;
    50 }
    51 
    52 inline void go()
    53 {
    54     if(check(st,ed,lu,ru)) {puts("T");return;}
    55     if(check(st,ed,lu,ld)) {puts("T");return;}
    56     if(check(st,ed,ld,rd)) {puts("T");return;}
    57     if(check(st,ed,ru,rd)) {puts("T");return;}
    58     if(inside()) {puts("T");return;}//在矩形内部 
    59     puts("F");
    60 }
    61 
    62 int main()
    63 {
    64     while(scanf("%d",&n)!=EOF) 
    65         while(n--) read(),go();
    66     return 0;
    67 }
  • 相关阅读:
    Ubuntu系统下《汇编语言》环境配置

    GetDiskFreeSpaceEx的使用
    夏走了
    小笨妞
    项目管理怎么可以这样子!


    浪费
    查找文件夹
  • 原文地址:https://www.cnblogs.com/proverbs/p/2855648.html
Copyright © 2011-2022 走看看