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

mcl*_*fee 1 python math geometry

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

distance = math.sqrt((pt1[0] - pt2[0])**2 + (pt1[1] - pt2[1])**2)
pt2[1] = m*pt2[0] + n
Run Code Online (Sandbox Code Playgroud)

距离pt1,m和n是已知的。

我如何在python中实现此计算?也许有python库已经知道为我做这个了?

Ste*_*iao 5

给定直线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))
Run Code Online (Sandbox Code Playgroud)

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))
Run Code Online (Sandbox Code Playgroud)