Re: Affine Transform
Re: Affine Transform
- Subject: Re: Affine Transform
- From: Erik Buck <email@hidden>
- Date: Tue, 14 Feb 2006 18:13:01 -0500
There is a potential problem in the code posted.
Code of the form NSSize imageSize = [image size]; should check
that "image" is not nil. The struct return value is undefined if the
message is sent to nil.
I suggest the following code instead of the code previously posted:
- (void)drawRect:(NSRect)rect
{
// Erase background
[[NSColor blackColor] set];
NSRectFill([self bounds]);
// Rotate coordinate system abount center of bounds
NSAffineTransform *newTransform = [NSAffineTransform transform];
[newTransform translateXBy:[self bounds].size.width/2.0 yBy:[self
bounds].size.height/2.0];
[newTransform rotateByDegrees:-angleDegrees];
[newTransform translateXBy:-[self bounds].size.width/2.0 yBy:-
[self bounds].size.height/2.0];
[newTransform concat];
NSImage *currentImage = [self image];
if(nil != currentImage)
{
// Draw current image centered in bounds
NSRect imageRect = NSZeroRect;
imageRect.size = [currentImage size];
[currentImage drawAtPoint:NSMakePoint([self bounds].size.width/
2.0-imageRect.size.width/2.0,
[self
bounds].size.height/2.0-imageRect.size.height/2.0)
fromRect:imageRect
operation:NSCompositeSourceOver fraction:1.0f];
}
// Draw center point just for reference
[[NSColor redColor] set];
NSRectFill(NSMakeRect([self bounds].size.width/2.0 - 5, [self
bounds].size.height/2.0 - 5, 10, 10));
}
- (void)adjustFrame
{
NSImage *currentImage = [self image];
if(nil != currentImage)
{
// Resize the frame to be large enough to show all of the
current image even if it is rotated.
NSSize imageSize = [currentImage size];
NSAffineTransform *newTransform = [NSAffineTransform transform];
[newTransform translateXBy:imageSize.width/2.0
yBy:imageSize.height/2.0];
if(shouldUseMinRect)
{
[newTransform rotateByDegrees:-angleDegrees];
}
else
{ // This will result in the largest frame needed for any angle
[newTransform rotateByDegrees:45.0f];
}
[newTransform translateXBy:-imageSize.width/2.0 yBy:-
imageSize.height/2.0];
// Transform all four corners of the rectangle that encloses
the current image
NSPoint point0 = [newTransform transformPoint:NSZeroPoint];
NSPoint point1 = [newTransform transformPoint:NSMakePoint
(0.0f, imageSize.height)];
NSPoint point2 = [newTransform transformPoint:NSMakePoint
(imageSize.width, imageSize.height)];
NSPoint point3 = [newTransform transformPoint:NSMakePoint
(imageSize.width, 0.0f)];
// Find the minimum and maximum coordinates of the transformed
rectangle that encloses the current image
float minX = MIN(MIN(MIN(point0.x, point1.x), point2.x),
point3.x);
float minY = MIN(MIN(MIN(point0.y, point1.y), point2.y),
point3.y);
float maxX = MAX(MAX(MAX(point0.x, point1.x), point2.x),
point3.x);
float maxY = MAX(MAX(MAX(point0.y, point1.y), point2.y),
point3.y);
// Set the frame size large enough to enclose the minimum and
maximum coordinates of the transformed rectangle
[self setFrameSize:NSMakeSize(maxX - minX, maxY - minY)];
}
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden