{"id":19600215,"url":"https://github.com/habedi/morton","last_synced_at":"2026-04-20T14:02:08.556Z","repository":{"id":257805140,"uuid":"831916543","full_name":"habedi/morton","owner":"habedi","description":"A Morton code library for Go","archived":false,"fork":false,"pushed_at":"2024-09-29T17:25:44.000Z","size":7,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-08-27T01:04:30.718Z","etag":null,"topics":["go-library","golang","morton-encoding-library","z-order"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/habedi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-07-22T01:19:54.000Z","updated_at":"2025-03-10T10:11:18.000Z","dependencies_parsed_at":null,"dependency_job_id":"2ecb320c-99d8-4775-b293-7cf3c96fa98c","html_url":"https://github.com/habedi/morton","commit_stats":null,"previous_names":["habedi/morton"],"tags_count":1,"template":false,"template_full_name":"habedi/template-python-project","purl":"pkg:github/habedi/morton","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/habedi%2Fmorton","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/habedi%2Fmorton/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/habedi%2Fmorton/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/habedi%2Fmorton/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/habedi","download_url":"https://codeload.github.com/habedi/morton/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/habedi%2Fmorton/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32050451,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-20T11:35:06.609Z","status":"ssl_error","status_checked_at":"2026-04-20T11:34:48.899Z","response_time":94,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["go-library","golang","morton-encoding-library","z-order"],"created_at":"2024-11-11T09:14:02.105Z","updated_at":"2026-04-20T14:02:08.536Z","avatar_url":"https://github.com/habedi.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Morton Code in Go\n\n[![Tests](https://github.com/habedi/morton/actions/workflows/tests.yml/badge.svg)](https://github.com/habedi/morton/actions/workflows/tests.yml)\n[![Go Report Card](https://goreportcard.com/badge/github.com/habedi/morton)](https://goreportcard.com/report/github.com/habedi/morton)\n[![Go Reference](https://pkg.go.dev/badge/github.com/habedi/morton.svg)](https://pkg.go.dev/github.com/habedi/morton)\n[![License](https://img.shields.io/github/license/habedi/morton)](https://github.com/habedi/morton/blob/main/LICENSE)\n\nThis is an implementation of [Morton code](https://en.wikipedia.org/wiki/Z-order_curve) in Golang. It provides\nfunctions for encoding 2D and 3D points into 1D and decoding them back to the original points. This package has no\nexternal dependencies and provides an easy-to-use API.\n\nI made this package to understand how Morton code works and also to learn how to write and publish Go packages. I hope\nyou find it useful.\n\n## What is Morton code?\n\n**Morton code** (also called the **Z-order curve**) is a way to convert multi-dimensional data points, like 2D or 3D\ncoordinates into single numbers while keeping nearby points close together. This is useful for things like spatial\nindexing in [Quadtrees](https://en.wikipedia.org/wiki/Quadtree) and [Octrees](https://en.wikipedia.org/wiki/Octree), as\nwell as in computer graphics to make memory access faster.\n\nThe main idea is to **interleave** the bits of the numbers. For example, take the 2D point `(10, 13)`:\n\n- `10` in binary is `1010`, and\n- `13` in binary is `1101`.\n\nBy mixing the bits of these numbers via interleaving, we get `11011001`, which is `217` in decimal. This number is the\n**Morton code**.\n\nYou can also reverse the process to get the original point back. This is done by **deinterleaving** the bits of the\nMorton\ncode.\n\nThe image below shows how `(10, 13)` is turned into a Morton code and then is decoded back.\n\n\u003cp style=\"text-align: center;\"\u003e\n  \u003cimg src=\"assets/morton_v2.svg\" alt=\"Morton Code\" style=\"width: 60%; height: auto;\"\u003e\n\u003c/p\u003e\n\n## Installation\n\nYou can install this package using `go get`:\n\n```bash\ngo get github.com/habedi/morton\n\n```\n\n## Implemented Functions\n\nThe package provides the following functions:\n\n| Function     | Input Signature                         | Output                                  | Comments                                                                                                      |\n|--------------|-----------------------------------------|-----------------------------------------|---------------------------------------------------------------------------------------------------------------|\n| **Encode2D** | (`x: uint32`, `y: uint32`)              | `code: uint64`                          | Encodes (`x`, `y`) into a Morton code.                                                                        |\n| **Decode2D** | `code: uint64`                          | (`x: uint32`, `y: uint32`)              | Decodes `code` into its 2D representation.                                                                    |\n| **Encode3D** | (`x: uint32`, `y: uint32`, `z: uint32`) | `uint64`                                | Encodes a (`x`, `y`, `z`) into a Morton code. \u003cbr/\u003eCurrently, `x`, `y`, and `z` can range from `0` to `2^21`. |                                     \n| **Decode3D** | `code: uint64`                          | (`x: uint32`, `y: uint32`, `z: uint32`) | Decodes `code` into its 3D representation.                                                                    |\n\nNote that the inputs and outputs are unsigned integers. So, negative numbers are not supported. Also, the maximum value\nfor `x`, `y`, and `z` is `2^21` (2,097,152) when encoding 3D points. So, bits beyond the 21st bit will be ignored for\nencoding and decoding a 3D point.\n\n## Usage\n\nBelow is an example of how to use this package to encode and decode 2D and 3D points.\n\n```go\npackage main\n\nimport (\n    \"fmt\"\n    \"github.com/habedi/morton\"\n)\n\nfunc main() {\n\n    // Encode and decode 2D\n    demo2D(10, 13)\n\n    // Encode and decode 3D\n    demo3D(10, 13, 5)\n}\n\nfunc demo2D(x uint32, y uint32) {\n    code := morton.Encode2D(x, y)\n    fmt.Printf(\"Morton code of (%d, %d) is %d\\n\", x, y, code)\n\n    x, y = morton.Decode2D(code)\n    fmt.Printf(\"Decoded Morton code %d to (%d, %d)\\n\", code, x, y)\n}\n\nfunc demo3D(x uint32, y uint32, z uint32) {\n    code := morton.Encode3D(x, y, z)\n    fmt.Printf(\"Morton code of (%d, %d, %d) is %d\\n\", x, y, z, code)\n\n    x, y, z = morton.Decode3D(code)\n    fmt.Printf(\"Decoded Morton code %d to (%d, %d, %d)\\n\", code, x, y, z)\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhabedi%2Fmorton","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhabedi%2Fmorton","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhabedi%2Fmorton/lists"}