#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 {
    point point_A, point_B;
    line(point A, point B) {
        point_A = A, point_B = B;
    }
    line() = default;
};

point findMeetingPoint(line line_A, line line_B) {
    double x1 = line_A.point_A.x, y1 = line_A.point_A.y;
    double x2 = line_A.point_B.x, y2 = line_A.point_B.y;
    double x3 = line_B.point_A.x, y3 = line_B.point_A.y;
    double x4 = line_B.point_B.x, y4 = line_B.point_B.y;
    double denom = (x1-x2)*(y3-y4)-(y1-y2)*(x3-x4);
    if (fabs(denom)<1e-6) {
        return {(double)-1.000000,(double)-1.000000};
    }
    double px = ((x1 * y2 - y1*x2)*(x3-x4)- (x1 - x2) * (x3 * y4 - y3 * x4)) / denom;
    double py = ((x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4)) / denom;
    cout<<fixed<<setprecision(6);
    return {px,py};
}

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 << ans.x << " " << ans.y;
    return 0;
}

这个题目不能用联立两个截距式来做,因为:

  1. 如果二者平行,k1-k2=0,分母为0,无法除0,导致错误。
  2. 如果垂直或者平行于x轴,k=dx/dy或者dy/dx,不能兼顾两种情况都不为0。

所以,应该:使用两点式:

然后化为:

令:

line_A:

line_B:

使用克莱姆法则求解即可。得到: