Bezier Curves

I wont go much into the details of elaborating on Bezier curves. On a very surface level of description a cubic Bezier is curve that is defined by 4 points. Two endpoints and two control points, given by the equation

B(t)=(1t)3 p0+(1t)2t p1+(1t)t2 p2+t3 p3 B(t) = (1-t)^3\ p_0 + (1-t)^2t\ p_1 + (1-t)t^2\ p_2 + t^3\ p_3

p0p_0 and p3p_3 are the end points and p1p_1 and p2p_2 are the end points.

The basic idea is this suppose we had some kind of "pencil" tool that draws by dropping points (and connecting them) as we drag the mouse across the screen, can one convert it to a cubic bezier curve or the closest approximation of it? Sure! Just get the bunch of points that your hand drawn curve has. For all bezier curves with these two fixed end points as the hand drawn curve , find the right control points that get the points as close as possible to the bezier. This minimization one can use good old gradient descent.

Gradient descent requires computing the derivative of functions. For automatic differentiation of (almost) arbitrary code there exists Enzyme, if you write functions that take some number ( or a bunch of numbers) and outputs a number , you can simply pass it to enzyme and it will tell you the derivative w.r.t the input values. This post will be more of a tutorial on how to use enzyme compile it to wasm and

Coding it up in C

check out the source here

bezier

first we go about definining the bezier curve in C