Notes on rotating and zooming an image around its center
Notes on rotating and zooming an image around its center
- Subject: Notes on rotating and zooming an image around its center
- From: Jerry LeVan <email@hidden>
- Date: Thu, 8 Jul 2004 14:15:29 -0400
In the next release of ImageBrowser I am going to have the
capability of zooming and rotating images around its center.
The basic idea is fairly simple, (after a days work).
1) Zoom the image via the method "setSize:" method
2) Conceptually embed the (zoomed)image in a square with side
equal to the length of a diagonal of the (zoomed) image,
anchored in the lower left corner. Call the side length
bigSide. (This size ensures that all rotations will not
be clipped.)
3) Rotate the image by the appropriate amount.
4) Calculate the new coordinates of the center of the rotated
image. Say x1 and y1.
5) Translate the rotated image so that the center of the image
coincides with the center. x2= (bigSide/2 -x1); y2 = (bigSide/2 -y1)
6) Draw the Image.
Hope this saves someone some time one day :)
Jerry
Here is some working code...
-(void) spin:(NSImage*)anImage
{
NSAffineTransform *imageRotation,*translate ;// Rotation about center
of bounds
NSPoint center; // Center of receiver's bounds
NSSize imageSize; // Size of the image to draw
NSImage *targetImage;
float rotateFactor;
NSSize scaledRect,bigSize;
NSImage *tmpImage=NULL;
#define M_PI 3.14159265358979323846 /* pi */
// Get the desired rotation angle
rotateFactor = [rotation floatValue];
// Get the desired scale factor
float scaleFactor=[theZoomer floatValue];
// Get a copy of the image we wish to process
tmpImage = [anImage copy];
[tmpImage setScalesWhenResized:YES];
//do the scaling arithmetic
NSSize tmpRect = [tmpImage size];
scaledRect.width= tmpRect.width*scaleFactor;
scaledRect.height=tmpRect.height*scaleFactor;
// Do the scaling
[tmpImage setSize:scaledRect];
imageSize = [tmpImage size];
// until I brush up on my trig we will set the
// target image large enough to hold the zoomed
// image for any rotation.
float bigSide = sqrt(imageSize.width*imageSize.width +
imageSize.height*imageSize.height);
bigSize = NSMakeSize(bigSide,bigSide);
targetImage = [[NSImage alloc] initWithSize:bigSize];
// Create transformations for the rotation and translation..
imageRotation = [NSAffineTransform transform];
translate = [NSAffineTransform transform];
// For all angles we will translate the center of the image
// in its rotated position back to (bigSide/2,bigSide/2).
center =NSMakePoint(
(bigSide-
((scaledRect.width)*cos(rotateFactor*M_PI/180.0)
-scaledRect.height*sin(rotateFactor*M_PI/180.0)))/2.0,
(bigSide-
((scaledRect.width)*sin(rotateFactor*M_PI/180.0)
+scaledRect.height*cos(rotateFactor*M_PI/180.0)))/2.0 );
[targetImage lockFocus];
[imageRotation rotateByDegrees:rotateFactor];
[translate translateXBy:center.x yBy:center.y];
[imageRotation appendTransform: translate];
[imageRotation concat] ; // map the coordinate system
// Draw the image in the current coordinate system
[ tmpImage drawAtPoint:NSMakePoint(0,0)
fromRect:NSMakeRect(0,0, imageSize.width, imageSize.height)
operation:NSCompositeCopy fraction:1.0];
// Restore the previous graphics state
[targetImage unlockFocus];
// Set the image into the view
[bigImage setImage:targetImage];
[self setNewFrame]; // sets the frame for bigImage to "larger"
// of contentRect of scroll view or image size
[targetImage release];
[tmpImage release];
}
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.