doLinesIntersect static method

bool doLinesIntersect(
  1. ILatLong line1Start,
  2. ILatLong line1End,
  3. ILatLong line2Start,
  4. ILatLong line2End,
)

Checks if two line segments intersect. They do NOT intersect if the intersection point is outside of the given start-end points.

@param line1Start Der Startpunkt des ersten Liniensegments. @param line1End Der Endpunkt des ersten Liniensegments. @param line2Start Der Startpunkt des zweiten Liniensegments. @param line2End Der Endpunkt des zweiten Liniensegments. @return true, wenn sich die Liniensegmente überschneiden, andernfalls false.

Implementation

static bool doLinesIntersect(ILatLong line1Start, ILatLong line1End, ILatLong line2Start, ILatLong line2End) {
  // https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection
  double x1 = line1Start.longitude;
  double y1 = line1Start.latitude;
  double x2 = line1End.longitude;
  double y2 = line1End.latitude;
  double x3 = line2Start.longitude;
  double y3 = line2Start.latitude;
  double x4 = line2End.longitude;
  double y4 = line2End.latitude;

  final x12diff = x1 - x2;
  final x34diff = x3 - x4;
  final y12diff = y1 - y2;
  final y34diff = y3 - y4;
  final denominator = x12diff * y34diff - y12diff * x34diff;

  if (denominator == 0.0) {
    // Die Linien sind parallel.
    return false;
  }

  final x13diff = x1 - x3;
  final y13diff = y1 - y3;
  final tNumerator = x13diff * y34diff - y13diff * x34diff;
  final uNumerator = -(x12diff * y13diff - y12diff * x13diff);

  double t = tNumerator / denominator;
  double u = uNumerator / denominator;

  return t >= 0.0 && t <= 1.0 && u >= 0 && u <= 1.0;
}