Python Sympy:轻松定位垂足和对称点

求解某个点关于一条直线的垂足对称点 是几何运算中经常遇到的问题。

本篇介绍如何用 PythonSympy 库来轻松的找到垂足对称点,不需要进行任何代数公式的推导。

1. 垂足问题

垂足 是指从一点向直线作垂线,垂线与直线的交点即为垂足。

所以,知道任意点的坐标 <math xmlns="http://www.w3.org/1998/Math/MathML"> P 0 ( x 0 , y 0 ) P_0 (x_0, y_0) </math>P0(x0,y0)和直线的方程 <math xmlns="http://www.w3.org/1998/Math/MathML"> y = k x + b y = kx+b </math>y=kx+b,就能算出垂足的坐标。

比如,已知三个点的坐标: <math xmlns="http://www.w3.org/1998/Math/MathML"> P 0 ( x 0 , y 0 ) , P 1 ( x 1 , y 1 ) , P 2 ( x 2 , y 2 ) P_0(x_0, y_0), P_1(x_1, y_1), P_2(x_2, y_2) </math>P0(x0,y0),P1(x1,y1),P2(x2,y2),其中 <math xmlns="http://www.w3.org/1998/Math/MathML"> P 1 P_1 </math>P1和 <math xmlns="http://www.w3.org/1998/Math/MathML"> P 2 P_2 </math>P2确定一条直线,

求点 <math xmlns="http://www.w3.org/1998/Math/MathML"> P 0 P_0 </math>P0在直线上的垂足。

解决的思路:

  1. 根据 <math xmlns="http://www.w3.org/1998/Math/MathML"> P 1 ( x 1 , y 1 ) , P 2 ( x 2 , y 2 ) P_1(x_1, y_1), P_2(x_2, y_2) </math>P1(x1,y1),P2(x2,y2)算出直线的方程 <math xmlns="http://www.w3.org/1998/Math/MathML"> y = k x + b y = kx+b </math>y=kx+b
  2. 假设垂足坐标 <math xmlns="http://www.w3.org/1998/Math/MathML"> P ( x , y ) P(x, y) </math>P(x,y)
  3. 垂足在直线上,代入坐标可得 <math xmlns="http://www.w3.org/1998/Math/MathML"> k x + b − y = 0 kx+b-y = 0 </math>kx+b−y=0
  4. 互相垂直的两个向量 <math xmlns="http://www.w3.org/1998/Math/MathML"> P P 0 PP_0 </math>PP0和 <math xmlns="http://www.w3.org/1998/Math/MathML"> P 1 P 2 P_1P_2 </math>P1P2内积为 <math xmlns="http://www.w3.org/1998/Math/MathML"> 0 0 </math>0,

即 <math xmlns="http://www.w3.org/1998/Math/MathML"> ( x − x 0 ) × ( x 1 − x 2 ) + ( y − y 0 ) × ( y 1 − y 2 ) = 0 (x - x_0) \times (x_1 - x_2) + (y - y_0) \times (y_1 - y_2)=0 </math>(x−x0)×(x1−x2)+(y−y0)×(y1−y2)=0

根据上面步骤34中的两个方程,就能算出垂足 的坐标。

当然,借助Sympy库,我们不用自己去推导计算过程,只要列出解决的思路即可。

python 复制代码
# 根据两点求直线的斜率和截距
def get_line(p1, p2):
    k = Symbol("k")
    b = Symbol("b")

    expr1 = p1[0] * k + b - p1[1]
    expr2 = p2[0] * k + b - p2[1]

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

# 已知三个点
def get_foot_from_points(p1, p2, p0):
    # 垂足的坐标
    x = Symbol("x")
    y = Symbol("y")
    l = get_line(p1, p2)

    # 垂足P位于直线上
    expr1 = x * l["k"] + l["b"] - y
    
    # 向量 PP0 和 P1P2 的内积为0
    expr2 = (x - p0[0]) * (p1[0] - p2[0]) + (y - p0[1]) * (p1[1] - p2[1])

    ret = solve((expr1, expr2), dict=True)
    return np.array([float(ret[0][x]), float(ret[0][y]), 0])

2. 对称点问题

对称点 则是指某一点相对于直线对称的点。
对称点 <math xmlns="http://www.w3.org/1998/Math/MathML"> P P </math>P的计算思路和计算垂足 类似,其实某个点 <math xmlns="http://www.w3.org/1998/Math/MathML"> P 0 P_0 </math>P0与它对称点 <math xmlns="http://www.w3.org/1998/Math/MathML"> P P </math>P的中点就是垂足

所以,已知三个点的坐标: <math xmlns="http://www.w3.org/1998/Math/MathML"> P 0 ( x 0 , y 0 ) , P 1 ( x 1 , y 1 ) , P 2 ( x 2 , y 2 ) P_0(x_0, y_0), P_1(x_1, y_1), P_2(x_2, y_2) </math>P0(x0,y0),P1(x1,y1),P2(x2,y2),其中 <math xmlns="http://www.w3.org/1998/Math/MathML"> P 1 P_1 </math>P1和 <math xmlns="http://www.w3.org/1998/Math/MathML"> P 2 P_2 </math>P2确定一条直线 <math xmlns="http://www.w3.org/1998/Math/MathML"> l l </math>l,

