@@ -39,42 +39,41 @@ base class _PolygonPainter<R extends Object>
3939 required LatLng coordinate,
4040 }) {
4141 final polygon = projectedPolygon.polygon;
42-
43- if (! polygon.boundingBox.contains (coordinate)) return false ;
42+ if (! polygon.boundingBox.contains (coordinate)) {
43+ return false ;
44+ }
4445
4546 final projectedCoords = getOffsetsXY (
4647 camera: camera,
4748 origin: hitTestCameraOrigin,
4849 points: projectedPolygon.points,
49- ). toList () ;
50+ );
5051
5152 if (projectedCoords.first != projectedCoords.last) {
5253 projectedCoords.add (projectedCoords.first);
5354 }
55+ final isInPolygon = isPointInPolygon (point, projectedCoords);
5456
5557 final hasHoles = projectedPolygon.holePoints.isNotEmpty;
56- late final List <List <Offset >> projectedHoleCoords;
57- if (hasHoles) {
58- projectedHoleCoords = projectedPolygon.holePoints
59- .map (
60- (points) => getOffsetsXY (
58+ final isInHole = hasHoles &&
59+ () {
60+ for (final points in projectedPolygon.holePoints) {
61+ final projectedHoleCoords = getOffsetsXY (
6162 camera: camera,
6263 origin: hitTestCameraOrigin,
6364 points: points,
64- ).toList (),
65- )
66- .toList ();
65+ );
6766
68- if (projectedHoleCoords.firstOrNull != projectedHoleCoords.lastOrNull) {
69- projectedHoleCoords.add (projectedHoleCoords.first);
70- }
71- }
67+ if (projectedHoleCoords.first != projectedHoleCoords.last) {
68+ projectedHoleCoords.add (projectedHoleCoords.first);
69+ }
7270
73- final isInPolygon = _isPointInPolygon (point, projectedCoords);
74- final isInHole = hasHoles &&
75- projectedHoleCoords
76- .map ((c) => _isPointInPolygon (point, c))
77- .any ((e) => e);
71+ if (isPointInPolygon (point, projectedHoleCoords)) {
72+ return true ;
73+ }
74+ }
75+ return false ;
76+ }();
7877
7978 // Second check handles case where polygon outline intersects a hole,
8079 // ensuring that the hit matches with the visual representation
@@ -361,24 +360,6 @@ base class _PolygonPainter<R extends Object>
361360 );
362361 }
363362
364- /// Checks whether point [p] is within the specified closed [polygon]
365- ///
366- /// Uses the even-odd algorithm.
367- static bool _isPointInPolygon (math.Point p, List <Offset > polygon) {
368- bool isInPolygon = false ;
369-
370- for (int i = 0 , j = polygon.length - 1 ; i < polygon.length; j = i++ ) {
371- if ((((polygon[i].dy <= p.y) && (p.y < polygon[j].dy)) ||
372- ((polygon[j].dy <= p.y) && (p.y < polygon[i].dy))) &&
373- (p.x <
374- (polygon[j].dx - polygon[i].dx) *
375- (p.y - polygon[i].dy) /
376- (polygon[j].dy - polygon[i].dy) +
377- polygon[i].dx)) isInPolygon = ! isInPolygon;
378- }
379- return isInPolygon;
380- }
381-
382363 @override
383364 bool shouldRepaint (_PolygonPainter <R > oldDelegate) =>
384365 polygons != oldDelegate.polygons ||
0 commit comments