How to translate a geometric figure in relation to a coordinate in a mirror way

59 Views Asked by At

So to start, I'm working with svg images and I need to move a bunch of programmatically created shapes in an inverted cartesian plane.

so my problem is math related only at this time, I need to rotate each figure created in 180° degrees but the problem is that they rotate in their own center and I can't merge them to rotate in the same axis so painfully I have to move them independent.

So my idea is to mirror the position of the figure I want to rotate to the other side of the cartesian figure and then rotate it. Sounds kind of difficult to understand it like that so let me explain.

so I have this figure that we are gonna call it figure A with coordinates (40, 15), (39, 38), (23,38) and the last coordinate is (24, 15), this is surrounded by a bounding box with coordinate A1 = (23, 15) and A2 = (40, 38); then we have the figure that we are gonna call it figure B with coordinates (25, 27), (30, 17), (37, 20), (32, 30) and this one has a bounding box of B1 = (25, 17) and B2 (37, 30). (I'm gonna put an image as reference, figure A is blue color and his bounding box is yellow, figure B is purple and his bounding box is red).

enter image description here

So my goal is to move figure B in a mirror coordinate inside figure A, the catch is that the one who moves the figure in svg shapes are the bounding box coordinates A1 in the case shape A and B1 in the case of shape B.

What I tried so far is to move by distance, taking the distance between A1 and B1 and the distance of B2 and A2 and then rest both of the results to get the distance "average" and then sum that number in B1 coordinates to move it but it goes waay out of the supposed destination. I also tried to start moving the A1 relative to B2 but no avail so I'm kind of out of options right now. Any idea of how to mirror B1 coordinates to his relative position? for this case I did a guessing that the relative position of this figure in particular should be B1 = (26,23) approx. But it can vary, is just an approximation so I need a formula that can be applied to different shapes.

1

There are 1 best solutions below

1
On BEST ANSWER

To obtain a rotation of all figures about the same point $O$, just translate each figure, after the rotation about its own center $F$, by vector $2\vec{FO}$.

In the case of your figure, for instance, if we take $O=(31.5,26.5)$ (center of fig. A) and $F=(31,23.5)$ (center of fig. B), then $2\vec{FO}=(1,6)$. After fig. B has been rotated about $F$ all its points should then be translated by that vector.

enter image description here

If you want, instead, to compute the new coordinates of point $P=(x,y)$ after a rotation of 180° about $O=(x_0,y_0)$, the formula you need is even simpler: $$ P'=(2x_0-x,2y_0-y). $$