[英]How to find the coordinates of a point such that it makes a 135 degree angle with a given line?
Find uv
vector as找到uv
向量为
uv.x = v.x - u.x
uv.y = v.y - u.y
Calculate its length计算其长度
luv = sqrt(uv.x * uv.x + uv.y * uv.y)
Find point at continuation of uv
line with distance L
from v
:在uv
线与v
距离为L
延续处找到点:
px = v.x + L * uv.x / luv
py = v.y + L * uv.y / luv
Rotate this point around v
by 45 degrees ( Pi/4
):将此点绕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)
Note that we can simplify tha last expressions a bit using coordinate difference请注意,我们可以使用坐标差来简化最后一个表达式
rx = L * uv.x / luv
ry = L * uv.y / luv
Rotate this point around v
by 45 degrees ( Pi/4
):将此点绕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)
Also both cos(Pi/4)
and sin(Pi/4)
are equal to sqrt(2)/2
, so you can use this constant if you don't need different angles.此外, cos(Pi/4)
和sin(Pi/4)
都等于sqrt(2)/2
,因此如果您不需要不同的角度,则可以使用此常量。
Actually, you don't have to calculate all the math by yourself.实际上,您不必自己计算所有数学运算。 There are several libraries for geometric computation that offer functionality to perform the task in just a couple of lines of code.有几个用于几何计算的库提供了只需几行代码即可完成任务的功能。
For example, Shapely has rotate
and scale
functions that could be used here:例如, 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
Note that we should give the negative angle to rotate
since by default rotation is performed counterclokwise but we need it to be clockwise.请注意,我们应该给rotate
负角度,因为默认情况下旋转是逆时针执行的,但我们需要它是顺时针的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.