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:优雅解决动态直线交点的实时追踪
相关推荐
IVEN_2 小时前
只会Python皮毛?深入理解这几点,轻松进阶全栈开发
python·全栈
Ray Liang3 小时前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
AI攻城狮4 小时前
如何给 AI Agent 做"断舍离":OpenClaw Session 自动清理实践
python
千寻girling4 小时前
一份不可多得的 《 Python 》语言教程
人工智能·后端·python
AI攻城狮7 小时前
用 Playwright 实现博客一键发布到稀土掘金
python·自动化运维
曲幽7 小时前
FastAPI分布式系统实战:拆解分布式系统中常见问题及解决方案
redis·python·fastapi·web·httpx·lock·asyncio
孟健1 天前
Karpathy 用 200 行纯 Python 从零实现 GPT:代码逐行解析
python
码路飞1 天前
写了个 AI 聊天页面,被 5 种流式格式折腾了一整天 😭
javascript·python
曲幽1 天前
FastAPI压力测试实战:Locust模拟真实用户并发及优化建议
python·fastapi·web·locust·asyncio·test·uvicorn·workers