Skip to main content
 首页 » 编程设计

python之根据与 A 点的距离在直线上找到 B 点

2025年05月04日17落叶无声

我有线方程、A 点和距离,需要在直线上的距离边缘找到 B 点。 我有 2 个等式:

distance = math.sqrt((pt1[0] - pt2[0])**2 + (pt1[1] - pt2[1])**2) 
pt2[1] = m*pt2[0] + n 

距离、pt1、m 和 n 已知。

如何在 Python 中实现此计算? 也许有一个 Python 库可以为我做这件事?

请您参考如下方法:

给定直线 y=mx+b,斜率 m 告诉我们 x (dx) 的变化与y (dy) 的变化。

数学:

// Given 
point_b = (point_a[0]+dx,point_a[1]+dy) 
other_possible_point_b = (point_a[0]-dx,point_a[1]-dy) 
dy = m*dx 
x**2 + y**2 = distance 
 
// Calculations 
dx**2 + (m*dx)**2 = distance 
(m**2+1)(dx**2) = distance 
dx = sqrt(distance/(m**2+1)) 
dy = m*sqrt(distance/(m**2+1)) 

Python 解决方案:

from math import sqrt 
 
point_b = (point_a[0]+dx(distance,m), point_a[1]+dy(distance,m)) 
other_possible_point_b = (point_a[0]-dx(distance,m), point_a[1]-dy(distance,m)) # going the other way 
 
def dy(distance, m): 
    return m*dx(distance, m) 
 
def dx(distance, m): 
    return sqrt(distance/(m**2+1))