Re: Affine Transform
Re: Affine Transform
- Subject: Re: Affine Transform
- From: Terry Heaford <email@hidden>
- Date: Sun, 12 Feb 2006 15:32:05 +0000
I have been messing around all day with this as I do not follow
affine transforms very well and I am having difficulty getting my
head around bounds vs frame vs origin.
However I think some progress has been made with the following code
which rotates the image and draws correctly for angles up to and
including 90.
It does not yet work for angles greater than 90.
Can anyone help with this please and/or provide observations
concerning the code below.
Please be kind, I am new to this.
if (image)
{
degrees = 75;
//calculate new image size & rectangle from original image dims
plus degrees of rotation
imageWidth = [image size].width;
imageHeight = [image size].height;
imageRect = NSMakeRect(0,0,imageWidth,imageHeight);
//calculate via trig the new image size.
w1 = cos(degrees * pi/180) * imageWidth;
w2 = cos((90-degrees) * pi/180) * imageHeight;
h1 = sin(degrees * pi/180) * imageWidth;
h2 = sin((90-degrees) * pi/180) * imageHeight;
newImageWidth = w1 + w2;
newImageHeight = h1 + h2;
newImageRect = NSMakeRect(0,0,newImageWidth,newImageHeight);
//rotation point of image
centrePoint = NSMakePoint(NSMidX(imageRect), NSMidY(imageRect));
// offset to centre point of new image size.
imageOffset = NSMakePoint((newImageWidth - imageWidth)/2,
(newImageHeight - imageHeight)/2);
NSRect dstRect = NSMakeRect(-imageOffset.x,-
imageOffset.y,newImageWidth,newImageHeight);
[self setFrame:dstRect];
[self setBounds:dstRect];
[transform translateXBy:centrePoint.x yBy:centrePoint.y];
[transform rotateByDegrees:degrees];
[transform translateXBy:-centrePoint.x yBy:-centrePoint.y];
[transform concat];
[image drawInRect:imageRect fromRect:imageRect
operation:NSCompositeCopy fraction:opacity];
}
On 12 Feb 2006, at 02:41, Ian was here wrote:
Here is some sample code that demonstrates image
rotation, amongst other things...
http://www.howleware.com/apps/simple_sprite_engine.hqx
--- Terry Heaford <email@hidden>
wrote:
A newbie question:
I am really struggling with affine transforms to
rotate an image
contained in an NSView which is a subview of a
scrollview.
The image is larger than than the visible size of
the scrollview.
The code I have from drawRect is below:
if (image)
{
deg = 45;
NSRect imageRect = NSMakeRect(0,0,[image
size].width,[image
size].height);
NSPoint imageDrawPoint = NSMakePoint([self
visibleRect].size.width/
2, [self visibleRect].size.height/2);
NSAffineTransform *trans = [NSAffineTransform
transform];
[trans translateXBy:imageDrawPoint.x
yBy:imageDrawPoint.y];
[trans rotateByDegrees:deg];
[trans translateXBy:-imageDrawPoint.x
yBy:-imageDrawPoint.y];
[trans concat];
[image drawAtPoint:NSZeroPoint fromRect:imageRect
operation:NSCompositeCopy fraction:opacity];
}
This seems to rotate the image correctly but how do
I size the frame
or the bounds (not sure which or both) to the
correct size for the
rotated image allowing it to be scrolled within the
scrollview and
how do I locate the image within the frame
correctly.
Thanks in advance
Terry
_______________________________________________
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
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
_______________________________________________
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