General Category > General Game and Application Development

Convert lines to bezier curve


Anyone know how to convert a bunch of lines into a bezier curve?  Basically I want to go from the first image below to the second image.  I know it is possible as I have done it with inkscape, but I want to do it in code.

I've got this little function hidden away in my maths module:

--- Code: ---Function Bezier:Float(p1:Float, p2:Float, cp1:Float, cp2:Float, t:Float)
Local v:Float = p1 * Pow( (1 - t), 3) + 3 * cp1 * Pow( (1 - t), 2) * t + 3 * cp2 * (1 - t) * Pow(t, 2) + p2 * Pow(t, 3)
Return v
--- End code ---


--- Code: ---Function BezierCurve(sx:Float,sy:Float,ex:Float,ey:Float,p1x:Float,p1y:Float,p2x:Float,p2y:Float, t:Float,pointx:Float Var, pointy:Float Var)
Local neg:Float =1-t
pointx:Float  = sx * (neg) ^ 3 + 3 * p1x * (neg) ^ 2 * t + 3 * p2x * (neg) * t ^ 2 + ex * t ^ 3
pointy:Float  = sy * (neg) ^ 3 + 3 * p1y * (neg) ^ 2 * t + 3 * p2y * (neg) * t ^ 2 + ey * t ^ 3
End Function
--- End code ---

Steve Elliott:
A long time a go I wrote a Galaxians-type game and I wanted some of the aliens to follow a smooth path, so I wrote a routine that produced lots of points along a bezier curve and read in the data at load time.  The aliens went from point to point.

I've found the game, but not the program to actually produce the bezier curve at the moment...

Looks like therevills has something.

Drawing a bezier curve (or a similar thing like catmull rom splines) is not what the OP wants, he wants to convert data points into a (set of) bezier curve(s).

Seems it is not as trivial as drawing the bezier curve once you got the parameters.


@Derron:  That seems to be what I'm looking for, thanks.  I actually have the book Graphics Gems.  When I saw that C code, I had to dig up my old book and, sure enough, the code is in there.  Now to convert it to BlitzMax. :)


[0] Message Index

Go to full version