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

Speed up the pointInPolygon function. #1052

Merged
merged 1 commit into from
Jun 3, 2020
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
- Added a track feature (#1040)
- Added a geo.gui.scaleWidget.formatUnit utility function (#1048)

### Improvements
- The pointInPolygon2D function is faster (#1052)

## Version 0.19.8

### Changes
Expand Down
10 changes: 6 additions & 4 deletions src/util/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ var util = {
return false;
}
}
return util.distanceToPolygon2d(point, inner ? {outer: outer, inner: inner} : outer) > 0;
return util.distanceToPolygon2d(point, inner ? {outer: outer, inner: inner} : outer, true) > 0;
},

/**
Expand Down Expand Up @@ -680,10 +680,12 @@ var util = {
*
* @param {geo.geoPosition} pt The point.
* @param {geo.polygonObject} poly The polygon.
* @param {boolean} [onlySign] If truthy, only the sign of the answer is
* significant.
* @returns {number} The signed distance.
* @memberof geo.util
*/
distanceToPolygon2d: function (pt, poly) {
distanceToPolygon2d: function (pt, poly, onlySign) {
let outer = poly.outer || poly;
let inside = false,
minDistSq, distSq, dist;
Expand All @@ -693,14 +695,14 @@ var util = {
if (((p0.y > pt.y) !== (p1.y > pt.y)) && (pt.x < (p1.x - p0.x) * (pt.y - p0.y) / (p1.y - p0.y) + p0.x)) {
inside = !inside;
}
distSq = util.distance2dToLineSquared(pt, p0, p1);
distSq = onlySign ? 1 : util.distance2dToLineSquared(pt, p0, p1);
Copy link
Member

Choose a reason for hiding this comment

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

so if onlySign is true, the function will return distSq == 1? I did not look at the entire block, just the diff.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

distSq will be 1 (because we don't care about the actual value of the distance), but the returned result will be -1 or +1 depending on the value of the inside flag.

Copy link
Member

Choose a reason for hiding this comment

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

thanks makes sense.

if (minDistSq === undefined || distSq < minDistSq) {
minDistSq = distSq;
}
}
if (poly.inner) {
poly.inner.forEach(inner => {
let innerDist = util.distanceToPolygon2d(pt, inner);
let innerDist = util.distanceToPolygon2d(pt, inner, onlySign);
if (innerDist * innerDist < minDistSq) {
minDistSq = innerDist * innerDist;
}
Expand Down