7

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!

DataCrypt
  • 307
  • 1
  • 5
  • 15

2 Answers2

1

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;
Atomosk
  • 1,871
  • 2
  • 22
  • 26
  • Thank you. I will give this a try. I suppose I should clarify that I'm moving the circle from position 1 to position 2 and then performing the rotation there. So, I need to center that orange point (X2, Y2) at the center of the red square (known point X3, Y3). The angle is calculated based on the blue circles and sometimes could be negative (between -1 & -180). Not sure if this effects the formulas? – DataCrypt Sep 24 '14 at 16:21
  • @DataCrypt If rotation base point moves with orange point then for calculation purpose it doesn't matter what will be first: moving or rotating. If you move orange point to (X3, Y3) however answer will be wrong since you probably rotating circle around it's center, not around orange point (point, around which you do rotation, doesn't change it's coordinate after rotation). Of course it's only if (X3, Y3) doesn't rotates with circle. – Atomosk Sep 25 '14 at 02:36
  • @DataCrypt Any value are fine for `sin` and `cos` and those formulas even -9000. Only thing that confuses me is `90 - Angle`, since all sin and cos implementations I saw take radian value of angle, not degree. But I'm pretty sure you know it. – Atomosk Sep 25 '14 at 02:46
0

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.

se5a
  • 97
  • 10