import numpy as np
定义两个直线的参数方程
def line1(t):
x = 2 + 3t
y = 1 - t
z = 4 + 2t
return np.array([x, y, z])
def line2(s):
x = 1 + s
y = 2 - 2s
z = 3 + 3s
return np.array([x, y, z])
求解交点
def find_intersection():
# 构造直线的参数方程表示
A = np.array([line1(1) - line1(0), line2(1) - line2(0)]).T
b = line2(0) - line1(0)
# 使用线性代数库求解方程组
try:
t, s = np.linalg.solve(A, b)
intersection_point = line1(t)
return intersection_point
except np.linalg.LinAlgError:
# 方程组无解,直线平行或重合
return None
调用函数求解交点
intersection = find_intersection()
if intersection is not None:
print(“两直线的交点为:”, intersection)
else:
print(“两直线平行或重合,无交点”)