Skip to content

Commit

Permalink
fix: added polygon validity check before hit testing (#1964)
Browse files Browse the repository at this point in the history
  • Loading branch information
JaffaKetchup authored Sep 12, 2024
1 parent 182c4fc commit cb6d6a0
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions lib/src/layer/polygon_layer/painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,32 +64,30 @@ base class _PolygonPainter<R extends Object>
origin: hitTestCameraOrigin,
points: projectedPolygon.points,
);

if (projectedCoords.first != projectedCoords.last) {
projectedCoords.add(projectedCoords.first);
}
final isInPolygon = 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);
}

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

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;
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

0 comments on commit cb6d6a0

Please sign in to comment.