Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/turf-bearing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ function calculateFinalBearing(start: Coord, end: Coord) {
// Swap start & end
let bear = bearing(end, start);
bear = (bear + 180) % 360;
return bear;
// Normalize to [-180, 180]; the % 360 above yields [0, 360) which violates
// the documented return range when the initial reverse bearing is positive.
return bear > 180 ? bear - 360 : bear;
}

export { bearing };
Expand Down
13 changes: 13 additions & 0 deletions packages/turf-bearing/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,16 @@ test("bearing", (t) => {
writeJsonFileSync(out + "results.geojson", results);
}
});
test("bearing - final bearing is in [-180, 180] range", (t) => {
// Traveling due west along the equator: start=(10,0), end=(0,0)
// The final bearing at the destination should be -90 (west), not 270.
const west_start = point([10, 0]);
const west_end = point([0, 0]);
const finalWest = bearing(west_start, west_end, { final: true });
t.ok(
finalWest >= -180 && finalWest <= 180,
"final bearing is within [-180, 180]: got " + finalWest
);
t.equal(finalWest.toFixed(2), "-90.00", "final bearing westward is -90");
t.end();
});
Loading