POJ_1410

    几个需要注意的地方:

    ①给出的矩形顶点的坐标需要自己重新排下序再使用,看discuss说所谓的top等并不是严格指上方,只是一个相对的参考,所以要重新排下序再用。

    ②因为题目里面说了矩形的内部也算矩形的一部分,所以线段在矩形内部是认为和矩形相交的。

    ③在判断线段与矩形四个边是否有交点时,要注意对非规范相交的判定,当线段和边共线且不相交时叉积也为0。

#include<stdio.h>
#include<string.h>
#define zero 1e-8
double xs, ys, xe, ye, xl, yl, xr, yr, x1[5], y1[5], x2[5], y2[5];
double det(double x1, double y1, double x2, double y2)
{
return x1 * y2 - y1 * x2;
}
double fabs(double x)
{
return x < 0 ? -x : x;
}
double min(double x, double y)
{
return x < y ? x : y;
}
double max(double x, double y)
{
return x > y ? x : y;
}
int dcmp(double x)
{
if(fabs(x) < zero)
return 0;
if(x < 0)
return -1;
return 1;
}
int onseg(double x, double y, double x1, double y1, double x2, double y2)
{
if(fabs(x2 - x1) < fabs(y2 - y1))
return dcmp((y - y2) * (y - y1)) <= 0;
else
return dcmp((x - x2) * (x - x1)) <= 0;
}
void solve()
{
int i, j, k;
double t1, t2, t3, t4;
x1[0] = xl, y1[0] = yr, x2[0] = xr, y2[0] = yr;
x1[1] = xl, y1[1] = yr, x2[1] = xl, y2[1] = yl;
x1[2] = xl, y1[2] = yl, x2[2] = xr, y2[2] = yl;
x1[3] = xr, y1[3] = yr, x2[3] = xr, y2[3] = yl;
for(i = 0; i < 4; i ++)
{
t1 = det(x2[i] - x1[i], y2[i] - y1[i], xs - x1[i], ys - y1[i]);
t2 = det(x2[i] - x1[i], y2[i] - y1[i], xe - x1[i], ye - y1[i]);
t3 = det(xe - xs, ye - ys, x1[i] - xs, y1[i] - ys);
t4 = det(xe - xs, ye - ys, x2[i] - xs, y2[i] - ys);
if(dcmp(t1) * dcmp(t2) < 0 && dcmp(t3) * dcmp(t4) < 0)
{
printf("T\n");
return ;
}
if((dcmp(t1) == 0 && onseg(xs, ys, x1[i], y1[i], x2[i], y2[i])) || (dcmp(t2) == 0 && onseg(xe, ye, x1[i], y1[i], x2[i], y2[i])) || (dcmp(t3) == 0 && onseg(x1[i], y1[i], xs, ys, xe, ye)) ||(dcmp(t4) == 0 && onseg(x2[i], y2[i], xs, ys, xe, ye)))
{
printf("T\n");
return ;
}
}
printf("F\n");
}
int main()
{
int i, t;
double x;
scanf("%d", &t);
while(t --)
{
scanf("%lf%lf%lf%lf%lf%lf%lf%lf", &xs, &ys, &xe, &ye, &xl, &yl, &xr, &yr);
if(xl > xr)
x = xl, xl = xr, xr = x;
if(yl < yr)
x = yl, yl = yr, yr = x;
if((xs < xr && xs > xl && ys < yl && ys > yr) || (xe < xr && xe > xl && ye < yl && ye > yr))
printf("T\n");
else
solve();
}
return 0;
}


posted on 2012-02-10 20:44  Staginner  阅读(340)  评论(0编辑  收藏  举报