vector - 用两点旋转线

标签 vector rotation geometry plane

我有两个点的线

(x1, y1) (x2, y2)

我只在 x+、y+ 平面上工作

并说这条线是垂直的

(320, 320) (320, 160)

如何将其旋转 90 度以获得

(320, 320) (480, 320)  [90 deg rotated by bottom point (320, 320)]
(320, 160) (480, 160)  [90 deg rotated by top point (320, 160)]

记住我需要相同的形式,即

(x1, y1) (x2, y2)

顺便说一句,这些线只能是垂直或水平的,因此斜率要么未定义,要么为零。

最佳答案

要围绕 A 旋转 B 90 度:

diff = B-A
B_new = A + array([-diff[1],diff[0]])

更一般地说,您可以这样做:

def rot_origin(p, ang):
    return array([p[0]*cos(ang)-p[1]*sin(ang),p[0]*sin(ang)+p[1]*cos(ang)])

def rot_around(p, p0, ang):
    return p0 + rot_origin(p-p0, ang)

那么,您的情况将是 B_new = rot_around(A, B, pi/2),因为 90 度是 pi/2弧度。

编辑:只是为了让您的示例完全明确。要围绕点 1 旋转 90 度,您将得到:

(x1,y1) (x1-(y2-y1),y1+(x2-x1))

要围绕点 2 旋转,您将得到:

(x2-(y1-y2),y2+(x1-x2)) (x2,y2)

关于vector - 用两点旋转线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15086488/

相关文章:

java - 用 vector 计算3点之间的角度,需要更精确(java)

c++ - 在 std::vector 中搜索一个值的近似值

Java:旋转和 3D 扭曲

algorithm - 射线-三角形相交

python - 在球形体积内采样均匀分布的随机点

r - 向量的最小和最大连续值

c++ - 初始化具有大小的结构 vector 时出错

度数和距离定位系统的算法

java - 游戏对象旋转形成椭圆形而不是圆形

c# - 如何查找鼠标单击是否在 wf 面板中呈现的图像上?