maps_toolkit icon indicating copy to clipboard operation
maps_toolkit copied to clipboard

Is there any function to give result as Boolean for whether given list coordinates polylines are self interacting or overlapping.

Open pshanmukha opened this issue 2 years ago • 2 comments

I just want to check, whether given list of coordinates points forms a perfect polygon( shouldn't be self intersect or overlap) are not. Perfect polygon --> image Not a perfect polygon --> image

pshanmukha avatar Nov 02 '23 18:11 pshanmukha

I found a working solution to the problem here. https://stackoverflow.com/questions/68174332/how-to-define-self-intersection-of-polygon-in-google-maps-flutter

sirkalmi avatar Nov 03 '23 10:11 sirkalmi

I tried it already but I didn't understand what max(x1,x2) and max(y1, y2) is doing and this gave me an error when implementing.

And also I tried below code it only works some cases.


import 'dart:core';

class LatLng {
  double latitude;
  double longitude;

  LatLng(this.latitude, this.longitude);
}

bool doSegmentsIntersect(LatLng p0, LatLng p1, LatLng p2, LatLng p3) {
  var denominator = (p3.longitude - p2.longitude) * (p1.latitude - p0.latitude) - (p3.latitude - p2.latitude) * (p1.longitude - p0.longitude);
  var ua = (p3.latitude - p2.latitude) * (p0.longitude - p2.longitude) - (p3.longitude - p2.longitude) * (p0.latitude - p2.latitude);
  var ub = (p1.latitude - p0.latitude) * (p0.longitude - p2.longitude) - (p1.longitude - p0.longitude) * (p0.latitude - p2.latitude);

  if (denominator < 0) {
    ua = -ua;
    ub = -ub;
    denominator = -denominator;
  }

  if (ua >= 0.0 && ua <= denominator && ub >= 0.0 && ub <= denominator && denominator != 0) {
    return true; // Segments intersect
  }
  return false; // Segments do not intersect
}

void main() {
  List<LatLng> coordArray = [
    LatLng(0.0, 0.0),
    LatLng(1.0, 1.0),
    LatLng(0.5, 0.0),
    LatLng(0.5, 1.0),
  ];

  if (coordArray.length > 2) {
    final n = coordArray.length - 1;

    for (var i = 1; i < n; i++) {
      for (var j = 0; j < i - 1; j++) {
        final intersect = doSegmentsIntersect(coordArray[i], coordArray[i + 1], coordArray[j], coordArray[j + 1]);
        if (intersect) {
          print("Segments intersect");
          return true; // Return true when an intersection is found
        }
      }
    }
  }

  // If no intersection is found, return false
  return false;
}

pshanmukha avatar Nov 03 '23 10:11 pshanmukha