Re: Smooth a NSBezierPath
Re: Smooth a NSBezierPath
- Subject: Re: Smooth a NSBezierPath
- From: Wagner Truppel <email@hidden>
- Date: Mon, 2 Jul 2007 12:54:49 +0200
Hi Vance,
a reasonably good way to do what you want is to replace each line
segment with a quadratic Bezier, requiring successive curves to meet
smoothly at the common end point of the line segments they replace.
There's some math involved, which I'll spare everyone in this
message. The bottom line is this:
a) compute the first control point:
Q(0) = P(1) + [ P(0) - P(2) ] / 4
b) compute the remaining control points:
Q(n) = 2P(n) - Q(n-1)
Since you're using quadratic Bezier curves, there is only one control
point per line segment, that is, N-1 control points if you have N
vertices.
The gist of the reasoning that leads to the result above is that we
require successive curves to join at a vertex in such a way that
their tangents are the same and their curvatures are as small as
possible.
Once you have the control points, do this (obvious pseudo-code for P
(..) and Q(..)):
NSBezierPath *path = [NSBezierPath bezierPath];
for (n = 0; n < N-1; ++n)
{
[path moveToPoint: P(n)];
[path curveToPoint: P(n+1) controlPoing1: Q(n) controlPoint2: P(n
+1)];
}
[path stroke];
Note that the last control point argument in the curveToPoint call is
the same as the target point. That's because curveToPoint uses a
cubic Bezier and you're going to use a quadratic one.
I've put together a simple Cocoa app to illustrate the result when
you have 4 vertices. I've also written a pdf showing the math, for
the benefit of those who are curious. The app, source code, and the
pdf are archived into a dmg file, here:
http://www.restlessbrain.com/BezierInterpolation.dmg
Keep in mind that I threw the whole thing together very quickly, so
don't expect anything really polished.
Hope this helps.
Wagner
Hello,
I have an array that contains points. There is an equal distance
between all the points on the x axis. However, on the y axis they
can be anywhere between ymin and ymax.
I use NSBezierPath lineToPoint to connect the current point to the
next.
I would like to smooth the line that passes through the points,
instead of having pointy edges at each point. Can this be done
automatically?
I tried using curveToPoint: controlPoing1: controlPoint2, however i
never managed to calculate the control points quite right for them
to look good, there were always some deformities. I am hoping there
is a way to automatically smooth the line, and if not, what is the
correct way to calculate the control points given a set of vertexes
that would from a line?
Thanks.
Vance
_______________________________________________
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