您的位置:首页 > 其它

POJ 1410 Intersection(计算几何)

2016-01-25 10:29 267 查看
题目链接: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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: