[英]How to find the coordinates of a point such that it makes a 135 degree angle with a given line?
找到uv
向量為
uv.x = v.x - u.x
uv.y = v.y - u.y
計算其長度
luv = sqrt(uv.x * uv.x + uv.y * uv.y)
在uv
線與v
距離為L
延續處找到點:
px = v.x + L * uv.x / luv
py = v.y + L * uv.y / luv
將此點繞v
旋轉 45 度 ( Pi/4
):
qx = v.x + (p.x - v.x) * cos(Pi/4) - (p.y - v.y) * sin(Pi/4)
qy = v.y + (p.x - v.x) * sin(Pi/4) + (p.y - v.y) * cos(Pi/4)
請注意,我們可以使用坐標差來簡化最后一個表達式
rx = L * uv.x / luv
ry = L * uv.y / luv
將此點繞v
旋轉 45 度 ( Pi/4
):
qx = v.x + rx * cos(Pi/4) - ry * sin(Pi/4)
qy = v.y + rx * sin(Pi/4) + ry * cos(Pi/4)
此外, cos(Pi/4)
和sin(Pi/4)
都等於sqrt(2)/2
,因此如果您不需要不同的角度,則可以使用此常量。
實際上,您不必自己計算所有數學運算。 有幾個用於幾何計算的庫提供了只需幾行代碼即可完成任務的功能。
例如, Shapely具有可以在此處使用的rotate
和scale
功能:
from shapely.geometry import LineString
from shapely.affinity import rotate, scale
uv = LineString([(0, 0), (1, 0)])
vx_length = 2
pivot_point = uv.boundary[1]
scale_factor = vx_length / uv.length
uv_rotated = rotate(uv, -135, origin=pivot_point)
vx = scale(uv_rotated,
xfact=scale_factor,
yfact=scale_factor,
origin=pivot_point)
print(vx)
# LINESTRING (2.414213562373095 1.414213562373095, 1 0)
print(vx.length)
# 2.0
請注意,我們應該給rotate
負角度,因為默認情況下旋轉是逆時針執行的,但我們需要它是順時針的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.