getPolygonIntersection static method

List<LatLng> getPolygonIntersection(
  1. List<LatLng> polygon1,
  2. List<LatLng> polygon2
)

Get the intersection points of two polygons.

polygon1 - A list of LatLng coordinates representing the vertices of the first polygon. polygon2 - A list of LatLng coordinates representing the vertices of the second polygon.

Returns a list of LatLng points representing the intersection points of the two polygons.

Implementation

static List<LatLng> getPolygonIntersection(
    List<LatLng> polygon1, List<LatLng> polygon2) {
  final List<LatLng> intersectionPoints = <LatLng>[];

  for (int i = 0; i < polygon1.length; i++) {
    final int j = (i + 1) % polygon1.length;
    final LatLng edge1Start = polygon1[i];
    final LatLng edge1End = polygon1[j];

    for (int k = 0; k < polygon2.length; k++) {
      final int l = (k + 1) % polygon2.length;
      final LatLng edge2Start = polygon2[k];
      final LatLng edge2End = polygon2[l];

      final LatLng? intersection =
          _getLineIntersection(edge1Start, edge1End, edge2Start, edge2End);
      if (intersection != null) {
        intersectionPoints.add(intersection);
      }
    }
  }
  return intersectionPoints;
}