Reputation: 798
I have line with two points
(x1, y1) (x2, y2)
and I am working on x+, y+ plane only
and say this line is vertical say
(320, 320) (320, 160)
How do I rotate it by 90 degrees to get
(320, 320) (480, 320) [90 deg rotated by bottom point (320, 320)]
(320, 160) (480, 160) [90 deg rotated by top point (320, 160)]
Remember I would need in same form—i.e,
(x1, y1) (x2, y2)
By the way, these lines can only be vertical or horizontal, so the slope is either undefined or zero.
Upvotes: 2
Views: 5341
Reputation: 5067
To rotate B
90 degrees around A
:
diff = B-A
B_new = A + array([-diff[1],diff[0]])
To be more general, you do this:
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)
Then, your case would be B_new = rot_around(A, B, pi/2)
, since 90
degrees is pi/2
radians.
Edit: Just to make it completely explicit for your example. To rotate by 90 degrees around point 1, you would get:
(x1,y1) (x1-(y2-y1),y1+(x2-x1))
To rotate around point 2, you would get:
(x2-(y1-y2),y2+(x1-x2)) (x2,y2)
Upvotes: 3
Reputation: 2317
To get what you put as example:
This can be easily generalized to any line, not only a vertical one, and any rotation angle.
Upvotes: -1