交点
def find_intersection(line1, line2):
# 提取线的端点
x1, y1, x2, y2 = line1
x3, y3, x4, y4 = line2
# 计算分母
denom = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1)
if denom == 0:
return None # 两条线平行或重叠
# 计算两个比例参数
ua = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)) / denom
ub = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) / denom
# 判断交点是否在线段上
if 0 <= ua <= 1 and 0 <= ub <= 1:
# 计算交点坐标
intersection_x = x1 + ua * (x2 - x1)
intersection_y = y1 + ua * (y2 - y1)
return (intersection_x, intersection_y)
else:
return None # 交点不在线段上
# 示例用法
line1 = (0, 0, 4, 4)
line2 = (0, 4, 4, 0)
intersection = find_intersection(line1, line2)
print(intersection) # 输出: (2.0, 2.0)
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.