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

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

相关推荐
电子_咸鱼22 分钟前
高阶数据结构——并查集
数据结构·c++·vscode·b树·python·算法·线性回归
生信大杂烩26 分钟前
Xenium数据分析 | 使用Xenium Ranger重新分析数据
python·数据分析
郁大锤1 小时前
OpenAI responses使用教程(三) ——Responses create python SDK 介绍
人工智能·python·ai·openai
hardmenstudent1 小时前
Python字典--第1关:元组使用:这份菜单能修改吗?
开发语言·python
再__努力1点1 小时前
【02】深入理解Harris角点检测:从原理推导到实战实现
python·opencv·计算机视觉·特征提取
moeyui7051 小时前
Python文件编码读取和处理整理知识点
开发语言·前端·python
程序员爱钓鱼2 小时前
Python编程实战 - Python实用工具与库 - 正则表达式匹配(re 模块)
后端·python·面试
程序员爱钓鱼2 小时前
Python编程实战 - Python实用工具与库 - 爬取并存储网页数据
后端·python·面试
陈辛chenxin2 小时前
【大数据技术01】数据科学的基础理论
大数据·人工智能·python·深度学习·机器学习·数据挖掘·数据分析
蒋星熠3 小时前
爬虫中Cookies模拟浏览器登录技术详解
开发语言·爬虫·python·正则表达式·自动化·php·web