在另一条线的一点上绘制固定长度的垂直线 [英] Draw perpendicular line of fixed length at a point of another line
问题描述
我有两个点A(10,20)和B(15,30).这些点生成线AB.我需要在Python中的点B上绘制一条长度为6(每个方向为3个单位)的垂直线CD.
I have two points A (10,20) and B (15,30). The points generate a line AB. I need to draw a perpendicular line, CD, on point B with a length of 6 (each direction 3 units) in Python.
我已经具有使用以下代码的AB行的某些属性:
I already have some properties of line AB using the following code:
from scipy import stats
x = [10,15]
y = [20,30]
slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
如何计算C和D的位置.我需要它们的X和Y值.
How can I calculate the location of C and D. I need their X and Y value.
使用Shapely库将C和D的值用于完成另一个目标.
The value of C and D will be used for accomplishing another objective using the Shapely library.
推荐答案
如果 slope
是AB的斜率,则CD的斜率是 -1/slope
.这等于水平变化的垂直变化: dy/dx = -1/slope
.这样得出 dx = -slope * dx
.根据勾股定理,您有 3 ** 2 = dy ** 2 + dx ** 2
.替换 dx
,您会得到
If slope
is the slope of AB, then the slope of CD is -1/slope
. This is equal to vertical change over horizontal change: dy/dx = -1/slope
. This gives that dx = -slope*dx
. And by Pythagorean Theorem, you have 3**2 = dy**2+dx**2
. Substitute for dx
, and you get
3 ** 2 =(-slope * dy)** 2 + dy ** 2
3 ** 2 =(斜率** 2 + 1)* dy ** 2
dy ** 2 = 3 ** 2/(坡度** 2 + 1)
dy = math.sqrt(3 ** 2/(slope ** 2 + 1))
然后您将获得 dx = -slope * dy
.最后,您可以使用 dx
和 dy
来获取C和D.因此,代码为:
Then you can get dx = -slope*dy
. Finally, you can use dx
and dy
to get C and D. So the code would be:
import math
dy = math.sqrt(3**2/(slope**2+1))
dx = -slope*dy
C[0] = B[0] + dx
C[1] = B[1] + dy
D[0] = B[0] - dx
D[1] = B[1] - dy
(请注意,尽管 math.sqrt
仅返回一个数字,但通常存在正负平方根.C对应于正平方根,D对应于负).
(Note that although math.sqrt
returns only one number, in general there is a positive and negative square root. C corresponds to the positive square root, and D to the negative).
这篇关于在另一条线的一点上绘制固定长度的垂直线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!