则点 <math xmlns="http://www.w3.org/1998/Math/MathML"> P 0 P_0 </math>P0对于直线 <math xmlns="http://www.w3.org/1998/Math/MathML"> l l </math>l的对称点 <math xmlns="http://www.w3.org/1998/Math/MathML"> P P </math>P的计算思路:

  1. 根据 <math xmlns="http://www.w3.org/1998/Math/MathML"> P 1 ( x 1 , y 1 ) , P 2 ( x 2 , y 2 ) P_1(x_1, y_1), P_2(x_2, y_2) </math>P1(x1,y1),P2(x2,y2)算出直线的方程 <math xmlns="http://www.w3.org/1998/Math/MathML"> y = k x + b y = kx+b </math>y=kx+b
  2. 假设对称点坐标 <math xmlns="http://www.w3.org/1998/Math/MathML"> P ( x , y ) P(x, y) </math>P(x,y)
  3. <math xmlns="http://www.w3.org/1998/Math/MathML"> P 0 P_0 </math>P0和对称点 <math xmlns="http://www.w3.org/1998/Math/MathML"> P P </math>P的中点在直线上,代入坐标可得 <math xmlns="http://www.w3.org/1998/Math/MathML"> k × ( x + x 0 ) / 2 + b − ( y + y 0 ) / 2 = 0 k\times(x+x_0)/2+b-(y+y_0)/2 = 0 </math>k×(x+x0)/2+b−(y+y0)/2=0
  4. 互相垂直的两个向量 <math xmlns="http://www.w3.org/1998/Math/MathML"> P P 0 PP_0 </math>PP0和 <math xmlns="http://www.w3.org/1998/Math/MathML"> P 1 P 2 P_1P_2 </math>P1P2内积为 <math xmlns="http://www.w3.org/1998/Math/MathML"> 0 0 </math>0,

即 <math xmlns="http://www.w3.org/1998/Math/MathML"> ( x − x 0 ) × ( x 1 − x 2 ) + ( y − y 0 ) × ( y 1 − y 2 ) = 0 (x - x_0) \times (x_1 - x_2) + (y - y_0) \times (y_1 - y_2)=0 </math>(x−x0)×(x1−x2)+(y−y0)×(y1−y2)=0

根据上面步骤34中的两个方程,就能算出对称点的坐标。

python 复制代码
# 关于直线的对称点
# p1和p2在直线l上,计算p0关于l的对称点
def symmetry_point(p1, p2, p0):
    # 对称点坐标
    x = Symbol("x")
    y = Symbol("y")

    l = get_line(p1, p2)

    # (x, y) 和 p 的中点在直线上
    expr1 = l["k"] * (x + p0[0]) / 2 + l["b"] - (y + p0[1]) / 2
    # 内积为0
    expr2 = (x - p0[0]) * (p1[0] - p2[0]) + (y - p0[1]) * (p1[1] - p2[1])
    ret = solve((expr1, expr2), dict=True)

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

3. 总结

再一次看到Sympy的符号化计算的强大之处,它节约了我们在求解问题时计算和推导的时间,

而这部分恰恰是耗时最多的部分。

活用Sympy库,极大的降低了用程序来解决代数和几何问题的门槛。

相关内容回顾

  1. Python Sympy:解方程利器
  2. Python Sympy:计算微积分利器
  3. Python Sympy:优雅解决动态直线交点的实时追踪
相关推荐
waterHBO17 分钟前
python 爬虫 selenium 笔记
爬虫·python·selenium
编程零零七1 小时前
Python数据分析工具(三):pymssql的用法
开发语言·前端·数据库·python·oracle·数据分析·pymssql
AIAdvocate3 小时前
Pandas_数据结构详解
数据结构·python·pandas
小言从不摸鱼3 小时前
【AI大模型】ChatGPT模型原理介绍(下)
人工智能·python·深度学习·机器学习·自然语言处理·chatgpt
FreakStudio5 小时前
全网最适合入门的面向对象编程教程:50 Python函数方法与接口-接口和抽象基类
python·嵌入式·面向对象·电子diy
redcocal6 小时前
地平线秋招
python·嵌入式硬件·算法·fpga开发·求职招聘
artificiali7 小时前
Anaconda配置pytorch的基本操作
人工智能·pytorch·python
RaidenQ7 小时前
2024.9.13 Python与图像处理新国大EE5731课程大作业,索贝尔算子计算边缘,高斯核模糊边缘,Haar小波计算边缘
图像处理·python·算法·课程设计
花生了什么树~.7 小时前
python基础知识(六)--字典遍历、公共运算符、公共方法、函数、变量分类、参数分类、拆包、引用
开发语言·python
Trouvaille ~8 小时前
【Python篇】深度探索NumPy(下篇):从科学计算到机器学习的高效实战技巧
图像处理·python·机器学习·numpy·信号处理·时间序列分析·科学计算