Re: Transform not quite right
Re: Transform not quite right
- Subject: Re: Transform not quite right
- From: "R. Matthew Emerson" <email@hidden>
- Date: Sat, 30 Jun 2007 11:39:22 -0400
On Jun 30, 2007, at 3:49 AM, David Arnold wrote:
I am very new to cocoa and I am trying to map my user space to an
NSView object. I am almost success, but for some reason, I am not
getting the translation upward that I need so the the entire sine
curve will fit in the window. I feel I am missing something
fundamental. Can anyone help?
- (void)drawRect:(NSRect)rect {
float xmin = 0.0;
float xmax = 2*pi;
float ymin = -2.0;
float ymax = 2.0;
float numPoints = 200;
float x = xmin;
float dx = (xmax-xmin)/numPoints;
float y;
[[NSColor grayColor] set];
NSRectFill(rect);
NSAffineTransform *xForm = [NSAffineTransform transform];
[xForm translateXBy:-xmin yBy:-ymin];
[xForm scaleXBy:(rect.size.width/(xmax-xmin)) yBy:
(rect.size.height/(ymax-ymin))];
// [xForm translateXBy:rect.origin.x yBy:rect.origin.y];
Looks like your translations are done in the wrong order. Also, note
the passed in rect includes only the invalidated portions of your
view, so you'll want to refer to the view's bounds rect directly.
NSRect r = NSInsetRect([self bounds], 5, 5); /* add some margin
around the plotted area */
/* snippet from working code */
float xs = r.size.width/([self maxX] - [self minX]);
float ys = r.size.height/([self maxY] - [self minY]);
[transform translateXBy:r.origin.x yBy:r.origin.y];
[transform scaleXBy:xs yBy:ys];
[transform translateXBy:-[self minX] yBy:-[self minY]];
_______________________________________________
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