Python Sympy:优雅解决动态直线交点的实时追踪

最近在做一个数学的动画时,遇到一个需求。

简单来说,就是一个直线 <math xmlns="http://www.w3.org/1998/Math/MathML"> l 1 l_1 </math>l1在移动时,它与另一个直线 <math xmlns="http://www.w3.org/1998/Math/MathML"> l 2 l_2 </math>l2的交点如何定位?

制作动画的时候,当直线 <math xmlns="http://www.w3.org/1998/Math/MathML"> l 1 l_1 </math>l1移动时,需要不断重新计算它与 <math xmlns="http://www.w3.org/1998/Math/MathML"> l 2 l_2 </math>l2的新交点,然后在动画中渲染出来。

1. 问题描述

任意点 <math xmlns="http://www.w3.org/1998/Math/MathML"> A A </math>A和 <math xmlns="http://www.w3.org/1998/Math/MathML"> B B </math>B确定一条直线 <math xmlns="http://www.w3.org/1998/Math/MathML"> l 1 l_1 </math>l1,点 <math xmlns="http://www.w3.org/1998/Math/MathML"> C C </math>C和 <math xmlns="http://www.w3.org/1998/Math/MathML"> D D </math>D确定一条直线 <math xmlns="http://www.w3.org/1998/Math/MathML"> l 2 l_2 </math>l2。

两直线的交点 <math xmlns="http://www.w3.org/1998/Math/MathML"> O O </math>O。

当点 <math xmlns="http://www.w3.org/1998/Math/MathML"> B B </math>B移动时,动态绘制交点 <math xmlns="http://www.w3.org/1998/Math/MathML"> O O </math>O的变化。

2. 解决方法

解决的思路不难,首先计算交点 <math xmlns="http://www.w3.org/1998/Math/MathML"> O O </math>O的坐标,

1-1. 根据点 <math xmlns="http://www.w3.org/1998/Math/MathML"> A ( x 1 , y 1 ) A(x_1, y_1) </math>A(x1,y1)和点 <math xmlns="http://www.w3.org/1998/Math/MathML"> B ( x 2 , y 2 ) B(x_2, y_2) </math>B(x2,y2)可以计算出直线 <math xmlns="http://www.w3.org/1998/Math/MathML"> l 1 l_1 </math>l1的方程( <math xmlns="http://www.w3.org/1998/Math/MathML"> y = k 1 x + b 1 y = k_1x+b_1 </math>y=k1x+b1)

1-2. 根据点 <math xmlns="http://www.w3.org/1998/Math/MathML"> C ( x 3 , y 3 ) C(x_3, y_3) </math>C(x3,y3)和点 <math xmlns="http://www.w3.org/1998/Math/MathML"> D ( x 4 , y 4 ) D(x_4, y_4) </math>D(x4,y4)可以计算出直线 <math xmlns="http://www.w3.org/1998/Math/MathML"> l 2 l_2 </math>l2的方程( <math xmlns="http://www.w3.org/1998/Math/MathML"> y = k 2 x + b 2 y = k_2x+b_2 </math>y=k2x+b2)

1-3. 根据 <math xmlns="http://www.w3.org/1998/Math/MathML"> l 1 l_1 </math>l1的方程和 <math xmlns="http://www.w3.org/1998/Math/MathML"> l 2 l_2 </math>l2的方程可以计算出点 <math xmlns="http://www.w3.org/1998/Math/MathML"> O O </math>O的坐标

当点 <math xmlns="http://www.w3.org/1998/Math/MathML"> B B </math>B不断变化时:

2-1. 根据点 <math xmlns="http://www.w3.org/1998/Math/MathML"> A A </math>A和不断变化的点 <math xmlns="http://www.w3.org/1998/Math/MathML"> B B </math>B重新计算 <math xmlns="http://www.w3.org/1998/Math/MathML"> l 1 l_1 </math>l1的方程( <math xmlns="http://www.w3.org/1998/Math/MathML"> y = k 1 x + b 1 y = k_1x+b_1 </math>y=k1x+b1)

