Sou*_*rav 6 python geometry shapely
我有两个点 A (10,20) 和 B (15,30)。这些点生成一条线 AB。我需要在 B 点上用 Python 绘制一条长度为 6(每个方向 3 个单位)的垂直线 CD。
我已经使用以下代码获得了 AB 行的一些属性:
from scipy import stats
x = [10,15]
y = [20,30]
slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
Run Code Online (Sandbox Code Playgroud)
如何计算 C 和 D 的位置。我需要它们的 X 和 Y 值。

C 和 D 的值将用于使用 Shapely 库完成另一个目标。
Geo*_*rgy 10
Since you are interested in using Shapely, the easiest way to get the perpendicular line that I can think of, is to use parallel_offset method to get two parallel lines to AB, and connect their endpoints:
from shapely.geometry import LineString
a = (10, 20)
b = (15, 30)
cd_length = 6
ab = LineString([a, b])
left = ab.parallel_offset(cd_length / 2, 'left')
right = ab.parallel_offset(cd_length / 2, 'right')
c = left.boundary[1]
d = right.boundary[0] # note the different orientation for right offset
cd = LineString([c, d])
Run Code Online (Sandbox Code Playgroud)
And the coordinates of CD:
>>> c.x, c.y
(12.316718427000252, 31.341640786499873)
>>> d.x, d.y
(17.683281572999746, 28.658359213500127)
Run Code Online (Sandbox Code Playgroud)
如果slope是 AB 的斜率,则 CD 的斜率是-1/slope。这等于垂直变化除以水平变化:dy/dx = -1/slope。这给出了dx = -slope*dx. 根据毕达哥拉斯定理,你有3**2 = dy**2+dx**2。替换dx,你得到
3**2 = (-slope*dy)**2+dy**2
3**2 = (slope**2 + 1)*dy**2
dy**2 = 3**2/(slope**2+1)
dy = math.sqrt(3**2/(slope**2+1))
然后你就可以得到dx = -slope*dy. 最后,您可以使用dxanddy来获取 C 和 D。所以代码将是:
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
Run Code Online (Sandbox Code Playgroud)
(注意,虽然math.sqrt只返回一个数,但一般来说是有正负平方根的。C对应正平方根,D对应负平方根)。
| 归档时间: |
|
| 查看次数: |
6841 次 |
| 最近记录: |