@@ -39,42 +39,41 @@ base class _PolygonPainter<R extends Object>
39
39
required LatLng coordinate,
40
40
}) {
41
41
final polygon = projectedPolygon.polygon;
42
-
43
- if (! polygon.boundingBox.contains (coordinate)) return false ;
42
+ if (! polygon.boundingBox.contains (coordinate)) {
43
+ return false ;
44
+ }
44
45
45
46
final projectedCoords = getOffsetsXY (
46
47
camera: camera,
47
48
origin: hitTestCameraOrigin,
48
49
points: projectedPolygon.points,
49
- ). toList () ;
50
+ );
50
51
51
52
if (projectedCoords.first != projectedCoords.last) {
52
53
projectedCoords.add (projectedCoords.first);
53
54
}
55
+ final isInPolygon = isPointInPolygon (point, projectedCoords);
54
56
55
57
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 (
61
62
camera: camera,
62
63
origin: hitTestCameraOrigin,
63
64
points: points,
64
- ).toList (),
65
- )
66
- .toList ();
65
+ );
67
66
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
+ }
72
70
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
+ }();
78
77
79
78
// Second check handles case where polygon outline intersects a hole,
80
79
// ensuring that the hit matches with the visual representation
@@ -361,24 +360,6 @@ base class _PolygonPainter<R extends Object>
361
360
);
362
361
}
363
362
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
-
382
363
@override
383
364
bool shouldRepaint (_PolygonPainter <R > oldDelegate) =>
384
365
polygons != oldDelegate.polygons ||
0 commit comments