Skip to content

Commit

Permalink
♻️ Updated azimuthToBearing according to comments on PR #2410
Browse files Browse the repository at this point in the history
  • Loading branch information
basvdijk committed Aug 5, 2024
1 parent a0370b9 commit 0cc6c93
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
4 changes: 3 additions & 1 deletion packages/turf-helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,9 @@ export function bearingToAzimuth(bearing: number): number {
* @returns {number} bearing between -180 and +180 degrees
*/
export function azimuthToBearing(angle: number): number {
return angle < 0 ? angle + 360 : angle;
angle = angle % 360;
if (angle > 0) return angle > 180 ? angle - 360 : angle;
return angle < -180 ? angle + 360 : angle;
}

/**
Expand Down
18 changes: 15 additions & 3 deletions packages/turf-helpers/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -541,11 +541,23 @@ test("bearingToAzimuth", (t) => {
t.end();
});

test("azimuthToBearing", (t) => {
test('azimuthToBearing', t => {
t.equal(azimuthToBearing(0), 0);
t.equal(azimuthToBearing(360), 0);
t.equal(azimuthToBearing(180), 180);

t.equal(azimuthToBearing(40), 40);
t.equal(azimuthToBearing(40 + 360), 40);

t.equal(azimuthToBearing(-35), -35);
t.equal(azimuthToBearing(-35 - 360), -35);

t.equal(azimuthToBearing(255), -105);
t.equal(azimuthToBearing(50), 50);
t.equal(azimuthToBearing(325), -35);
t.equal(azimuthToBearing(255 + 360), -105);

t.equal(azimuthToBearing(-200), 160);
t.equal(azimuthToBearing(-200 - 360), 160);

t.end();
});

Expand Down

0 comments on commit 0cc6c93

Please sign in to comment.