Hilbert Curve Generator

Hilbert Curve Generator preview image

1 collaborator

Glenn_fink Glenn Fink (Author)

Tags

(This model has yet to be categorized with any tags)
Visible to everyone | Changeable by everyone
Model was written in NetLogo 5.0.5 • Viewed 1481 times • Downloaded 33 times • Run 0 times
Download the 'Hilbert Curve Generator' modelDownload this modelEmbed this model

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)


WHAT IS IT?

The model draws a Hilbert space-filling curve of order 1 to 9. A Hilbert curve (also known as a Peano-Hilbert curve is a continuous fractal that touches every square of a grid without crossing lines.

HOW IT WORKS

There is a single agent that starts in the lower righthand corner and draws the Hilbert curve of the specified degree.

HOW TO USE IT

Choose a degree. Hit run. Pretty.

THINGS TO TRY

Try moving the sliders and the switch. The degree slider chooses how convoluted the curve will be. The slowly? switch slows down the drawing by adding delays so you can see how it is drawn. By adjusting the colorChangeRate slider you can make rainbow Hilbert curves of arbitrary colorfulness.

EXTENDING THE MODEL

It would be fun to have a whole bunch of turtles making the curve cooperatively. Or have turtles navigate the "maze" created after the curve is drawn.

CREDITS AND REFERENCES

This model was adapted from JavaScript code found at http://en.wikipedia.org/wiki/Hilbert_curve.

Comments and Questions

Please start the discussion about this model! (You'll first need to log in.)

Click to Run Model

globals [
  U       ; up direction
  L       ; left
  D       ; down
  R       ; right
  step    ; width of each step of the trail
  agent
  waitsecs
  line-color
]

to setup 
  clear-all
  set U   0
  set L 270
  set D 180
  set R  90
  crt 1 [setxy (max-pxcor - 1) (min-pycor + 1) set heading D set agent self pen-down]
  set waitsecs exp (- degree)
end ; setup

to move [dir]
  ask agent [
    set color (color + colorChangeRate)
    set heading dir 
    fd step
  ]
  if slowly? [display wait waitsecs]
end ; move

to hilbert [level dir]
  ifelse level <= 1 [
    if dir = L [move R move D move L]
    if dir = R [move L move U move R]
    if dir = U [move D move R move U]
    if dir = D [move U move L move D]
  ]
  [
    if dir = L [
      hilbert (level - 1) U
      move R
      hilbert (level - 1) L
      move D
      hilbert (level - 1) L
      move L
      hilbert (level - 1) D
    ]
    if dir = R [
      hilbert (level - 1) D
      move L
      hilbert (level - 1) R
      move U
      hilbert (level - 1) R
      move R
      hilbert (level - 1) U
    ]
    if dir = U [
      hilbert (level - 1) L
      move D
      hilbert (level - 1) U
      move R
      hilbert (level - 1) U
      move U
      hilbert (level - 1) R
    ]
    if dir = D [
      hilbert (level - 1) R
      move U
      hilbert (level - 1) D
      move L
      hilbert (level - 1) D
      move D
      hilbert (level - 1) L
    ]
  ]
end ; hilbert

to go
  setup
  set step ((((max-pxcor - min-pxcor) / 2) - 1) / (2 ^ (degree - 1)))
  hilbert degree ([heading] of agent)
end ; go  

There is only one version of this model, created over 10 years ago by Glenn Fink.

Attached files

File Type Description Last updated
Hilbert Curve Generator.png preview Preview for 'Hilbert Curve Generator' over 10 years ago, by Glenn Fink Download

This model does not have any ancestors.

This model does not have any descendants.