intersectsSegmentPolygon method

bool intersectsSegmentPolygon(
  1. Float32List polygon,
  2. double x1,
  3. double y1,
  4. double x2,
  5. double y2,
)

Implementation

bool intersectsSegmentPolygon(
    Float32List polygon, double x1, double y1, double x2, double y2) {
  final Float32List vertices = polygon;
  final int nn = polygon.length;

  final double width12 = x1 - x2, height12 = y1 - y2;
  final double det1 = x1 * y2 - y1 * x2;
  double x3 = vertices[nn - 2], y3 = vertices[nn - 1];
  for (int ii = 0; ii < nn; ii += 2) {
    final double x4 = vertices[ii], y4 = vertices[ii + 1];
    final double det2 = x3 * y4 - y3 * x4;
    final double width34 = x3 - x4, height34 = y3 - y4;
    final double det3 = width12 * height34 - height12 * width34;
    final double x = (det1 * width34 - width12 * det2) / det3;
    if (((x >= x3 && x <= x4) || (x >= x4 && x <= x3)) &&
        ((x >= x1 && x <= x2) || (x >= x2 && x <= x1))) {
      final double y = (det1 * height34 - height12 * det2) / det3;
      if (((y >= y3 && y <= y4) || (y >= y4 && y <= y3)) &&
          ((y >= y1 && y <= y2) || (y >= y2 && y <= y1))) return true;
    }
    x3 = x4;
    y3 = y4;
  }
  return false;
}