Function Repository Resource:

MortonCurve

Source Notebook

Generate a Morton (z-order) curve

Contributed by: Jan Mangaldan

ResourceFunction["MortonCurve"][n]

gives the line segments representing the nth-step Morton (z-order) curve.

ResourceFunction["MortonCurve"][n,d]

gives the nth-step Morton curve in d dimensions.

Details and Options

ResourceFunction["MortonCurve"] is also known as the Morton space-filling curve, z-order curve or the Lebesgue curve.
With the default option setting DataRangeAutomatic, ResourceFunction["MortonCurve"][n] returns a Line primitive corresponding to a path that starts at {0,0}, then joins all integer points in the 2n-1 by 2n-1 square and ends at {2n-1,0}.
Setting DataRange{{xmin,xmax},{ymin,ymax},} yields a Morton curve that occupies the specified coordinate ranges.

Examples

Basic Examples (2) 

A 2D z-order curve:

In[1]:=
Graphics[ResourceFunction["MortonCurve"][2]]
Out[1]=

Lengths of the approximations to the Morton curve:

In[2]:=
Table[RegionMeasure[ResourceFunction["MortonCurve"][n]], {n, 5}]
Out[2]=

Visualize the Morton curve in 2D with splines:

In[3]:=
Graphics[{Thickness[Large], ResourceFunction["MortonCurve"][4] /. Line -> BSplineCurve}]
Out[3]=

Scope (4) 

A 2D Morton curve:

In[4]:=
Graphics[ResourceFunction["MortonCurve"][2]]
Out[4]=

A 3D Morton curve:

In[5]:=
Graphics3D[ResourceFunction["MortonCurve"][2, 3]]
Out[5]=

An n-dimensional Morton curve:

In[6]:=
ResourceFunction["MortonCurve"][2, 5] // Short
Out[6]=

Show the Morton curve for different numbers of steps:

In[7]:=
Table[Graphics[ResourceFunction["MortonCurve"][n]], {n, 1, 4}]
Out[7]=

Options (2) 

DataRange (2) 

DataRange allows you to specify the range of mesh coordinates to generate:

In[8]:=
ResourceFunction["MortonCurve"][2, 2]
Out[8]=

Specify a different range:

In[9]:=
ResourceFunction["MortonCurve"][2, 2, DataRange -> {{0, 1}, {0, 1}}]
Out[9]=

Applications (2) 

Visualize the Morton curve in 3D:

In[10]:=
Graphics3D[ResourceFunction["MortonCurve"][3, 3]]
Out[10]=

With tubes:

In[11]:=
% /. Line -> Tube
Out[11]=

Properties and Relations (2) 

MortonCurve consists of lines:

In[12]:=
ResourceFunction["MortonCurve"][1, 2]
Out[12]=

DataRangerange is equivalent to using RescalingTransform[{…},range]:

In[13]:=
Region[ResourceFunction["MortonCurve"][3, DataRange -> {{1, 2}, {1, 3}}], Frame -> True]
Out[13]=

Use RescalingTransform directly:

In[14]:=
box = TransformedRegion[ResourceFunction["MortonCurve"][3], RescalingTransform[{{0, 2^3 - 1}, {0, 2^3 - 1}}, {{1, 2}, {1, 3}}]];
Region[box, Frame -> True]
Out[14]=

Possible Issues (2) 

By default, the coordinates of the Morton curve are not in the unit square:

In[15]:=
ResourceFunction["MortonCurve"][2]
Out[15]=

Use DataRange to generate the Morton curve in the unit square:

In[16]:=
ResourceFunction["MortonCurve"][2, DataRange -> {{0, 1}, {0, 1}}]
Out[16]=

MortonCurve can be too large to generate:

In[17]:=
ResourceFunction["MortonCurve"][30]
Out[17]=

Neat Examples (1) 

Traverse a Morton curve dynamically:

In[18]:=
With[{curve = ResourceFunction["MortonCurve"][6]},
 Manipulate[
  Graphics[{curve, Red, Thick, Line[Take[First[curve], i]]}, ImageSize -> Medium], {i, 1, 4096, 1}, SaveDefinitions -> True]]
Out[18]=

Version History

  • 1.0.1 – 03 May 2021

Source Metadata

Related Resources

License Information