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

Return Best Distance #61

Merged
merged 2 commits into from
Jun 4, 2020
Merged

Conversation

curran
Copy link
Contributor

@curran curran commented Jun 4, 2020

This PR is a proposed solution for #2.

A fresh take on #14.

Summary of changes:

  • Adds a distance property to returned values.
  • Includes changes to tests.
  • Guarantee that the distance value is 0, not -0, for degenerate cases.
  • Guarantee that there is always a value included for distance (never gets undefined).
  • Stick to ES5 (use var, and Object.assign in tests instead of rest/spread).

Kindly requesting review @mourner @Fil . Feedback welcome!

🙏

polylabel.js Outdated
@@ -73,7 +77,9 @@ function polylabel(polygon, precision, debug) {
console.log('best distance: ' + bestCell.d);
}

return [bestCell.x, bestCell.y];
var poleOfInaccessibility = [bestCell.x, bestCell.y];
poleOfInaccessibility.distance = bestCell.d || 0;
Copy link
Contributor Author

@curran curran Jun 4, 2020

Choose a reason for hiding this comment

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

Note: the || 0 here is intended to replace occurances of -0 with 0. This works because -0 is falsy. Open to suggestions for alternative approaches here - this just seemed the most straightforward and compact, but perhaps a sacrifice of readability.

Copy link

Choose a reason for hiding this comment

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

we could clarify this earlier in polylabel.js#L110:

return minDistSq && (inside ? 1 : -1) * Math.sqrt(minDistSq);

Copy link
Contributor Author

@curran curran Jun 4, 2020

Choose a reason for hiding this comment

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

Ah I see, maybe something like

return minDistSq ? (inside ? 1 : -1) * Math.sqrt(minDistSq): 0;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Or maybe more explicit:

return minDistSq !== 0 ? (inside ? 1 : -1) * Math.sqrt(minDistSq) : 0;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Or this variant might be most readable:

return minDistSq === 0 ? 0 : (inside ? 1 : -1) * Math.sqrt(minDistSq);

polylabel.js Outdated
@@ -73,7 +77,9 @@ function polylabel(polygon, precision, debug) {
console.log('best distance: ' + bestCell.d);
}

return [bestCell.x, bestCell.y];
var poleOfInaccessibility = [bestCell.x, bestCell.y];
poleOfInaccessibility.distance = bestCell.d || 0;
Copy link

Choose a reason for hiding this comment

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

we could clarify this earlier in polylabel.js#L110:

return minDistSq && (inside ? 1 : -1) * Math.sqrt(minDistSq);

@curran
Copy link
Contributor Author

curran commented Jun 4, 2020

Thank you @Fil for reviewing!

I've made one more commit to address your idea. Requesting re-review please.

Copy link
Member

@mourner mourner left a comment

Choose a reason for hiding this comment

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

Thank you!

@mourner mourner merged commit b3be95b into mapbox:master Jun 4, 2020
@mourner mourner mentioned this pull request Jun 4, 2020
@curran
Copy link
Contributor Author

curran commented Jun 4, 2020

Hooray!!! Thank you @mourner .

@curran
Copy link
Contributor Author

curran commented Jun 10, 2020

@mourner Might it be possible to cut a new NPM release with this change? Thank you.

@curran
Copy link
Contributor Author

curran commented Jun 15, 2020

Published as https://www.npmjs.com/package/@datavis-tech/polylabel for now.

Will mark that as deprecated when a new version of this upstream package comes out.

JonLoesch added a commit to JonLoesch/DefinitelyTyped that referenced this pull request Apr 30, 2023
This was added to library in June 2020 -- see mapbox/polylabel#61
weswigham pushed a commit to DefinitelyTyped/DefinitelyTyped that referenced this pull request Jun 6, 2023
* Adding polylabel 'distance' return field

This was added to library in June 2020 -- see mapbox/polylabel#61

* Updating version number as well

* Whoops got confused between version number in two different forks of the lib
Desplandis pushed a commit to Desplandis/DefinitelyTyped that referenced this pull request Jul 3, 2023
…Typed#65352)

* Adding polylabel 'distance' return field

This was added to library in June 2020 -- see mapbox/polylabel#61

* Updating version number as well

* Whoops got confused between version number in two different forks of the lib
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.

3 participants