Skip to content
Back to skills

Toroidal Manhattan Distance

ASecurity

Compute shortest Manhattan distance on a toroidal (wrapping) grid by comparing normal vs wrap-around routes in each axis

  • 61 stars
  • 0 votes
  • 0 copies
  • 2 views
  • Added September 12, 2026
developmentpythongo

Works with

  • cli

Security analysis

A100/100

Scanned September 12, 2026

npx -y skills add wenmin-wu/ds-skills --skill toroidal-manhattan-distance --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Toroidal Manhattan Distance?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Toroidal Manhattan Distance
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/wenmin-wu-toroidal-manhattan-distance/badge)](https://www.skillsdirectory.com/skills/wenmin-wu-toroidal-manhattan-distance)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: tabular-toroidal-manhattan-distance
description: Compute shortest Manhattan distance on a toroidal (wrapping) grid by comparing normal vs wrap-around routes in each axis
---

# Toroidal Manhattan Distance

## Overview

On a toroidal grid (edges wrap around), the shortest path between two points may go through the boundary. For each axis, compare the direct distance with the wrap-around distance and take the minimum. This is essential for pathfinding, nearest-neighbor search, and feature engineering on any game or simulation with periodic boundary conditions.

## Quick Start

```python
def toroidal_manhattan(x1, y1, x2, y2, size):
    """Shortest Manhattan distance on a size x size wrapping grid."""
    dx = abs(x1 - x2)
    dy = abs(y1 - y2)
    dx = min(dx, size - dx)
    dy = min(dy, size - dy)
    return dx + dy

def toroidal_direction(from_pos, to_pos, size):
    """Best direction to move toward target on toroidal grid."""
    dx = (to_pos[0] - from_pos[0]) % size
    dy = (to_pos[1] - from_pos[1]) % size
    best = None
    if dx != 0:
        best = 'EAST' if dx <= size // 2 else 'WEST'
    if dy != 0:
        d = 'NORTH' if dy <= size // 2 else 'SOUTH'
        best = d if best is None else best
    return best

dist = toroidal_manhattan(3, 18, 19, 2, size=21)  # wraps in both axes
```

## Workflow

1. Compute absolute difference in each axis
2. Compare with `size - diff` (wrap-around route)
3. Take minimum per axis, sum for Manhattan distance
4. For direction: use modular arithmetic to determine shortest wrap direction

## Key Decisions

- **Grid size**: must match the simulation's board dimensions
- **Euclidean variant**: replace sum with `sqrt(dx² + dy²)` for Euclidean distance
- **Batch computation**: vectorize with numpy for all-pairs distance matrices
- **Applications**: game AI pathfinding, spatial clustering on periodic domains, molecular simulations

## References

- [Halite Swarm Intelligence](https://www.kaggle.com/code/yegorbiryukov/halite-swarm-intelligence)

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…