我必須在 Python 中找到點 A、B、C、D 的坐標,給定它們的距離和直線 L(穿過中心的直線)的梯度,該直線平行于線段 AD 和 BC 并與線段正交AB 和 CD。

那是我寫的代碼:
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
uj5u.com熱心網友回復:
我認為最簡單的方法是首先假設梯度為 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
])
請注意,在底部我們有一個帶邊的三角形(x, l_gradient x, sqrt(1 l_gradient^2) x)。并記住cos(angle) = adjacent / hypot。
因此我們有:
l_gradient = 0.17
l_cos = 1 / np.sqrt(1 l_gradient**2)
l_sin = l_gradient * l_cos
現在我們可以使用它們來構建一個旋轉矩陣,并將我們的點旋轉到正確的位置:
l_rot = np.array([[l_cos , -l_sin], [l_sin, l_cos]])
points = (l_rot @ points.T).T
不需要三角函式!
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/355852.html
