Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

A Balanced Dithering Technique


December 1998/A Balanced Dithering Technique/Sidebar

The Hilbert Curve


The Hilbert curve is a space-filling curve that visits every point in a square grid with a size of 2x2, 4x4, 8x8, 16x16, or any other power of two.

It was described by David Hilbert in 1891. I have heard that Niklaus Wirth published a recursive algorithm to produce Hilbert curves in Algorithms + Data Structures = Programs, Second Edition (Prentice Hall, 1976). I am unable to confirm this because the book is now out of print. I developed the algorithm presented here independently; however, if Wirth's book does indeed contain an implementation of the algorithm, I suspect that my approach is quite similar to his.

The Hilbert curve has applications in image processing, especially image compression and dithering. It has advantages in those operations where the coherence between neighboring pixels is important [3]. The Hilbert curve is also a special version of a quadtree; any image-processing function that benefits from the use of quadtrees may also use a Hilbert curve.

Constructing a Hilbert Curve

The basic elements of a Hilbert curve are what I call "cups" (a square with one open side) and "joins" (a vector that joins two cups). The "open" side of a cup can be top, bottom, left, or right. In addition, every cup has two endpoints, and each of these can be the "entry" point or the "exit" point. So, there are eight possible varieties of cups. In practice, a Hilbert curve uses only four types of cups. In a similar vein, a join has a direction: up, down, left, or right.

A first-order Hilbert curve is just a single cup:

It fills a 2x2 space. The second-order Hilbert curve replaces that cup by four (smaller) cups, which are linked together by three joins

The link between a cup and a join has been marked with a fat dot in the figure.

Figure 2 shows how to produce a second-order curve from any of the four possible cups. Read this figure from left to right. For example, to produce a second-order Hilbert curve from an upward pointing ("up") cup (top row of figure), first produce a left cup; attach a vertical join to its lower endpoint; attach the bottom end of the vertical join to the left endpoint of an up cup; attach a horizontal join to the right endpoint of the up cup; attach the right end of the horizontal join to the left endpoint of another up cup; attach a vertical join to the right endpoint of this up cup; and attach the top of this vertical join to the bottom endpoint of a right cup. Using this method, you can produce a Hilbert curve of the next-highest order from any existing Hilbert curve. Simply replace every cup in the first curve with four cups as outline by Figure 2.

A Hilbert Curve Function

Listing 2 presents a function that computes the Hilbert curve. Note that the curve is symmetrical around the vertical axis. If you were drawing Hilbert curves, it would therefore be sufficient to draw half of the Hilbert curve and flip it on its axis of symmetry.

The Hilbert curve is easy to generate. When applied over a digitized photograph or a ray-traced image, it makes better use of the coherence of neighboring pixels than the traditional scan-line based approach.

For those interested in a three-dimensional variant of the Hilbert curve (a curve that fills a cube), see http://math.uwaterloo.ca/~wgilbert/Research/HilbertCurve/HilbertCurve.html.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.