Bézier curves

(I’m putting this in a new discussion as it may be of interest to more than just those involved in the original one which was this one.)

I’ve uploaded a Bézier curve class. It’s part of my graphical library. I use toadkick’s cmodule stuff for loading library code so if you don’t use that then you’ll need to do the following:

  1. Remove the --[==[ and --]==] at the start and end of the file.
  2. Remove the return Bezier at the end.

The code is the Bezier file at this gist. (I might experiment with an AutoGist auto-installer …).

Main features:

  1. A Bezier class for managing (cubic or lower) bézier curves, either mathematically or graphically. When working graphically (ie wanting to draw the curves), you can either specify the width and stroke in advance or it will take the stroke colour and width in effect when the curve is first drawn (after that you need to manually update the style, this avoids unnecessary passing of information to the GPU).
  2. A bezier function which simply draws a bézier cubic with the given parameters using the current stroke style. All of these béziers actually use the same mesh so if you’re drawing the same bézier over and over again it is best to use the class as that avoids needing to send information to the GPU on every draw cycle.
  3. An implementation of John Hobby’s algorithm for producing a family of cubic béziers through a given set of points so that the resulting curve “looks smooth”.
  4. A variant on that algorithm that is a bit quicker and where adding new points doesn’t affect the béziers between previous pairs of points (at least, between all but the last pair).

For examples of the syntax, take a look at the Main.lua file in that gist (the gist won’t run without some modifications since it’s part of my library and uses other stuff from my libraries, but the syntax should be clear).

Cool. This will be helpful in my future games.