Search code examples
mathmathematical-optimization

Finding the new relative position of a point against the line after moved together


I am working on a drawing application. I summerize the main question to a tiny scenario:

User draws the line A1-B1 and also the point M1. Now user moves the line A1-B1 to a new position A2-B2 while the point M1 also should move with the line (like a rigid body). How can I calculate the new position of point M2?

enter image description here

I know it is possbile with complex calucations on lines or circles conjuction and finally detemrining if the new point is on the right or left of line (this question) etc. But as I want to recalculate live on any steps of dragging, I guess there should be a shortcut and light solution which all modeling softwares use. Is there really a shortcut formula rahter than the line or circle conjuctions?

To summerize the problem again I am looking for a light function with given A1,B1,A2,B2,M1 and looking for M2 position (two separate formula for x and y)

M2 = f(A1,B1,A2,B2,M1)

My guess: I think finding the center and the angle of rotation is one of the best options but I don't know again if there is a shotcut function to find them.

Edit: I prefer to implement it in an online website, so if there is a ready javascript library, it would be helpful, However I am now just looking for the mathematical logic.


Solution

  • Start with A1 and B1.

    D = B1 - A1
    C1 = D/|D|

    Rotate C1 90 degrees counter-clockwise to get C'1:
    C1 = (cx, cy)
    C'1 = (-cy, cx)

    Now take M1 - A1 and measure it against C1 and C'1:

    j = (M1 - A) · C1
    k = (M1 - A) · C'1

    Now it's easy to prove:

    M1 = A1 + j C1 + k C'1

    So j and k tell you where M1 is, given A1 and B1.

    When you get A2 and B2, use them to construct C2, rotate it to get C'2, and then you'll have:

    M2 = A2 + j C2 + k C'2