Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
pshanmukha opened this issue Nov 2, 2023 · 2 comments

Comments

@pshanmukha
Copy link

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

@sirkalmi
Copy link

sirkalmi commented Nov 3, 2023

@pshanmukha
Copy link
Author

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;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants