POJ 1410 Intersection(计算几何)

题目链接:

id=1410">Intersection

推断线段与矩形的关系。与矩形相交打印T,否则打印F。

坑题,精度。

。。

思路就是,先推断 线段是否在矩形里面,再推断线段和两条对角线的关系,利用叉积模板就可以

測试数据有个坑,就是 左上角的坐标并不一定比右下角的小。

。。这根本不符合题意嘛

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <math.h>
#define init(a) memset(a,0,sizeof(a))
#define PI acos(-1,0)
using namespace std;
const int maxn = 110;
const int maxm = 10010;
#define lson left, m, id<<1
#define rson m+1, right, id<<1|1
#define min(a,b) (a>b)?b:a
#define max(a,b) (a>b)?a:b
void swap(int a,int b) {int t = a;a = b; b = t;}
struct node
{
    int x1,y1,x2,y2;
};
node g,h;
int judge(node p1,node p2)
{
    double h1,h2;
    h1 = p2.x1*(p1.y2-p1.y1) + p1.x2*p1.y1 - p1.x1 * p1.y2 - p2.y1*(p1.x2 - p1.x1);
    h2 = p2.x2*(p1.y2-p1.y1) + p1.x2*p1.y1 - p1.x1*p1.y2 - p2.y2*(p1.x2 - p1.x1);
    if(h1*h2<=0)
        return 1;
    else
        return 0;
}
bool OK(node g,node h)//推断是否在矩形里面
{
    if(g.x1>=h.x1 &&g.x1<=h.x2 && g.x2>=h.x1&&g.x2<=h.x2 &&
        g.y2>=h.y1&&g.y2<=h.y2 &&g.y1>=h.y1&&g.y1<=h.y2)
     {
         return 1;
     }
     return 0;
}
int main()
{
    int n;
    scanf("%d",&n);
    while(n--)
     {
      scanf("%d%d",&g.x1,&g.y1);
      scanf("%d%d",&g.x2,&g.y2);
      scanf("%d%d",&h.x1,&h.y1);
      scanf("%d%d",&h.x2,&h.y2);

    //  printf("%d %d %d %d\n",h.x1,h.y1,h.x2,h.y2);

     if(h.x1>h.x2)
         swap(h.x1,h.x2);
     if(h.y1>h.y2)
        swap(h.y1,h.y2);
        //printf("%d %d %d %d\n",h.x1,h.y1,h.x2,h.y2);
     if(OK(g,h))
     {
         puts("T");
         continue;
     }
      if(judge(g,h)==1 && judge(h,g)==1 )//推断主对角线
          {
           puts("T");
           continue;
          }

        int flag = 0;
       // h.y1 = 12; h.y2 = 20;
          //swap(h.y1,h.y2);
          int t = h.y1; h.y1 = h.y2; h.y2 = t;
       // printf("%d %d %d %d\n",h.x1,h.y1,h.x2,h.y2);
         if(judge(g,h) && judge(h,g))//还有一条对角线
            {
                flag=1;
            }
            (flag==1)?

puts("T"):puts("F");
     }
        // printf("%d %d %d %d\n",h.x1,h.y1,h.x2,h.y2);
    return 0;
}
时间: 2024-08-07 04:30:57

POJ 1410 Intersection(计算几何)的相关文章

POJ 1410 Intersection(线段相交&amp;&amp;判断点在矩形内&amp;&amp;坑爹)

Intersection 大意:给你一条线段,给你一个矩形,问是否相交. 相交:线段完全在矩形内部算相交:线段与矩形任意一条边不规范相交算相交. 思路:知道具体的相交规则之后题其实是不难的,但是还有个坑点就是题目里明明说给的是矩形左上角跟右下角的点,但实际上不是,需要重新判断一下...真坑. 1 struct Point 2 { 3 double x, y; 4 } A, B, C, D; 5 struct Line 6 { 7 Point a, b; 8 } L; 9 10 int n; 11

POJ 1410 Intersection(线段相交&amp;amp;&amp;amp;推断点在矩形内&amp;amp;&amp;amp;坑爹)

Intersection 大意:给你一条线段,给你一个矩形,问是否相交. 相交:线段全然在矩形内部算相交:线段与矩形随意一条边不规范相交算相交. 思路:知道详细的相交规则之后题事实上是不难的,可是还有个坑点就是题目里明明说给的是矩形左上角跟右下角的点,但实际上不是,须要又一次推断一下...真坑. struct Point { double x, y; } A, B, C, D; struct Line { Point a, b; } L; int n; double xmult(Point p1

poj 1410 Intersection (判断线段与矩形相交 判线段相交)

题目链接 Intersection Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12040   Accepted: 3125 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:

POJ 1410 Intersection (线段和矩形相交)

题目: 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 doe

POJ 1410 Intersection

这题我就是用最原始的思考方法,其中有许多细节要注意.主体思想就是四条边分别和线段比较. 线段在矩形内要考虑. 我的代码有点乱有点长,其中有的部分可以写成函数. #include<cstdio> #include<cstring> #include<algorithm> #include<vector> using namespace std; int x_s,y_s,x_e,y_e,x_l,y_t,x_r,y_b; bool f1(int x) { retu

POJ 1410 Intersection 数据错误

题目要求判断一条线段和一个矩形是否相交,或者是否在矩形里面(题目好像没说?) 思路就是直接暴力判断和矩形四条边是否相交,和线段的坐标是否在矩形的坐标范围即可. 然后题目的数据,(xleft,ytop) 和 (xright,ybottom)不是按顺序给出的,需要自己判断下顺序. #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algori

POJ 1410 Intersection --几何,线段相交

题意: 给一条线段,和一个矩形,问线段是否与矩形相交或在矩形内. 解法: 判断是否在矩形内,如果不在,判断与四条边是否相交即可.这题让我发现自己的线段相交函数有错误的地方,原来我写的线段相交函数就是单纯做了两次跨立实验,在下图这种情况是错误的: 这样的话线段与右边界的两次跨立实验(叉积<=0)都会通过,但是并不相交. 所以要加快速排斥. 还有就是这题题目说给出的不一定是左上角,右下角依次的顺序.所以干脆重新自己定义左上角,右下角. 代码: #include <iostream> #inc

简单几何(线段相交) POJ 1410 Intersection

题目传送门 题意:一个矩形和一条线段,问是否有相交 分析:考虑各种情况.坑点:给出的矩形的两个端点是无序的,还有线段完全在矩形内也算相交 /************************************************ * Author :Running_Time * Created Time :2015/10/27 星期二 13:17:49 * File Name :POJ_1410.cpp ******************************************

poj 1410 计算几何

1 /** 2 注意: 千万得小心..就因为一个分号,调了一个晚上... 3 **/ 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 struct point { 8 int x,y; 9 }; 10 11 struct line{ 12 point a; 13 point b; 14 }; 15 line l,lt;//l 直线 lt 矩形的边 16 int x1,x2,y1,y2; 17