[英]Find coordinates of points given their distances
I have to find in Python the coordinates of the points A, B, C, D given their distances and the gradient of the line L (the one that passes through the center), which is parallel to segments AD and BC and orthogonal to segments AB and CD.我必须在 Python 中找到点 A、B、C、D 的坐标,给定它们的距离和直线 L(穿过中心的直线)的梯度,它平行于线段 AD 和 BC 并与线段正交AB 和 CD。
That is the code I wrote:那是我写的代码:
import numpy as np
# Gradient of the known line
l_gradient = 0.17
l_angle = np.arctan(l_gradient)
# Length of the segments
ad_distance = 1
ab_distance = 2
# Gradient and Intercept of lines AB and DC with the y axes
ab_gradient = dc_gradient = -1 / l_gradient # orthogonal to L
dc_intercept = (ad_distance / 2) / np.sin(l_angle) # Inverse formula of the right triangle
ab_intercept = - dc_intercept
# Gradient and Intercept of lines AD and BC with the y axes
ad_gradient = bc_gradient = l_gradient # parallel to L
ad_intercept = (ab_distance / 2) / np.cos(l_angle) # Inverse formula of the right triangle
bc_intercept = - ad_intercept
I think the easiest way to do this is first assume the gradient is 0. Then we have our points:我认为最简单的方法是首先假设梯度为 0。然后我们有我们的观点:
ad_distance = 1
ab_distance = 2
points = np.array([
[-ad_distance / 2, +ab_distance / 2], # A
[-ad_distance / 2, -ab_distance / 2], # B
[+ad_distance / 2, -ab_distance / 2], # C
[+ad_distance / 2, +ab_distance / 2], # D
])
Note that at the bottom we have a triangle with sides (x, l_gradient x, sqrt(1 + l_gradient^2) x)
.请注意,在底部我们有一个边为
(x, l_gradient x, sqrt(1 + l_gradient^2) x)
的三角形。 And remember cos(angle) = adjacent / hypot
.并记住
cos(angle) = adjacent / hypot
。
Thus we have:因此我们有:
l_gradient = 0.17
l_cos = 1 / np.sqrt(1 + l_gradient**2)
l_sin = l_gradient * l_cos
Now we can use those to construct a rotation matrix, and rotate our points into the correct positions:现在我们可以使用它们来构建一个旋转矩阵,并将我们的点旋转到正确的位置:
l_rot = np.array([[l_cos , -l_sin], [l_sin, l_cos]])
points = (l_rot @ points.T).T
No trigonometry functions required!不需要三角函数!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.