题解 | 两直线交点
两直线交点
https://www.nowcoder.com/practice/b14130882e704fd6aa57f8762ae17bac
#include <bits/stdc++.h>
#include <iomanip>
using namespace std;
struct point {
double x, y;
point(double A, double B) {
x = A, y = B;
}
point() = default;
};
struct line {
double A, B, C;
line(point p1, point p2) {
A = p2.y - p1.y;
B = p1.x - p2.x;
C = p2.x*p1.y - p1.x*p2.y;
}
line() = default;
};
point findMeetingPoint(line L1, line L2) {
double A1 = L1.A, B1 = L1.B, C1 = L1.C;
double A2 = L2.A, B2 = L2.B, C2 = L2.C;
if (A1*B2 == A2*B1) return {-1, -1};
return {(B1*C2-B2*C1)/(A1*B2-A2*B1), (A1*C2-A2*C1)/(A2*B1-A1*B2)};
}
int main() {
point A, B, C, D;
cin >> A.x >> A.y >> B.x >> B.y >> C.x >> C.y >> D.x >> D.y;
line AB = line(A, B);
line CD = line(C, D);
point ans = findMeetingPoint(AB, CD);
cout << fixed << setprecision(6);
cout << ans.x << " " << ans.y;
return 0;
}
查看2道真题和解析