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

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

相关推荐
wyiyiyi2 小时前
【Web后端】Django、flask及其场景——以构建系统原型为例
前端·数据库·后端·python·django·flask
mit6.8242 小时前
[1Prompt1Story] 滑动窗口机制 | 图像生成管线 | VAE变分自编码器 | UNet去噪神经网络
人工智能·python
没有bug.的程序员2 小时前
JVM 总览与运行原理:深入Java虚拟机的核心引擎
java·jvm·python·虚拟机
甄超锋2 小时前
Java ArrayList的介绍及用法
java·windows·spring boot·python·spring·spring cloud·tomcat
AntBlack3 小时前
不当韭菜V1.1 :增强能力 ,辅助构建自己的交易规则
后端·python·pyqt
杜子不疼.5 小时前
《Python学习之字典(一):基础操作与核心用法》
开发语言·python·学习
myzzb6 小时前
基于uiautomation的自动化流程RPA开源开发演示
运维·python·学习·算法·自动化·rpa
TLuoQiu6 小时前
小电视视频内容获取GUI工具
爬虫·python
我叫黑大帅6 小时前
【CustomTkinter】 python可以写前端?😆
后端·python
胡耀超6 小时前
DataOceanAI Dolphin(ffmpeg音频转化教程) 多语言(中国方言)语音识别系统部署与应用指南
python·深度学习·ffmpeg·音视频·语音识别·多模态·asr