Re: Transform not quite right
Re: Transform not quite right
- Subject: Re: Transform not quite right
- From: David Arnold <email@hidden>
- Date: Sat, 30 Jun 2007 11:15:32 -0700
Thanks for the response. I tried instead (complete code below):
NSRect bRect = [self bounds];
[[NSColor grayColor] set];
NSRectFill(bRect);
I am thinking in terms of two windows, my user space and my NSView.
My user space is a rectangle with lower left corner at (xmin,ymin)
and upper right corner at (xmax,ymax). My view is a rectangle with
lower left corner at (0,0) and upper right corner at (width,height).
Let (x,y) be a point in user space and let (u,v) be the coordinates
of the same point in my NSView. Then, I think these ratios must be
the same:
u/width = (x-xmin)/(xmax-xmin).
This leads to:
u = (x-xmin)(width)/(xmax-xmin).
So, I am thinking to first shift by -xmin, then scale by width/(xmax-
xmin).
Similarly for y, as:
v=(y-ymin)(height)/(ymax-ymin).
Thus, I am led to these transforms:
NSAffineTransform *xForm = [NSAffineTransform transform];
[xForm translateXBy:-xmin yBy:-ymin];
[xForm scaleXBy:(bRect.size.width/(xmax-xmin)) yBy:
(bRect.size.height/(ymax-ymin))];
I have these:
float xmin = 0.0;
float xmax = 2*pi;
float ymin = -2.0;
float ymax = 2.0;
And these:
2007-06-30 10:48:29.224 David[9657] bRect.origin.x = 0.000000
2007-06-30 10:48:29.225 David[9657] bRect.origin.y = 0.000000
2007-06-30 10:48:29.225 David[9657] bRect.size.width = 760.000000
2007-06-30 10:48:29.225 David[9657] bRect.size.height = 560.000000
So, I can actually test. In my user coordinates, the sine wave starts
at (x,y)=(0,0). Let's see what happens to these points with my
transformations. First, the translation, (x,y)->(x-xmin,y-ymin), so
(0,0) -> (0,2)
Next, the scale. Obviously, 0 -> 0, regardless of the scale. For 2,
we scale by bRect.size.height/(ymax-ymin). Hence,
2 -> 2 * (560)/(2-(-2)) = 280.
Hence, the scale transform sends
(0,2) -> (0,280).
Which is perfect! Unfortunately, when I plot the sine wave in the
NSVIew, it starts at (0,0).
So, you can see that there is something fundamentally wrong with my
thinking.
Current Code:
//
// DavidView.m
// David
//
// Created by David Arnold on 6/29/07.
// Copyright 2007 David Arnold. All rights reserved.
//
#import "DavidView.h"
@implementation DavidView
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
//code here
}
return self;
}
- (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;
NSRect bRect = [self bounds];
[[NSColor grayColor] set];
NSRectFill(bRect);
NSLog(@"bRect.origin.x = %f\n",bRect.origin.x);
NSLog(@"bRect.origin.y = %f\n",bRect.origin.y);
NSLog(@"bRect.size.width = %f\n",bRect.size.width);
NSLog(@"bRect.size.height = %f\n",bRect.size.height);
NSAffineTransform *xForm = [NSAffineTransform transform];
[xForm translateXBy:-xmin yBy:-ymin];
[xForm scaleXBy:(bRect.size.width/(xmax-xmin)) yBy:
(bRect.size.height/(ymax-ymin))];
NSBezierPath *path = [NSBezierPath bezierPath];
[path moveToPoint:NSMakePoint(x,[self f:x])];
while (x<=xmax) {
y=[self f:x];
[path lineToPoint:NSMakePoint(x,y)];
x+=dx;
}
x=xmax;
y=[self f:x];
[path lineToPoint:NSMakePoint(x,y)];
[[NSColor whiteColor] set];
[path setLineWidth:1];
[path transformUsingAffineTransform:xForm];
[path stroke];
}
- (float)f:(float)x
{
return sin(x);
}
@end
_______________________________________________
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