现在的位置: 首页 > 综合 > 正文

POJ 1410

2013年06月25日 ⁄ 综合 ⁄ 共 1272字 ⁄ 字号 评论关闭

练习基本的计算几何。

主要这个题意很BUG,线段在矩形内要T,这个看出来了,没看到输入不保证 -左上-右下-。。。

POJ 1410

/*
*9117030 MDK 1410 Accepted 688K 0MS G++ 2707B 2011-08-09 14:42:44
*/

using namespace std;
struct POINT {
int x,y;
POINT(
int a = 0,int b = 0) {
x
=a;y=b;
}
};

struct LINESEG {
POINT s;
POINT e;
LINESEG(POINT a,POINT b){s
=a;e=b;}
LINESEG() {}
}l;

struct RECTANGLE {
POINT s;
POINT e;
}r;

int n,m;
inline
double multiply(POINT sp,POINT ep,POINT op) {
return ((sp.x - op.x)*(ep.y-op.y) - (ep.x-op.x)*(sp.y-op.y));
}
bool intersect(LINESEG u,LINESEG v) {
return (
(max(u.s.x,u.e.x)
>= min(v.s.x,v.e.x)) &&
(max(v.s.x,v.e.x)
>= min(u.s.x,u.e.x)) &&
(max(u.s.y,u.e.y)
>= min(v.s.y,v.e.y)) &&
(max(v.s.y,v.e.y)
>= min(u.s.y,u.e.y)) &&

(multiply(v.s,u.e,u.s)
* multiply(u.e,v.e,u.s) >= 0) &&
(multiply(u.s,v.e,v.s)
* multiply(v.e,u.e,v.s) >= 0));
}

void init() {
m
= 0;
scanf(
"%d%d%d%d",&l.s.x,&l.s.y,&l.e.x,&l.e.y);
scanf(
"%d%d%d%d",&r.s.x,&r.s.y,&r.e.x,&r.e.y);
if(r.s.x > r.e.x) swap(r.s.x,r.e.x);
if(r.s.y < r.e.y) swap(r.s.y,r.e.y);

if((l.s.x >= r.s.x && l.s.x <= r.e.x &&
l.s.y
>= r.e.y && l.s.y <= r.s.y) ||
((l.e.x
>= r.s.x && l.e.x <= r.e.x &&
l.e.y
>= r.e.y && l.e.y <= r.s.y)))
m
= 1;

m
|= intersect(LINESEG(r.s,POINT(r.e.x,r.s.y)),l) ||
intersect(LINESEG(POINT(r.s.x,r.e.y),r.e),l)
||
intersect(LINESEG(r.s,POINT(r.s.x,r.e.y)),l)
||
intersect(LINESEG(POINT(r.e.x,r.s.y),r.e),l);

if(m) puts("T");
else puts("F");
}

int main() {
int cse;
scanf(
"%d",&cse);
while(cse --) {
init();
}

}

  

抱歉!评论已关闭.