views:

646

answers:

2

I have a polyline that approximates a curve, and I want to draw it as a smooth curve in PostScript, which supports Bezier curve drawing. To do that, I need to generate two control points between each pair of polyline points. (I can't get the original curve from the source, just the polyline.)

I've had some success using cardinal splines from this description, but the parameters there are different from Wikipedia and GDIPlus.DrawCurve, both of which refer to tension. MS has no details, and Wikipedia has incomplete details (cardinal spline ignores x values?).

What are the formulas for the control points based on tension?

A: 

You can do this with a two step process: first do a cubic b-spline and then get the cubic Bezier curves from this.

The trick with fitting the Bezier curves is matching the derivatives at the segment end points, and this is essentially what the spline does. If you can get at the representation of the spline, it should have the points for the end points and the control point for each segment. Otherwise, you could determine the slopes at each segment end point and the control point will be the intersection of the extrapolated lines.

tom10
+3  A: 

See this link http://www.ibiblio.org/e-notes/Splines/Cardinal.htm which provides simple formulas which can be used to calculate Bezier control points for a multi-segment smooth curve.

The equations are really simple but for those who don't want to repeat the calculations I'm providing my results:

Let Pi (i=1..n) be the polyline points.

First, learn how to calculate the derivatives on Pi:

P1' = (P2 - P1) / a
Pi' = (Pi+1 - Pi-1) / a (for i=2..n-1)
Pn' = (Pn - Pn-1) / a

where "a" is a coeffecient (which probably means "tension" you mentioned), for instance a=2.

Then, for each segment i (i=1..n-1) from Pi to Pi+1, Bezier control points B1i and B2i would be:

B1i = Pi + Pi'/3
B2i = Pi+1 - Pi+1'/3
Ilya Semenov
I was hoping someone knew definitively, but by inspecting the DrawPath results, I think tension there is 2/a in the terms used in your answer.
xan