Perl Implementation of Hilberts space filling Curve

Math::Curve::Hilbert - Perl Implementation of Hilberts space filling Curve

use Math::Curve::Hilbert;

# get object representing 8x8 curve with a step of 10 (i.e. draw 80x80 pixels) my $hilbert = Math::Curve::Hilbert->new( direction=>'up', max=>3, clockwise=>1, step=>10);

# get a point from coordinates my $point = $hilbert->PointFromCoordinates(20,60);

# get coordinates from a point my ($x,$y) = $hilbert->CoordinatesFromPoint($point);

# get range(s) from box my @ranges = $hilbert->RangeFromCoordinates($x1,$y1,$x2,$y2);

# # draw image representing curve

use GD; # create a new image my $im = new GD::Image(300,300); my $black = $im->colorAllocate(0,0,0); my $blue = $im->colorAllocate(0,0,255);

my $count = 0; my ($x1,$y1) = $hilbert->CoordinatesFromPoint($count++); while ( ($hilbert->CoordinatesFromPoint($count))[0] ) { my ($x2,$y2) = $hilbert->CoordinatesFromPoint($count++); $im->line($x1,$y1,$x2,$y2,$black); ($x1,$y1) = ($x2,$y2); }

The Hilbert Curve module provides some useful functions using Hilberts Space-filling Curve. This is handy for things like Dithering, Flattening n-dimensional data, fractals - all kind of things really.

``A Space Filling Curve is a special fractal curve which has the following basic characteristics: it covers completely an area, a volume or a hyper-volume in a 2-d, 3-d or N-d space respectively, each point is visited once and only once (the curve does not cross itself), and neighbor points in the native space are likely to be neighbors in the space filling curve.'' definition from Multiple Range Query Optimization in Spatial Databases, Apostolos N. Papadopoulos and Yannis Manolopoulos

Other space filling curves include The Peano and Morton or Z-order curves. There is also the Hilbert II curve which has an 'S' shape rather than a 'U' shape. The Hilbert curve can also be applied to 3 dimensions, but this module only supports 2 dimensions.

Like most space filling curves, the area must be divided into 2 to the power of N parts, such as 8, 16, 32, etc

None by default.

# get object representing 8x8 curve with a step of 10 (i.e. draw 80x80 pixels) my $hilbert = Math::Curve::Hilbert->new( direction=>'up', max=>3, clockwise=>1, step=>10);

direction specifies which direction the curve follows :

up (clockwise) : up, right, down down (clockwise ) : down, right, up left (clockwise) : left, up, right right (clockwise) : right, down, left

clockwise specifies if the curve moves clockwise or anti-clockwise, the default is clockwise

max specifies the size of the grid to plot in powers of 2 - max=>2 would be a 4x4 grid, max=>4 would be 16 x 16 grid

step specifies how large a step should be (used in drawing the curve), the default is 1

X and Y allow you to specify a starting X and Y coordinate by passing a reference to a the value

my $point = $hilbert->PointFromCoordinates(20,60);

my ($x1,$y1) = $hilbert->CoordinatesFromPoint($point);

# get range(s) from box my @ranges = $hilbert->RangeFromCoordinates($x1,$y1,$x2,$y2);

A. J. Trevena, <teejay@droogs.org>

*perl*.

Programminig

Wy

Wy

yW

Wy

Wy

Wy

yW

Wy

Programming

Wy

Wy

Wy

Wy

Wy

Wy

Wy

Wy