A turtle that walks and turns, and two curves drawn with it. HILBERT draws the Hilbert curve — a single line that visits every square of a grid without crossing itself. KOCH draws the Koch snowflake — a triangle with a smaller triangle raised on the middle third of every edge, over and over.
What TurtleDraw does
Both are drawn as a single polyline, which is the point of them. A Hilbert curve is a route, and cutting it or following it with a machine needs it to be one object.
Why a Hilbert curve is useful. Not only decoration. It is the standard way to lay out a serpentine that has to cover an area evenly — underfloor heating loops, hatching for a pen plotter, a milling raster that avoids long returns — because it never crosses itself and never makes a long jump.
Order 1 is four cells, order 2 sixteen, order n is 4n.
How to use it
- Load the routine with
APPLOAD, or put it on your support file search path. - Type
HILBERTorKOCH. - Give the order and the size.
- Pick where it goes.
How it works
The curve is computed as points and drawn as one polyline. The original moved the turtle by building relative coordinate strings — "@12.34<90" — and feeding them to the LINE command, one command call per step.
An order 5 Hilbert curve is a thousand separate LINE commands and a thousand separate objects. Worse, because each step was its own line, the curve could not be offset, joined or followed as a path — the very things it exists for.
Nothing is left behind. No layers named TURTLE-WHITE and the like, no global list of nineteen function names assigned at load time, and the commands end with (princ) rather than returning nil at the console.
The recursion declares its own variables. The original declared DIST and SIGN as locals of C:HILBERT while the recursive routine that used them was a separate function. It worked only because AutoLISP looks names up dynamically, and would break under any other LISP.
Notes and limits
- One polyline, offsettable and followable.
- Order n is 4n cells. Order 6 is 4,096 — check before you go higher.
- No layers or styles are created.
- One undo group covers the curve.
Download and details
| Name | TurtleDraw — Hilbert curves and Koch snowflakes as one polyline |
| Download | TurtleDraw.lsp — 7.9 KB, plain AutoLISP source. Downloads directly; no zip to unpack. |
| Type | AutoCAD AutoLISP routine |
| Commands | HILBERT, KOCH |
| Requires | nothing beyond a CAD platform with AutoLISP. |
| Source size | 7.9 KB, 204 lines |
| Error handling | Yes — a *error* handler restores every system variable it changed, on cancel as well as on error. |
| Undo | The whole operation is wrapped in a single undo group, so one U reverses all of it. |
| Compatibility | AutoCAD (any release with AutoLISP), BricsCAD, ZWCAD, GstarCAD, ProgeCAD and other IntelliCAD-based platforms. AutoCAD LT needs a LISP enabler. No .NET, no ObjectARX, no installer. |
| Author | YZ, August 2026 |
| Licence | Free to use, supplied “as is” with no warranty. |
References
- FRACTREE LISP — the other recursive drawing routine
- HANOI LISP — recursion you can watch solve itself
- HELIX3D LISP — another path built for a machine to follow
- More free AutoLISP routines on this site
- Wisey’s Steel Shapes — free structural steel section drawing program