Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a Bezier curve to represent a smoothed polyline?

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?

like image 263
xan Avatar asked Dec 05 '22 05:12

xan


1 Answers

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
like image 169
Ilya Semenov Avatar answered May 13 '23 03:05

Ilya Semenov