Algorithms
Coordinate Mapping
Mathematics
Data Science
Geospatial Analysis

What is this algorithm mapping coordinates to numbers called?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

The name depends on what the mapping is trying to achieve. If you are taking an integer coordinate pair such as (x, y) and turning it into one integer uniquely, the general name is usually a pairing function, and the best-known example is the Cantor pairing function. If the goal is to preserve spatial locality across a grid, then the mapping is more likely a space-filling curve such as Morton order or a Hilbert curve.

The Pairing-Function Case

A pairing function is a bijection from two non-negative integers to one non-negative integer. The canonical example is Cantor pairing:

python
1def cantor_pair(x: int, y: int) -> int:
2    return (x + y) * (x + y + 1) // 2 + y
3
4
5print(cantor_pair(2, 3))  # 18

This guarantees that every (x, y) pair maps to a unique integer and every integer corresponds to one pair. That is useful for:

  • hashing grid coordinates deterministically
  • encoding table keys
  • turning 2D integer positions into a single sortable value

If uniqueness is the main property you care about, "pairing function" is probably the right name.

Inverting the Mapping

One reason Cantor pairing is popular is that it is reversible. You can reconstruct the original coordinates:

python
1import math
2
3
4def cantor_unpair(z: int) -> tuple[int, int]:
5    w = math.floor((math.sqrt(8 * z + 1) - 1) / 2)
6    t = w * (w + 1) // 2
7    y = z - t
8    x = w - y
9    return x, y
10
11
12print(cantor_unpair(18))  # (2, 3)

That reversibility distinguishes pairing functions from many simpler hash-like mappings.

When the Name Is Morton Code Instead

If the algorithm interleaves the bits of x and y, the usual name is a Morton code or Z-order curve, not Cantor pairing.

python
1def morton2d(x: int, y: int) -> int:
2    result = 0
3    for i in range(16):
4        result |= ((x >> i) & 1) << (2 * i)
5        result |= ((y >> i) & 1) << (2 * i + 1)
6    return result
7
8
9print(morton2d(2, 3))

Morton encoding is common in:

  • quadtrees
  • spatial indexes
  • GPU and graphics workloads

Its main appeal is that points near each other in space often remain relatively near each other in the 1D ordering.

When the Name Is Hilbert Curve

If the algorithm is recursively walking a grid in a locality-preserving path, it may be a Hilbert curve. Hilbert ordering usually preserves locality better than Morton order, but it is more complex to compute.

So there are really three related families people often mix together:

  • pairing functions for unique reversible mapping
  • Morton codes for simple locality-aware indexing
  • Hilbert curves for stronger locality preservation

How to Tell Which One You Have

Ask what property the algorithm emphasizes.

If it:

  • uses a closed-form arithmetic formula and is reversible, think pairing function
  • interleaves coordinate bits, think Morton code
  • recursively traverses cells in a rotated pattern, think Hilbert curve

That distinction is more useful than memorizing one label for every coordinate-to-number transformation.

Practical Example Choice

If you only need a unique key for (x, y) in a dictionary or database, Cantor pairing is easy:

python
points = {}
key = cantor_pair(12, 7)
points[key] = "occupied"

If you are building a spatial index where nearby points should cluster together on disk or in memory, Morton or Hilbert ordering is usually the more relevant concept.

Common Pitfalls

The biggest mistake is assuming every coordinate-to-number mapping is "a space-filling curve." Many are not. A pairing function and a space-filling curve solve related but different problems.

Another issue is ignoring the input domain. Classic Cantor pairing is defined for non-negative integers. If your coordinates can be negative or floating-point values, you need an additional encoding step before applying the pairing function.

Finally, do not confuse uniqueness with locality preservation. Cantor pairing gives a unique reversible number, but it does not preserve spatial proximity the way Morton or Hilbert methods aim to.

Summary

  • The general name for a unique reversible (x, y) -> n mapping is a pairing function.
  • The best-known example is the Cantor pairing function.
  • Bit-interleaving coordinate mappings are usually called Morton codes or Z-order curves.
  • Locality-preserving recursive orderings are often Hilbert curves.
  • Choose the term based on whether the goal is uniqueness, reversibility, or spatial locality.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design