Hilbert Node

john's Avatar

john

15 Nov, 2023 05:05 AM

FILL SHAPES WITH COOL FRACTAL PATTERNS!

ARRANGE OBJECTS IN A NEW WAY!

Attached is a demo of a new node: hilbert. The hilbert node creates a meandering path that fills an arbitrary rectangular area, and is based on the Hilbert Curve fractal. People using Nodebox to drive plotters may find it especially useful. Thanks to Ramanraj for suggesting this.

NOTE: this node requires an external Python module, gilbert2d.py, included in the zip file. The Nodebox code library module was adapted from code by Jakub Červený and was released under a BSD 2-Clause License (free for public use with copyright notice and disclaimers). For an explanation of the algorithm and licensing details see https://github.com/jakubcerveny/gilbert

The hilbert node outputs a single, open, meandering line comprised of short line segments that turn at right angles (with one short diagonal line segment only in rare cases). The path Is comprised of square cells arranged in a grid of rows and columns. By specifying the number of columns and rows, and the length of the base segment, you can fill any rectangular space. You can then use a compound node to fill any other shape.

It's fast (up to a quarter million cells or so) and fun to play with. You can immediately see how it works by simply copying the node and adjusting the size values.

Hilbert takes four parameters:

  • Columns. The number of columns which determine the width of the rectangle.
  • Rows.. The number of rows which determines the height of the rectangle
  • Segment length.. The segment size in pixels which determines how tightly the pattern is packed.
  • Corners only. Check this box to only include points in places where the curve turns. Leave it unchecked to include all points of the grid, even those at regular intervals along longer edges.

In order to see the effect of the "Corners only" checkbox, turn on the Points checkbox above the rendering pane to see little blue dots whenever a point occurs in the path. Checking "Corners only" will reduce the total number of points without affecting the shape of the path. If you leave it unchecked and feed the hilbert node into a point node, you will get a perfect grid of points, just like the output of a grid node, but in a Hilbert Curve order (check Point Numbers above the render pane to see this order).

So you can also think of the hilbert node as an alternative to the grid node which can arrange objects in space in a way that keeps objects near each other in the list also near each other in space.

An easy way of appreciating this effect is shown in the demo. Take a list of squares colored along a color spectrum from red through yellow, green, and blue to violet. If you arrange these squares using a hilbert grid, you will see a more organic flow of color than you would get in a standard left-to-right, top-to-bottom grid. Crank up the number of columns and rows in the hilbert node to see an ever more subtle flow of color. Changing the ratio of columns to rows will also cause the path to meander in different ways. If you set the segment length to less than the square size, you will get a cool crinkling effect. The "Corners only" should remain unchecked for this use; if you check it you will create holes in the grid (which might also be kind of cool).

The top part of the demo shows how to use a hilbert curve to fill an arbitrary shape (in this case a circle). I intersect a hilbert-filled rectangle with a circle using a compound node. Although the result still shows as a single path, it is actually composed of multiple contours because sections of the path along the edge of the circle formed separate islands. To clarify this I use my contours node to reveal 16 separate paths, then create a table to sort by path length and lookup the longest path. I add a clean node at the end to tidy up minor artifacts along the outer edge produced by the compound node.

The hilbert node produces a Hilbert-like path, not a classic Hilbert Curve. A classic Hilbert Curve is a perfect square with sides that are powers of two (e.g. 32 x 32). If you set the column and row values to values like that, you will get a perfect Hilbert Curve. But if you supply different values, you will get curves which meander in the same way but fill arbitrary rectangles, even with odd sides. In certain cases (when the larger dimension is odd and the smaller is even) a single diagonal step cannot be avoided (for example, try 15x12).

The path created by the hilbert node always starts at the origin and proceeds to the right and down. If the number of rows is less than or equal to the number of columns, the path will start at the upper left of the rectangle and exit at the upper right. If there are more rows than columns, it will exit at the lower left instead. If you want to change which side the path starts and ends on, use rotate or reflect nodes.

For more about the Hilbert Curve, see: https://en.wikipedia.org/wiki/Hilbert_curve

Hilbert will be added to the next release of the Cartan Node Library.

Enjoy!

  1. 1 Posted by florisdejonge on 09 Dec, 2023 02:07 PM

    florisdejonge's Avatar

    This is again a really cool node, John. It's fast, both in rendering and while plotting! Looking forward to trying the other recent new nodes.

  2. Support Staff 2 Posted by john on 11 Dec, 2023 08:49 AM

    john's Avatar

    Thanks, Floris! I love what you did with it. Your inkblots are cool.

    John

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Already uploaded files

  • Hilbert_Screenshot.png 386 KB
  • hilbert_node.zip 7.67 KB

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac

Recent Discussions

24 Jan, 2025 07:50 AM
22 Jan, 2025 11:42 PM
21 Jan, 2025 09:43 AM
21 Jan, 2025 09:41 AM
21 Jan, 2025 09:36 AM

 

21 Jan, 2025 02:00 AM
16 Jan, 2025 08:30 PM
15 Jan, 2025 06:37 AM
04 Jan, 2025 04:09 AM
03 Jan, 2025 05:32 PM
29 Dec, 2024 12:06 PM