user1879027
user1879027

Reputation:

Using CGAffineTransformMakeRotation to rotate a point

If I want to rotate one CGPoint about another, I'm currently doing this (which works fine):

    CGFloat rx = cos(DEGREES_TO_RADIANS(-angle)) * (positionToRotate.x-rotationPoint.x) - sin(DEGREES_TO_RADIANS(-angle)) * (positionToRotate.y-rotationPoint.y) + rotationPoint.x;
    CGFloat ry = sin(DEGREES_TO_RADIANS(-angle)) * (positionToRotate.x-rotationPoint.x) + cos(DEGREES_TO_RADIANS(-angle)) * (positionToRotate.y-rotationPoint.y) + rotationPoint.y;

It strikes me that I should be able to do this with a CGAffineTransform, but I'm a bit stuck as to how it would work:

CGAffineTransform affine CGAffineTransformMakeRotation(M_PI/4);
CGPointApplyAffineTransform(positionToRotate, affine);

That does nothing as I'm (hopefully) missing something obvious :) So how do you rotate a CGPoint about another without doing the matrix math myself? Cheers, Ian

Upvotes: 2

Views: 574

Answers (1)

rob mayoff
rob mayoff

Reputation: 386038

CGPointApplyAffineTransform returns the transformed point. It doesn't mutate the CGPoint you pass in.

CGPoint transformedPoint = CGPointApplyAffineTransform(positionToRotate, affine);

Upvotes: 1

Related Questions