Skip to content

fix(rhumb-bearing): return NaN for coincident points#3082

Open
JSap0914 wants to merge 1 commit into
Turfjs:masterfrom
JSap0914:fix/rhumb-bearing-coincident-points-nan-2478
Open

fix(rhumb-bearing): return NaN for coincident points#3082
JSap0914 wants to merge 1 commit into
Turfjs:masterfrom
JSap0914:fix/rhumb-bearing-coincident-points-nan-2478

Conversation

@JSap0914

Copy link
Copy Markdown
Contributor

Bug

When start and end points are identical (coincident), rhumbBearing() returns 0 (north). This is incorrect — a bearing is geometrically undefined for a zero-length segment. Returning 0 silently implies a northward direction where there is none.

Closes #2478

Root Cause

calculateRhumbBearing computes Math.atan2(deltaLambda, deltaPsi). For coincident points both values are 0, and Math.atan2(0, 0) === 0 in JavaScript — producing a spurious bearing of .

The Geodesy library that this implementation is adapted from (latlon-spherical.js) explicitly checks for this case and returns NaN.

Fix

Added an early-exit guard in calculateRhumbBearing:

if (from[0] === to[0] && from[1] === to[1]) {
  return NaN;
}

This matches the Geodesy reference behaviour and is consistent with what the regular bearing function also produces for coincident points (since Math.atan2(0,0) behaviour is identical there).

Verification

cd packages/turf-rhumb-bearing
pnpm run test:tape

Output:

TAP version 13
# bearing
ok 1 pair1
ok 2 coincident points return NaN
ok 3 coincident points return NaN (final bearing)
ok 4 invalid point

1..4
# tests 4
# pass  4

# ok

AI-assisted contribution.

When start and end points are identical, rhumbBearing() was returning 0
(north) because Math.atan2(0, 0) === 0. This is misleading since no
bearing can be defined for a zero-length segment.

The Geodesy library (which this implementation is adapted from) explicitly
guards for this case and returns NaN. Align turf's behaviour accordingly.

Closes Turfjs#2478
Copilot AI review requested due to automatic review settings June 27, 2026 09:47

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

JSap0914 added a commit to JSap0914/turf that referenced this pull request Jun 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

turf-rhumb-bearing return a valid bearing for coincident points (zero length line)

2 participants