pixelbath
pixelbath

Reputation: 55

Rotating points around another arbitrary point

Given the following test code inside a function:

int orientation = 0;        // increments up to 359, then loops to 0
int tempx, tempy;
float radians = orientation * (M_PI / 180);

tempx = point->x;
tempy = point->y;

tempx = (int) (tempx * cos(radians) - tempy * sin(radians));
tempy = (int) (tempx * sin(radians) + tempy * cos(radians));

tempx = tempx + origin->x;
tempy = tempy + origin->y;

With the following points (relative to origin): (-100, 0), (0, -100), (0, 100) I get this strange plot:

curved paths

The blue and green lines are overlapping paths. The intersection at the middle (with the barely-visible yellow point) is the origin. All points are in the correct position when orientation is 0 or 180 but in a non-circular path at all other angles. I've done plenty of linear algebra in my time, but this has me stumped. I'm not sure if I'm overlooking something in C itself, or if I'm just not seeing the problem.

Upvotes: 1

Views: 75

Answers (1)

uesp
uesp

Reputation: 6204

You are reusing tempx after rotating it. Try the following instead:

   tempx = (int) (point->x* cos(radians) - point->y* sin(radians));
   tempy = (int) (point->x* sin(radians) + point->y* cos(radians));

and see if that fixes things or not.

Upvotes: 3

Related Questions