有一个很明显的做法就是判断PointInPolygon 。枚举第二个矩形的点,是否在第一个矩形内,但是有bug
就是那种第二个矩形很大的那种,所以容易想到又枚举第一个矩形的点,看是否在第二个矩形里。
但是还是有bug。就是那种十字架的那种,大家都不属于大家,但是他们的对角线是相交的,判断对角线即可。
其实这题可以倒过来做。判断不相交。
1、如果第一个矩形的最大的x还比第二个矩形的最小的x小,那么永远不能相交。(画个图就可以)
2、.....类似的。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> #define IOS ios::sync_with_stdio(false) using namespace std; #define inf (0x3f3f3f3f) typedef long long int LL; #include <iostream> #include <sstream> #include <vector> #include <set> #include <map> #include <queue> #include <string> void work() { int mi_x[33]; int mi_y[33]; int mx_x[33]; int mx_y[33]; int xx1, yy1, xx2, yy2; for (int i = 1; i <= 2; ++i) { cin >> xx1 >> yy1 >> xx2 >> yy2; mi_x[i] = min(xx1, xx2); mx_x[i] = max(xx1, xx2); mi_y[i] = min(yy1, yy2); mx_y[i] = max(yy1, yy2); } if (mi_x[1] > mx_x[2] || mx_x[1] < mi_x[2] || mx_y[1] < mi_y[2] || mi_y[1] > mx_y[2]) { cout << "Miss" << endl; } else { cout << "Hit" << endl; } } int main() { #ifdef local freopen("data.txt","r",stdin); #endif int t; cin >> t; while (t--) work(); return 0; }