我有两个点的线
(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/