1

Say I have two rectangles, each with a 'connector' that points in a certain direction. The transform (location and angle) of the link is specified relative to the centre of its parent rectangle.

In the example below, rectangle A's link is (x: 0, y: -0.5, rotation: 0) while B's is (x: 0.5, y: 0, rotation: 45).

Two rectangles can 'plug in' to eachother by rotating such that their links have the same coordinates and face opposite directions.

Diagram

I'm trying to figure out how to calculate the transform of rectangle B relative to rectangle A after they are linked.

In this case, rectangle A is (0, 0, 0), A's link is (0, 0.5, 0), B's link is (0, 0.5, 180) and B is (~0.3, ~-0.8, 135).

Does anyone know how to calculate B's final position in the above example?

Omegastick
  • 1,773
  • 1
  • 20
  • 35
  • Please elaborate what the problem is. It seems to me that you just need to calculate the needed rotation as difference of connector rotations. Then determine the position by (rotation-adapted) adding the connector positions. Explain what you tried and explain how it failed. – Yunnosch Jan 28 '19 at 08:48
  • My best attempt so far has been to calculate the new position of B's link (A's link rotated 180 degrees). Then calculate B's rotation by adding the new B's link rotation with A's link rotation (I'm pretty sure this is where I'm going wrong). Then calculate B's position by rotating it by the rotation just calculated and translating it by the inverse of its link's position. This works for some setups but not for the one in the question. I'll try and convert what you said into code now. – Omegastick Jan 28 '19 at 09:02
  • Please explain all of that in the question itself. And show the code for that. Remember this is a programming question site. You are expected to show your current/best code, which brings you closest to the goal. Then people will help. But they love seeing a [mcve] first. – Yunnosch Jan 28 '19 at 09:24
  • @Omegastick see [Problem superimposing and aligning 3D triangles](https://stackoverflow.com/a/52163563/2521214) if you port it to 2D it would be even simpler – Spektre Jan 28 '19 at 09:35
  • I'm voting to close this question as off-topic because it is about geometry / mathematics instead of directly about programming / coding / programming tools / software algorithms. – Pang Jan 29 '19 at 00:37

1 Answers1

1

So you have base points A0 and B0 and link points AL and BL

At first you move B0 by difference of AL and BL, so

B0' = B0 + AL - BL

Then you have to rotate this point around AL to provide final position

B0''.X = AL.X + (B0.X - BL.X) * Cos(D) - (B0.Y - BL.Y) * Sin(D)
B0''.Y = AL.Y + (B0.X - BL.X) * Sin(D) + (B0.Y - BL.Y) * Cos(D)

where D is angle of rotation

D = Pi - A_rotation - B_rotation
MBo
  • 77,366
  • 5
  • 53
  • 86