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),

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

相关推荐
一乐小哥23 分钟前
一口气同步10年豆瓣记录———豆瓣书影音同步 Notion分享 🚀
后端·python
华研前沿标杆游学33 分钟前
华为在国内搞的研发基地有多野?标杆游学带你解锁“研发界顶流”
python
小胖墩有点瘦37 分钟前
【基于深度学习的中草药识别系统】
人工智能·python·深度学习·课程设计·计算机毕业设计·中草药识别
正在走向自律1 小时前
Ubuntu系统下Python连接国产KingbaseES数据库实现增删改查
开发语言·数据库·python·ubuntu·kingbasees·ksycopg2
Calihen的学习日志2 小时前
【Pandas】3.1-数据预处理:列的基本操作
python·pandas
打螺丝否2 小时前
稠密矩阵和稀疏矩阵的对比
python·机器学习·矩阵
这里有鱼汤2 小时前
你以为 FastAPI 足够强?其实 Litestar 能让你的项目更轻量高效
后端·python
大学生毕业题目2 小时前
毕业项目推荐:83-基于yolov8/yolov5/yolo11的农作物杂草检测识别系统(Python+卷积神经网络)
人工智能·python·yolo·目标检测·cnn·pyqt·杂草识别
Kyln.Wu2 小时前
【python实用小脚本-205】[HR揭秘]手工党逐行查Bug的终结者|Python版代码质量“CT机”加速器(建议收藏)
开发语言·python·bug
计算机毕业设计木哥2 小时前
Python毕业设计推荐:基于Django的饮食计划推荐与交流分享平台 饮食健康系统 健康食谱计划系统
开发语言·hadoop·spring boot·后端·python·django·课程设计