Hilbert curves are cool

Hilbert curves are cool.  They’re a form of fractal comprised entirely of 90 degree angles. It’s a way to make a single continuous line fill up any given space (2D, 3D, and beyond).

hilbert1

The background to this blog is a hilbert curve I rendered myself (code).  Above is a simpler version of the curve, blown up so you can see it, with the color of the line changing so you can see how it flows.

It has a some cool properties.  For example, if you subdivide the image width-wise and height-wise by a power of two, you get squares that also contain one contiguous part of the line.  It turns out this is important in computer simulations.  Because of how CPU caching works, it’s faster to read a bunch of bytes next to each other than ones far apart. So if you’re simulating weather patterns or particle physics or whatever, you have to map memory (which is one-dimensional) to 2D or 3D space.  If you use a hilbert curve to do that mapping, then when you divide the space up to work on it, each little square or block will live on a continuous piece of memory, and your program will run much faster.

Also, I just think they look cool.  I played with a few filters and came up with some cool images of the 2D hilbert curve:

hilbert-10-lavender-sin729x hilbert-10-rainbowedges-sin81x hilbert-10-rainbowedges-sin81x-quadmirror-rot45 hilbert-10-ice-sin81x hilbert-10-ice-sin9x hilbert-10-ice-sawtooth hilbert-10-fire-sin81x

Also, they work in 3D too.  I wanted to see this, so I wrote a program to build a 3D hilbert curve in Minecraft:

2011-01-17_22.34.05 2011-01-17_22.31.40 2011-01-17_22.31.332

The code to render these is here.  This code is two years old (Jan 2011), and it’s based on the pymclevel Python module, which may be dead or different by now.  Also, it outputs schematic files, which was a format you fed to a Minecraft map editor to plop the object into an existing world.

I also rendered another cool fractal, the Menger sponge:

2011-01-17_17.58.03 2011-01-17_17.58.30 2011-01-17_17.58.48 2011-01-17_17.59.40

Code to generate that is here.

4 Replies to “Hilbert curves are cool”

  1. Well, after a day of messing around with javascript and coming up with my own original method for Hilbert curve generation…

    http://imgur.com/a/bAzhH

    Those screenshots are from an iteration 5 Hilbert Curve, iteration 1 being the basic 7 segment structure.

      • Haha, thanks man. 🙂

        I’m not done yet. I still have a bunch of other fractals to take care of. Furthermore, my current Hilbert method can’t handle iteration level 6 Hilbert Curves, which has me a bit saddened. “Too complex Regex” javascript error. >_>

        So I’m trying a different method to bypass the error altogether.

        MUST. HAVE. MORE. FRACTAL.

Leave a Reply to Mator Cancel reply

Your email address will not be published. Required fields are marked *

*