Giter VIP home page Giter VIP logo

Comments (2)

sirkalmi avatar sirkalmi commented on June 25, 2024

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

from maps_toolkit.

pshanmukha avatar pshanmukha commented on June 25, 2024

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

from maps_toolkit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.