Bezier Curves On The NXT
As some of you from NXTasy may remember, I was working on a Bezier Curve implementation for the NXT in NXC. (I was also competing with Spiller for rendering speed; his code is in BezierCurve.zip\BezierComparison\Bezier_curves2\Bézier_curves2.nxc) I’ve managed to render the curve in the picture above in 286.5 ms. And that’s a 4th degree curve (3 control points and 2 end points) with 52 different points along the curve calculated and interpolated to give a “high resolution” output.
To do this, I added a bunch of my own optimizations and tried to avoid floating-point operations where possible. Then, using Spiller’s idea that:
Can be converted to:
I was able to speed up my drawing routines by up to 200%! (On the GeneralBezierCurve2DOut() drawing routine.)
NOTE: is the degree of the curve. is the Bezier Curve parametric function. represent the control points. and are temporary variables.
NOTE 2: Math geeks, sorry about the “while” notation. If you have any ideas in which I can use pure mathematical notation without resorting to C, please notify me by leaving a comment.
As well as the demos, I’ve included an entire library (BezierCurve.zip\BezierCurve\v0_3\BezierCurve_v0_3.h.nxc). It can:
- Draw curves on the screen using BezierCurve2DOut().
- Draw different shapes using the ShapeType parameter! For example, you can interpolate the points using Lines, make the curve thicker by using [Filled/Clear/etc] Circles/Ellipses, or make “calligraphy” effects using [Filled/Clear/etc] Rectangles.
- Determine the length of the Bezier curve using GeneralBezierCurve2D_Length_LocationType().
- Draw/connect points in an array onto the screen using LocationType_PointsOut(). (And specify the shape too, of course.)
- Pre-calculate Bezier curves and store their values in an array to increase program performance using BezierCurve2D_GetPoints_LocationType().
- And more!
Here’s a video of the demo/speed test programs in action: