Coordinate Rotation at a Point

  • Although I found references to rotating a Bezier path, searching for
    rotating coordinates at a point wasn't fruitful.  In my case, I wanted to
    rotate a shape in place about its center, including shading and/or content.
    I believe the following is about as simple as you can get and can be used to
    rotate about any point.

    @implementation  NSAffineTransform (RTPTransformAdditions)

    - (void)rotate:(float)angle atPoint:(NSPoint)point
    {
        [self rotateByDegrees:angle];
      NSAffineTransform* pointXfrm = [NSAffineTransform transform];
        [pointXfrm rotateByDegrees:-angle];    //    Needed to get original
    point in rotated coordinates.
        NSPoint oldPoint = [pointXfrm transformPoint:point];
        float x = oldPoint.x - point.x;
        float y = oldPoint.y - point.y;
        [self translateXBy:x yBy:y];    //    Done in the rotated coordinates.
    }
  • On 06/11/2007, at 4:33 PM, Gordon Apple wrote:

    > Although I found references to rotating a Bezier path,
    > searching for
    > rotating coordinates at a point wasn't fruitful.  In my case, I
    > wanted to
    > rotate a shape in place about its center, including shading and/or
    > content.
    > I believe the following is about as simple as you can get and can
    > be used to
    > rotate about any point.
    >
    > @implementation  NSAffineTransform (RTPTransformAdditions)
    >
    > - (void)rotate:(float)angle atPoint:(NSPoint)point
    > {
    > [self rotateByDegrees:angle];
    > NSAffineTransform* pointXfrm = [NSAffineTransform transform];
    > [pointXfrm rotateByDegrees:-angle];    //    Needed to get
    > original
    > point in rotated coordinates.
    > NSPoint oldPoint = [pointXfrm transformPoint:point];
    > float x = oldPoint.x - point.x;
    > float y = oldPoint.y - point.y;
    > [self translateXBy:x yBy:y];    //    Done in the rotated
    > coordinates.
    > }

    Hey, I think I know how to do this the easy way, thanks to Eric Buck:

    > To rotate about some point, translate to that point, rotate, and then
    > translate back.  The approach is not Cocoa specific and is covered in
    > any introductory graphics text book.

    so,     [theTransform translateToPoint:(thePointYouWantToRotateAround)];
    [theTransform rotateByWhatever];
    [theTransform translateToPoint:(-thePointYouWantToRotateAround)];
    [theTransform concat];

    I forget the NSTransformer method names, but I think the intent is
    clear.

    Ron
previous month november 2007 next month
MTWTFSS
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    
Go to today