简体   繁体   中英

How to find the coordinates of a point such that it makes a 135 degree angle with a given line?

Given a line segment uv I want to find the coordinates of point x such that they make up an angle of 135 degrees with a given distance of L , below is an illustration:

样本

I would use a triangle to determine where x is, you can use a 45 45 90 triangle because the angle is 135 degrees. If it was not 135 degrees you can use sin and cos to find x

在此处输入图片说明

Find uv vector as

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 :

px = v.x + L * uv.x  / luv
py = v.y + L * uv.y  / luv

Rotate this point around v by 45 degrees ( 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 ):

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.

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM