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

fix: added polygon validity check before hit testing #1964

Merged
merged 2 commits into from
Sep 12, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Minor improvement to remove anonymous function instant invocation pat…
…tern by using `any` method on list
  • Loading branch information
JaffaKetchup committed Sep 12, 2024
commit b15c0741bf26220a7c20cb3219fd96b7475e0a90
38 changes: 16 additions & 22 deletions lib/src/layer/polygon_layer/painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,36 +64,30 @@ base class _PolygonPainter<R extends Object>
origin: hitTestCameraOrigin,
points: projectedPolygon.points,
);

if (projectedCoords.first != projectedCoords.last) {
projectedCoords.add(projectedCoords.first);
}

final isValidPolygon = projectedCoords.length >= 3;
final isInPolygon =
isValidPolygon && isPointInPolygon(point, projectedCoords);

final hasHoles = projectedPolygon.holePoints.isNotEmpty;
final isInHole = hasHoles &&
() {
for (final points in projectedPolygon.holePoints) {
final projectedHoleCoords = getOffsetsXY(
camera: camera,
origin: hitTestCameraOrigin,
points: points,
);

if (projectedHoleCoords.first != projectedHoleCoords.last) {
projectedHoleCoords.add(projectedHoleCoords.first);
}
final isInHole = projectedPolygon.holePoints.any(
(points) {
final projectedHoleCoords = getOffsetsXY(
camera: camera,
origin: hitTestCameraOrigin,
points: points,
);
if (projectedHoleCoords.first != projectedHoleCoords.last) {
projectedHoleCoords.add(projectedHoleCoords.first);
}

final isValidHolePolygon = projectedHoleCoords.length >= 3;
if (isValidHolePolygon &&
isPointInPolygon(point, projectedHoleCoords)) {
return true;
}
}
return false;
}();
final isValidHolePolygon = projectedHoleCoords.length >= 3;
return isValidHolePolygon &&
isPointInPolygon(point, projectedHoleCoords);
},
);

// Second check handles case where polygon outline intersects a hole,
// ensuring that the hit matches with the visual representation
Expand Down
Loading