2-2. 根据 <math xmlns="http://www.w3.org/1998/Math/MathML"> l 1 l_1 </math>l1和 <math xmlns="http://www.w3.org/1998/Math/MathML"> l 2 l_2 </math>l2的方程不断重新计算点 <math xmlns="http://www.w3.org/1998/Math/MathML"> O O </math>O的坐标

这里麻烦的地方 在于需要推导出下面2个公式,然后用代码来实现:

  1. 根据两个点的坐标推导出直线方程中的斜率 <math xmlns="http://www.w3.org/1998/Math/MathML"> k k </math>k和截距 <math xmlns="http://www.w3.org/1998/Math/MathML"> b b </math>b
  2. 根据两条直线的方程推导出交点坐标

如果用Python Sympy库的话,那么一下就简单很多了。

2.1. 两点坐标求直线

根据两点坐标推导出直线斜率 <math xmlns="http://www.w3.org/1998/Math/MathML"> k k </math>k和截距 <math xmlns="http://www.w3.org/1998/Math/MathML"> b b </math>b:

python 复制代码
from sympy import Symbol, solve

def get_line(p1, p2):
    k = Symbol("k")
    b = Symbol("b")

    # 代入点p1坐标
    expr1 = p1[0] * k + b - p1[1]
    # 代入点p2坐标
    expr2 = p2[0] * k + b - p2[1]

    ret = solve((expr1, expr2), dict=True)
    return {"k": ret[0][k], "b": ret[0][b]}

2.2. 两条直线求交点

根据两条直线的斜率 <math xmlns="http://www.w3.org/1998/Math/MathML"> k k </math>k和截距 <math xmlns="http://www.w3.org/1998/Math/MathML"> b b </math>b推导交点的坐标:

python 复制代码
def cross_point(l1, l2):
    x = Symbol("x")
    y = Symbol("y")

    # 直线l1的方程
    expr1 = l1["k"] * x + l1["b"] - y
    # 直线l2的方程
    expr2 = l2["k"] * x + l2["b"] - y
    ret = solve((expr1, expr2), dict=True)

    return np.array((float(ret[0][x]), float(ret[0][y]), 0))

2.3. 实现效果

利用上面两个函数,可以很容易得到两条直线交点的坐标,渲染后的效果如下:

3. 总结

利用Sympy带来的最大便利 在于不需要推导公式,只要列出方程,交由Sympy去推导结果即可。

这样,我们在编写一些几何方面的运算时,并不需要了解多少数学的知识,也不用去记住那些求根公式等等。

同时,也可以大大简化代码,比如上面示例中封装的两个函数(get_linecross_point),

可以看出,不仅实现的代码很简短,可读性也提高很多,几乎不需要多少数学知识就能看懂。

相关推荐
西西弗Sisyphus20 分钟前
全面掌握Python时间处理
python·time
java1234_小锋2 小时前
一周学会Flask3 Python Web开发-http响应状态码
python·flask·flask3
奔跑吧邓邓子3 小时前
【Python爬虫(12)】正则表达式:Python爬虫的进阶利刃
爬虫·python·正则表达式·进阶·高级
码界筑梦坊4 小时前
基于Flask的京东商品信息可视化分析系统的设计与实现
大数据·python·信息可视化·flask·毕业设计
pianmian14 小时前
python绘图之箱型图
python·信息可视化·数据分析
csbDD4 小时前
2025年网络安全(黑客技术)三个月自学手册
linux·网络·python·安全·web安全
赔罪6 小时前
Python 高级特性-切片
开发语言·python
伊一大数据&人工智能学习日志6 小时前
selenium爬取苏宁易购平台某产品的评论
爬虫·python·selenium·测试工具·网络爬虫
说是用户昵称已存在6 小时前
Pycharm+CodeGPT+Ollama+Deepseek
ide·python·ai·pycharm
Fansv5877 小时前
深度学习-2.机械学习基础
人工智能·经验分享·python·深度学习·算法·机器学习