Intersection POJ - 1410
题目链接:https://vjudge.net/problem/POJ-1410
题目:
题意:判断线段与矩形是否有公共区域。。。一开始我以为判断线段与矩形四条边是否相交就行。。。没想到还有线段在矩形内部也算相交。。。
// // Created by HJYL on 2020/1/13. // #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<queue> #include<cmath> #define eps 1e-8 #define Inf 0x7fffffff //#include<bits/stdc++.h> using namespace std; const int maxn=1e5+100; struct Point{ double x,y; }; double min(double a, double b) { return a < b ? a : b; } double max(double a, double b) { return a > b ? a : b; } bool IsSegmentIntersect(Point a, Point b, Point c, Point d) { if( min(a.x, b.x) > max(c.x, d.x) || min(a.y, b.y) > max(c.y, d.y) || min(c.x, d.x) > max(a.x, b.x) || min(c.y, d.y) > max(a.y, b.y) ) return 0; double h, i, j, k; h = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x); i = (b.x - a.x) * (d.y - a.y) - (b.y - a.y) * (d.x - a.x); j = (d.x - c.x) * (a.y - c.y) - (d.y - c.y) * (a.x - c.x); k = (d.x - c.x) * (b.y - c.y) - (d.y - c.y) * (b.x - c.x); return h * i <= eps && j * k <= eps; } int main() { int T; while(~scanf("%d",&T)) { while (T--) { Point ju1, ju2, ju3, ju4, gun1, gun2; double gunx, guny, gun1x, gun1y, zsx, zsy, yxx, yxy; scanf("%lf%lf%lf%lf%lf%lf%lf%lf", &gunx, &guny, &gun1x, &gun1y, &zsx, &zsy, &yxx, &yxy); ju1.x = zsx; ju1.y = zsy; ju2.x = zsx; ju2.y = yxy; ju3.x = yxx; ju3.y = zsy; ju4.x = yxx; ju4.y = yxy; gun1.x = gunx; gun1.y = guny; gun2.x = gun1x; gun2.y = gun1y; double a=min(gunx,gun1x); double a1=max(gunx,gun1x); double b=min(guny,gun1y); double b1=max(guny,gun1y); double c=min(zsx,yxx); double c1=max(zsx,yxx); double d=min(zsy,yxy); double d1=max(zsy,yxy); if (IsSegmentIntersect(gun1, gun2, ju1, ju2) || IsSegmentIntersect(gun1, gun2, ju1, ju3) || IsSegmentIntersect(gun1, gun2, ju2, ju4) || IsSegmentIntersect(gun1, gun2, ju3, ju4) || (a>=c&&a1<=c1&&b>=d&&b1<=d1) ) printf("T "); else printf("F "); } } return 0; }