Re: NSAffineTransform scaleBy not scaling
Re: NSAffineTransform scaleBy not scaling
- Subject: Re: NSAffineTransform scaleBy not scaling
- From: Graham Cox <email@hidden>
- Date: Tue, 1 Dec 2009 13:11:14 +1100
On 01/12/2009, at 12:55 PM, Shane wrote:
> I'm drawing a graphing line, looks like a wave within an NSView. The
> line looks fine, except that it's not scaled to the size of the
> window. So my problem is trying to figure out how to scale it to the
> window size and every time that the window is resized.
>
> I currently have something like below, but my 'scaleXBy, yBy' on the
> NSAffineTransform isn't working out so well.
>
> Anyone see what I'm doing wrong here, or know how to get the
> NSBezierPath to take up 90% of my NSView (leaving 10% blank for
> borders which is what I tried to do below)?
>
> - (void) drawRect:(NSRect) rect
> {
> NSRect bounds = [self bounds];
>
> float xAxis = bounds.size.width * 0.9;
> float yAxis = bounds.size.height * 0.9;
>
> [[NSColor blackColor] setFill];
> [NSBezierPath fillRect:bounds];
>
> NSAffineTransform *newTransform = [NSAffineTransform transform];
> [newTransform translateXBy:40.0 yBy:30.0];
> [newTransform concat];
>
> // can't seem to get this transform to work as expected.
> [newTransform scaleXBy:xAxis yBy:yAxis];
>
> [self drawAxes:rect width:xAxis height:yAxis];
> [self drawGraph:rect];
>
> return;
> }
You concat your transform, then change its scale. The scale change will have no effect because you already did the concat. (n.b. a concat just multiplies the current graphics context transform by yours, so changes you make to yours later don't have an effect).
However, I think your maths is wrong also.
You need to take into account the size of the path you're trying to draw. If your path has a bounds of, say, 100 x 100, and you wish to fit it into your view's bounds, the scale factor on each axis will be view.bounds / path.bounds. So if your view is 200 x 150, you end up with scale factors of 2 and 1.5 respectively. You also need to carefully consider where the origin is - if translate before you scale, the scale will also scale the offset, maybe not what you want. But the order of operations you apply to the transform is the reverse of what you might think will happen. One way to simplify the maths slightly is to generate the graph path using a fixed reference bounds of 1 x 1, located at the origin. Then you can directly use your view's size and position to scale and translate.
--Graham
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please 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:
This email sent to email@hidden