7
votes

I am working on an application for the past few weeks which involves some trigonometry and am currently stuck. As shown in the diagram below, I have a circular item (green circle at position #1) which I know the center point (let's call that X1,Y1). The circle has another point (orange circle) that is off-centered a bit - midway between two other marks (blue circles). These marks can move around. The coordinates of the orange point are calculated (let's call it X2, Y2) and the angle of the blue line is calculated (call it Angle) in relation to the horizontal of the circle.

Diagram

I can calculate the difference between the center of the circle and the point by:

deltaX = X2-X1

deltaY = Y2-Y1

I need to move and rotate the green circle (either CW or CCW - whichever is shorter) from it's start location (position 1) over to position 2. This means the angle could be negative or positive. The blue line must end up vertical and the orange dot at the center of position 2 (red square). I know the coordinates for the center of position 2 (let's call this point X3,Y3). Position #1 and position #2 are exactly 90 degrees from each other.

I thought I could use some trig identity formulas that calculate the rotation of a point, as such:

offsetX = deltaX * cos(90-Angle) - deltaY * sin(90-Angle)

offsetY = deltaX * sin(90-Angle) + deltaY * cos(90-Angle)

I was hoping these offsets would be what I need to adjust the circle to it's new center when it moves/rotates over to position 2.

X3 = X3 + offsetX

Y3 = Y3 + offsetY

However, when I try use this math, it's not placing the orange mark of the circle in the center of the square. Not sure if my equations and calculations are correct based on the angle of rotation (positive or negative, CW or CCW) or if I'm using the angle correctly (where I subtract the known angle from 90 degrees). How do I correctly calculate the final point/position? Any help and examples would be greatly appreciated!

Thank you very much for your time!

2

2 Answers

1
votes

So you need to rotate your circle by 90 - Angle and then move orange point to (X3, Y3)?
First you need to find orange point coordinate after rotation:

newX = X2 * cos(90 - Angle) - Y2 * sin(90 - Angle);
newY = X2 * sin(90 - Angle) + Y2 * cos(90 - Angle);

newX and newY are orange point coordinates after rotation. To find move transformation simply substract:

moveX = X3 - newX;
moveY = Y3 - newY;

Now if you rotate circle by 90 - Angle and move it by (moveX, moveY) orange point will move to (X3, Y3). That is if you rotate circle around (0, 0) point. If you rotating around some (X, Y) point, you first need to substract X from X2, Y from Y2 and then add X to newX, Y to newY. That substraction 'moves' your rotation base point to (0, 0), so after rotation you need to move it back:

newX = (X2 - X) * cos(90 - Angle) - (Y2 - Y) * sin(90 - Angle) + X;
newY = (X2 - X) * sin(90 - Angle) + (Y2 - Y) * cos(90 - Angle) + Y;
0
votes

Be aware that your code is using a counter clockwise rotation, (conventionally angles are measured counter clockwise) which may be why you're not getting the results you expect. if you want a clockwise rotation try:

offsetX = deltaX * cos(angle) + deltaY * sin(angle)
offsetY = -deltaX * sin(angle) + deltaY * cos(angle)

Ensure your angles are in radians not degrees. Drawing some lines may help you debug things too.