• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
blog.draftsperson.net

blog.draftsperson.net

the art of technical drawing

blog.draftsperson.net

HILBERT LISP: Turtle Graphics and Space-Filling Curves

August 6, 2026

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

  1. Load the routine with APPLOAD, or put it on your support file search path.
  2. Type HILBERT or KOCH.
  3. Give the order and the size.
  4. 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

NameTurtleDraw — Hilbert curves and Koch snowflakes as one polyline
DownloadTurtleDraw.lsp — 7.9 KB, plain AutoLISP source. Downloads directly; no zip to unpack.
TypeAutoCAD AutoLISP routine
CommandsHILBERT, KOCH
Requiresnothing beyond a CAD platform with AutoLISP.
Source size7.9 KB, 204 lines
Error handlingYes — a *error* handler restores every system variable it changed, on cancel as well as on error.
UndoThe whole operation is wrapped in a single undo group, so one U reverses all of it.
CompatibilityAutoCAD (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.
AuthorYZ, August 2026
LicenceFree 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

Filed Under: AutoLISP Routines

Primary Sidebar

Subscribe to our Youtube Channel

Post CAD drafting jobs on Freelancer.com. and get some free money to do this.

Categories

  • American Steel Industry (4)
  • AutoCAD Articles & Reference (30)
  • AutoCAD Bugs & Problems (19)
  • AutoCAD Tips (29)
  • AutoCAD Tutorials (54)
  • AutoLISP Routines (328)
  • AutoLISP Tutorials (15)
  • CAD Standards (7)
  • Civil 3D (7)
  • Civil Drafting (4)
  • Dimensions (2)
  • Drafting Funnies (71)
  • Drafting History (9)
  • Drafting Standards (84)
  • Electrical Drafting (2)
  • General IT reference (7)
  • Geometry (15)
  • GIS (2)
  • Hatch Patterns (12)
  • Office Life Hacks (3)
  • PT (1)
  • Revit Tutorials (54)
  • Steel Detailing (14)
  • Structural Drafting (26)
  • Technical Dictionary (61)
  • Useful Website Links (19)

Footer

Free downloads

  • Free AutoLISP routines
  • Free hatch patterns
  • SHX fonts & text styles
  • Numbering LISP collection
  • ISO 3098B font

Reference charts

  • AWS weld symbols chart
  • Steel detailing chart
  • Hole & slot sizes
  • Bolt edge distance
  • ANSI & ARCH paper sizes
  • How to fold an A1 drawing

Useful links

  • CAD blocks (DWG)
  • Revit families
  • structuraldrafter.com
  • cad-corner.com
  • About
  • Privacy policy

Provided for free with no warranty · Log in