#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; }; // Ax+By+C = 0 // 直线一般式适用平面上任意直线 // 根据两点求解一般式的系数 // 设两个点为 (x1, y1) , (x2, y2),则有: // A = y2 - y1 // B = x1 - x2 // C = x2y1-x1y2 // 首先设交点坐标为 (x, y),两线段对应直线的一般式为: // a1x + b1y + c1 = 0 // a2x + b2y + c2 = 0 // 那么对 1 式乘 a2,对 2 式乘 a1 得: // a2*a1x + a2*b1y + a2*c1 = 0 // a1*a2x + a1*b2y + a1*c2 = 0 // 两式相减得: // y = (c1 * a2 - c2 * a1) / (a1 * b2 - a2 * b1) // 同样可以推得: // x = (c2 * b1 - c1 * b2) / (a1 * b2 - a2 * b1) // 如果(x,y)在两线段上,则(x,y)即为答案,否则交点不存在。 point findMeetingPoint(line line_A,line line_B){ // TODO:求直线 line_A 与 line_B 的交点 // 先求直线一般式的表示方法 double a1 = line_A.point_B.y - line_A.point_A.y; double b1 = line_A.point_A.x - line_A.point_B.x; double c1 = line_A.point_B.x * line_A.point_A.y - line_A.point_A.x * line_A.point_B.y; double a2 = line_B.point_B.y - line_B.point_A.y; double b2 = line_B.point_A.x - line_B.point_B.x; double c2 = line_B.point_B.x * line_B.point_A.y - line_B.point_A.x * line_B.point_B.y; double y = - ( a2*c1-a1*c2 )/( a2*b1-a1*b2 ); double x = - ( b2*c1-b1*c2 )/( b2*a1-b1*a2 ); point re = point(x,y); if (a2*b1-a1*b2==0|| b2*a1-b1*a2==0) { re.x =-1; re.y =-1; } return re; } 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(11)<<ans.x<<" "<<ans.y; return 0; }