From cb6d6a03125b92a3f6648bb4ae095dadc7a68b3a Mon Sep 17 00:00:00 2001 From: Luka S Date: Thu, 12 Sep 2024 20:00:40 +0100 Subject: [PATCH] fix: added polygon validity check before hit testing (#1964) --- lib/src/layer/polygon_layer/painter.dart | 42 +++++++++++------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/lib/src/layer/polygon_layer/painter.dart b/lib/src/layer/polygon_layer/painter.dart index 878bea4ea..44aff4b8b 100644 --- a/lib/src/layer/polygon_layer/painter.dart +++ b/lib/src/layer/polygon_layer/painter.dart @@ -64,32 +64,30 @@ base class _PolygonPainter 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