class Point:
def __init__(self, x=0.0, y=0.0):
self.x = x
self.y = y
class Line:
def __init__(self, point_a=None, point_b=None):
self.point_A = point_a if point_a is not None else Point()
self.point_B = point_b if point_b is not None else Point()
def find_meeting_point(line_A, line_B):
# TODO: 在这里输入你的代码,求直线 line_A 与 line_B 的交点
x1,x2 = line_A.point_A.x,line_A.point_B.x
y1,y2 = line_A.point_A.y,line_A.point_B.y
x3,x4 = line_B.point_A.x,line_B.point_B.x
y3,y4 = line_B.point_A.y,line_B.point_B.y
den = (x1-x2) * (y3-y4) - (y1-y2) * (x3-x4)
if abs(den) < 1e-12: # 平行或重合
return Point(-1, -1)
x = ((x1*y2 - y1*x2) * (x3-x4) - (x1-x2) * (x3*y4 - y3*x4)) / den
y = ((x1*y2 - y1*x2) * (y3-y4) - (y1-y2) * (x3*y4 - y3*x4)) / den
return Point(x,y)
def main():
data = list(map(float, input().split()))
data.extend(list(map(float, input().split())))
A = Point(data[0], data[1])
B = Point(data[2], data[3])
C = Point(data[4], data[5])
D = Point(data[6], data[7])
AB = Line(A, B)
CD = Line(C, D)
ans = find_meeting_point(AB, CD)
print(f"{ans.x} {ans.y}")
if __name__ == "__main__":
main()
犯错误:刚开始错写成 line_A.point_a,实际应该是line_A.point_A,用self.point_A,而不是输入的

京公网安备 11010502036488号