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

标签 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

距离、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))

关于python - 根据与 A 点的距离在直线上找到 B 点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18843469/

相关文章:

python - 使用 swig 包装读/写类函数

python - 需要一种简单的方法来删除 python 中嵌套元组的重复项

math - 两个向量的旋转/方向

c++ - INT_MIN/-1 是 C++ 中定义的行为吗?

传单 v1.03 : Make CircleMarker draggable?

python - CGAL 的 python 绑定(bind)发生了什么?

python - 序列化器在 Django shell 中工作但在 View 中失败

python - Python中的EAFP原理是什么?

javascript - 如何计算给定发射 Angular 物体行进多远

javascript - Three.js 沿着某条线移动对象以指向并查看