Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: rotating an object around the center of a view



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.

Here is some Cocoa code as a sample

/* MyRotateView */

#import <Cocoa/Cocoa.h>

@interface MyRotateView : NSView
{
   float       rotationAngleDeg;
}

- (IBAction)takeRotationAngleDegFrom:(id)sender;

@end


#import "MyRotateView.h"

@implementation MyRotateView


- (void)drawRect:(NSRect)rect
{
NSPoint centerOfView = NSMakePoint(NSMidX([self bounds]), NSMidY([self bounds]));
float inscribedCircleRadius = 0.5f * MIN([self bounds].size.width,
[self bounds].size.height);
NSRect pivotPointIndicatorRect = NSMakeRect (centerOfView.x - 10.0f,
centerOfView.y - 5.0f, 20.0f, 10.0f);
NSAffineTransform *translateTransform = [NSAffineTransform transform];


   {  // draw something not rotated
      // erase past drawing
      [[NSColor whiteColor] set];
      NSRectFill([self bounds]);

      NSBezierPath      *path = [NSBezierPath bezierPath];

[[NSColor grayColor] set];
[path moveToPoint:NSMakePoint(centerOfView.x - inscribedCircleRadius, centerOfView.y)];
[path lineToPoint:NSMakePoint(centerOfView.x + inscribedCircleRadius, centerOfView.y)];
[path moveToPoint:NSMakePoint(centerOfView.x, centerOfView.y - inscribedCircleRadius)];
[path lineToPoint:NSMakePoint(centerOfView.x, centerOfView.y + inscribedCircleRadius)];
[path stroke];
}



[NSGraphicsContext saveGraphicsState]; // Save the state

[translateTransform translateXBy:centerOfView.x yBy:centerOfView.y]; // Trans to center of rotation
[translateTransform rotateByDegrees:rotationAngleDeg]; // Rotate
[translateTransform translateXBy:-centerOfView.x yBy:- centerOfView.y];// Translate back
[translateTransform concat];


{ // draw something rotated
NSBezierPath *path = [NSBezierPath bezierPathWithOvalInRect:pivotPointIndicatorRect];


[[NSColor greenColor] set];
[path moveToPoint:NSMakePoint(centerOfView.x - 20.0f, centerOfView.y)];
[path lineToPoint:NSMakePoint(centerOfView.x + inscribedCircleRadius, centerOfView.y)];
[path stroke];
}


   [NSGraphicsContext restoreGraphicsState];       // Restore the state

{ // draw something not rotated
NSBezierPath *path = [NSBezierPath bezierPathWithOvalInRect:NSMakeRect(centerOfView.x -
inscribedCircleRadius, centerOfView.y - inscribedCircleRadius,
2.0f * inscribedCircleRadius, 2.0f * inscribedCircleRadius)];


      [[NSColor blackColor] set];
      [path stroke];
   }
}

- (IBAction)takeRotationAngleDegFrom:(id)sender
{
   rotationAngleDeg = [sender floatValue];
   [self setNeedsDisplay:YES];
}

@end

There is a sample called TransparentTetris that does this too at http://www.cocoaprogramming.net/Downloads.html

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden


Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.