Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented azimuthToBearing #2410

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Implemented azimuthToBearing #2410

wants to merge 4 commits into from

Conversation

basvdijk
Copy link

@basvdijk basvdijk commented Mar 9, 2023

Please fill in this template.

  • Use a meaningful title for the pull request. Include the name of the package modified.
  • Have read How To Contribute.
  • Run npm test at the sub modules where changes have occurred.
  • Run npm run lint to ensure code style at the turf module level.

Submitting a new TurfJS Module.

  • Overview description of proposed module.
  • Include JSDocs with a basic example.
  • Execute ./scripts/generate-readmes to create README.md.
  • Add yourself to contributors in package.json using "Full Name <@github Username>".

@basvdijk
Copy link
Author

basvdijk commented Mar 9, 2023

Implemented #2399

@basvdijk basvdijk mentioned this pull request Mar 9, 2023
Copy link
Member

@rowanwins rowanwins left a comment

Choose a reason for hiding this comment

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

👍 @basvdijk

@JamesLMilner
Copy link
Collaborator

I think we can merge this :)

@rowanwins rowanwins self-requested a review May 8, 2023 11:50
Copy link
Member

@rowanwins rowanwins left a comment

Choose a reason for hiding this comment

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

Requires a tidyup of the function so that the tests pass

* @returns {number} bearing between -180 and +180 degrees
*/
export function azimuthToBearing(angle: number): number {
return angle < 0 ? angle + 360 : angle;
Copy link
Member

Choose a reason for hiding this comment

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

Pretty sure this isn't working as expected @basvdijk now that I've run CI and seen the failing tests.

Copy link
Collaborator

Choose a reason for hiding this comment

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

this works 😃:

function azimuthToBearing(angle) {
  angle = angle % 360;
  if (angle > 0) return angle > 180 ? angle - 360 : angle;
  return angle < -180 ? angle + 360 : angle;
}

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(255 + 360), -105);

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

  t.end();
});

* and returns an angle between -180 and +180 degrees (positive clockwise), 0 being the north line
*
* @name azimuthToBearing
* @param {number} angle between 0 and 360 degrees
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think angle should not be restricted to 0-360, but any number, you can easily normalize it with mod 360

* @returns {number} bearing between -180 and +180 degrees
*/
export function azimuthToBearing(angle: number): number {
return angle < 0 ? angle + 360 : angle;
Copy link
Collaborator

Choose a reason for hiding this comment

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

this works 😃:

function azimuthToBearing(angle) {
  angle = angle % 360;
  if (angle > 0) return angle > 180 ? angle - 360 : angle;
  return angle < -180 ? angle + 360 : angle;
}

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(255 + 360), -105);

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

  t.end();
});

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.

None yet

4 participants