![@thi.ng/morton](https://media.thi.ng/umbrella/banners-20230807/thing-morton.svg?ad8d232e)
![Mastodon Follow](https://img.shields.io/mastodon/follow/109331703950160316?domain=https%3A%2F%2Fmastodon.thi.ng&style=social)
[!NOTE]
This is one of 201 standalone projects, maintained as part
of the @thi.ng/umbrella monorepo
and anti-framework.
🚀 Please help me to work full-time on these projects by sponsoring me on
GitHub. Thank you! ❤️
About
Z-order curve / Morton encoding, decoding & range extraction for arbitrary dimensions.
References:
Status
STABLE - used in production
Search or submit any issues for this package
Related packages
Installation
yarn add @thi.ng/morton
ESM import:
import * as m from "@thi.ng/morton";
Browser ESM import:
<script type="module" src="https://esm.run/@thi.ng/morton"></script>
JSDelivr documentation
For Node.js REPL:
const m = await import("@thi.ng/morton");
Package sizes (brotli'd, pre-treeshake): ESM: 2.00 KB
Dependencies
Note: @thi.ng/api is in most cases a type-only import (not used at runtime)
API
Generated API docs
ZCurve class
The ZCurve
class provides nD encoding/decoding and Z index range
extraction (for a given nD bounding box). The maximum per-component
value range is 32 bits (unsigned!).
Note: All Z indices are encoded as ES
BigInt
and therefore only available in environments where BigInt
s are already
supported. No polyfill is provided!
TypeScript projects using this class should set their compile target (in
tsconfig.json
) to "ESNext"
to enable BigInt
support.
import { ZCurve } from "@thi.ng/morton";
const z = new ZCurve(3, 16);
z.encode([60000, 30000, 20000]);
z.decode(67807501328384n);
[...z.range([2, 2, 0], [3, 6, 1])]
[...z.range([2, 2, 0], [3, 6, 1])].map((i) => z.decode(i));
Low level (2D / 3D only)
Source
import * as m from "@thi.ng/morton";
m.mux2(23, 42);
m.demux2(2461)
m.muxScaled2(0.25, 0.75)
m.demuxScaled2(m.muxScaled2(0.25, 0.75))
Authors
If this project contributes to an academic publication, please cite it as:
@misc{thing-morton,
title = "@thi.ng/morton",
author = "Karsten Schmidt",
note = "https://thi.ng/morton",
year = 2015
}
License
© 2015 - 2025 Karsten Schmidt // Apache License 2.0