tzoj3348 线段相交Ⅲ
就是个解方程。
#include <bits/stdc++.h> #define IO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); using namespace std; double ansx, ansy; bool pingxing(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) { return (y2 - y1) * (x4 - x3) == (x2 - x1) * (y4 - y3); } bool xiangjiao(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) { double t1, t2; t1 = ((x3 - x1) * (y4 - y3) - (y3 - y1) * (x4 - x3)) / ((x2 - x1) * (y4 - y3) - (x4 - x3) * (y2 - y1)); t2 = ((x1 - x3) * (y2 - y1) + (y3 - y1) * (x2 - x1)) / ((x4 - x3) * (y2 - y1) - (x2 - x1) * (y4 - y3)); if(t1 >= 0 && t1 <= 1 && t2 >= 0 && t2 <= 1) { ansx = x1 + t1 * (x2 - x1); ansy = y1 + t1 * (y2 - y1); return 1; } return 0; } bool gongxian(double x1, double y1, double x2, double y2, double x3, double y3) { return (y3 - y1) * (x2 - x1) == (x3 - x1) * (y2 - y1); } bool inside(double x1, double y1, double x2, double y2, double x3, double y3) { return (x3 >= min(x1, x2) && x3 <= max(x1, x2) && y3 >= min(y1, y2) && y3 <= max(y1, y2)); } signed main() { IO; int t; cin >> t; double x1, y1, x2, y2, x3, y3, x4, y4; while(t--) { cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4; if (pingxing(x1, y1, x2, y2, x3, y3, x4, y4)) { if (gongxian(x1, y1, x2, y2, x3, y3)) { if (inside(x1, y1, x2, y2, x3, y3)) { cout << "YES" << endl; continue; } if (inside(x1, y1, x2, y2, x4, y4)) { cout << "YES" << endl; continue; } } cout << "NO" << endl; continue; } else { if (xiangjiao(x1, y1, x2, y2, x3, y3, x4, y4)) { cout << "YES"; if ((ansx != x1 || ansy != y1) && (ansx != x2 || ansy != y2) && (ansx != x3 || ansy != y3) && (ansx != x4 || ansy != y4)) cout << fixed << setprecision(3) << " (" << ansx << "," << ansy << ")"; cout << endl; } else cout << "NO" << endl; } } return 0; }