Question

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.

Was it helpful?

Solution

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)

OTHER TIPS

To get what you put as example:

  1. Get the distance between top and bottom points. In your example, d = sqrt((x2 - x1)^2 + (y2-y1)^2).
  2. For a 90 degrees rotation by bottom point, simply use (x2,y2) and (x2+d, y2) as end points.
  3. For a 90 degrees rotation by top point, simply use (x1,y1) and (x1+d, y1) as end points.

This can be easily generalized to any line, not only a vertical one, and any rotation angle.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top