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

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

相关推荐
pianmian112 分钟前
python每日十题(10)
开发语言·python
王有品22 分钟前
python之横条形图实例
开发语言·python
Python数据分析与机器学习30 分钟前
《基于Python的财务数据可视化与决策支持系统开发》开题报告
大数据·开发语言·人工智能·python·深度学习·信息可视化
轻松Ai享生活39 分钟前
使用 Python 将工作流程自动化
人工智能·python
小宁爱Python1 小时前
Python从入门到精通2:SQLite数据库(FastAPI + SQLite全流程开发指南)
数据库·python·sqlite·fastapi
xinxiyinhe1 小时前
Python在图像处理领域的第三方库支持(三)
开发语言·图像处理·python
李昊哲小课1 小时前
垃圾短信分类
人工智能·python·机器学习·自然语言处理·分类·数据挖掘·中文分词
长安海1 小时前
SPPAS安装及问题汇总
python·sppas
Cccc吃吃吃2 小时前
python中深浅拷贝以及可变对象和不可变对象
开发语言·jvm·python·beautifulsoup·numpy·pyqt·fastapi
hx20243 小时前
R语言ggplot2散点形状和填充
开发语言·python·r语言