Re: NSBezierPath and NSAffineTransform
Re: NSBezierPath and NSAffineTransform
- Subject: Re: NSBezierPath and NSAffineTransform
- From: Scott Thompson <email@hidden>
- Date: Thu, 10 Mar 2005 09:17:48 -0600
I am writing an application that will mainly draw a custom view plotting a curve. I've already used bezierpath before, but in a light way. Now, i feel like i need to dive a bit deeper inside ....
My view is binded to a curve model object. The curve model object has the method
- (NSBezierPath *)plot;
As the user is able to resize the view, the range of the points in the curve is [0.0 ; 1.0] so that i can apply an NSAffineTransform to the returned path of - (NSBezierPath *)plot in the drawrect method of the view.
NSBezierPath *curvePlot = [curve plot];
[curvePlot transformUsingAffineTransform:transform];
[curvePlot stroke];
I have several kind of curves. Some plot lines thanks to the lineToPoint method in NSBezierPath, some others plot curves by calculating the coords of a point, creating a NSRect with these coords and then using the [NSBezierPath appendBezierPathWithRect:....].
The problem is that when i use a rect to add a point to the bezierpath, i have to specify a width and a height. And then, when i use the affinetransform, obviously, the size of the rect also gets scaled and i see big squares instead of seeing small points. This doesn't happen when i use the lineToPoint method, the line stays thin, I assume because of the parameter lineWidth ...
The big issue is that i am novice at plotting curves and maybe the use of a NSRect to plot a point is not the best idea, but i don't seem to find something else.
I wonder how is the lineToPoint working. Even if it is under the hood, there must be some kind of coordinates computing as well, and the plotting all the points in a smart way ?
Think of your path as a list of points. Those points have additional information that describes how the computer should connect them, one to another, but for this discussion, a list of points works well.
When you add a line (lineToPoint) you are simply adding two points. Those two points are subsequently sent through the affine transformation and what you get back is... two points. When you draw the line between the two points... you get pretty much what you expect.
The rectangle works in a similar way. It adds four points and notes that it should connect them with straight line segments. When you transform the points, they all get transformed together. That means that if you scale the coordinate system, the width between opposing points gets longer or shorter). When you ask the computer to fill the rectangle.... it happily does so and you get a rectangle instead of a square.
To get the effect you want, you will have to calculate the position and size of the rectangles in the transformed coordinate space rather in the coordinate space before the transformation is applied. I would create an NSSize that you want the final plotted points to be, and then run that size through the inverse transform of the transformation you want to apply to your graph. Use that as the size for the rectangles you plot for each of your points. When the computer transforms the points, it will be undoing the transformation you applied manually and the rectangles will end up being the "right" size.
Scott
_______________________________________________
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