tscircuit / tscircuit/math-utils

computeManhattanDistanceBetweenBoxes inflates distance and returns wrong closest points when centers are offset on the overlapping axis

Open
#35 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
0
Forks
11
PR merge metrics
No merged PRs in 30d

Description

Bug

computeManhattanDistanceBetweenBoxes returns an inflated distance and wrong closest points when two boxes are gap-separated on one axis but their centers are offset on the other (overlapping) axis.

Reproduce
import { computeManhattanDistanceBetweenBoxes, computeGapBetweenBoxes } from "@tscircuit/math-utils"

const boxA = { center: { x: 0, y: 0 },   width: 2, height: 2 }   // bounds [-1,1] x [-1,1]
const boxB = { center: { x: 10, y: 0.5 }, width: 2, height: 2 }  // bounds [9,11] x [-0.5,1.5]

computeManhattanDistanceBetweenBoxes(boxA, boxB).distance // → 8.0156097709407  ❌
computeGapBetweenBoxes(boxA, boxB)                        // → 8                ✅

The boxes are 8 apart on x and their y-ranges overlap, so the true minimum distance is exactly 8 (verified by brute-force perimeter sampling). The two functions in the same file disagree.

Root cause

src/nearest-box.ts computes the closest points by clamping each box's center onto the other box's bounds:

pointA.x = clamp(boxA.center.x, b.minX, b.maxX)
pointA.y = clamp(boxA.center.y, b.minY, b.maxY)
pointB.x = clamp(boxB.center.x, a.minX, a.maxX)
pointB.y = clamp(boxB.center.y, a.minY, a.maxY)
const distance = Math.hypot(pointA.x - pointB.x, pointA.y - pointB.y)

On the overlapping axis the two clamped coordinates differ by the center offset (here 0 vs 0.5), so hypot(8, 0.5) = 8.0156 leaks in. The returned pointA/pointB are also on the wrong boxes and are not an actual closest pair, which then makes findNearestPointsBetweenBoxSets pick the wrong nearest box pair.

Impact

findNearestPointsBetweenBoxSets (built on this) is used to choose nearest boxes; inflated per-pair distances corrupt the argmin, so the wrong pair can be selected.

Repro + fix PRs to follow.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in src/nearest-box.ts and compare computeManhattanDistanceBetweenBoxes with computeGapBetweenBoxes using the reproduced boxes. Done means the distance is 8, the returned points form an actual closest pair, and findNearestPointsBetweenBoxSets selects the correct nearest box pair.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.