被坑一次,判断错误一次,不过A掉了。
题目输入left, top, right, bottom不一定按左上-右下输入,随机的需要判断。
而且线段在矩形内也算 'T' ;
判断是否相交问题,转化为判断 线段端点 与 边长线段 的位置关系和 边长线段端点 与线段之间的位置关系。
相互判断同时成立即可,一左一右, 一左另一个在线段上, 一右另一个在线段上
若共线单独思考(别人告诉我不用,我不会,各位求教下).
#include <stdio.h>
int left, top, right, bottom;
//判断线段是否与矩形各个边相交
int Judge (int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)
{
int a, b, c, d;
a = (x2 - x1) * (y4 - y1) - (x4 - x1) * (y2 - y1);
b = (x2 - x1) * (y3 - y1) - (x3 - x1) * (y2 - y1);
c = (x4 - x3) * (y2 - y3) - (x2 - x3) * (y4 - y3);
d = (x4 - x3) * (y1 - y3) - (x1 - x3) * (y4 - y3);
//两个线段不相交时不能共线
if ((a * b <= 0) && (c * d < 0) || (a * b < 0) && (c * d <= 0))
return 1;
return 0;
}
void swap(int *a, int *b)
{
*a ^= *b;
*b ^= *a;
*a ^= *b;
}
//线段端点在矩形内或在矩形上
int inside(int x, int y)
{
if (x >= left && x <= right && y >= bottom && y <= top)
return 1;
return 0;
}
int main()
{
int n, ok;
int x1, y1, x2, y2;
scanf("%d", &n);
while (n--)
{
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
scanf("%d%d%d%d", &left, &top, &right, &bottom);
//处理,使left < right && bottom < top
//题目没有这么给出,需要自己处理
if (left > right) swap(&left, &right);
if (top < bottom) swap(&top, &bottom);
if (inside(x1, y1) && inside(x2, y2))
ok = 1;
else if (Judge(x1, y1, x2, y2, left, bottom, right, bottom) == 1)
ok = 1;
else if (Judge(x1, y1, x2, y2, right, bottom, right, top) == 1)
ok = 1;
else if (Judge(x1, y1, x2, y2, right, top, left, top) == 1)
ok = 1;
else if (Judge(x1, y1, x2, y2, left, top, left, bottom) == 1)
ok = 1;
else
ok = 0;
printf("%c\n", ok ? 'T' : 'F');
}
return 0;
}