intersections method

List<Vector2> intersections(
  1. LineSegment otherSegment

Returns an empty list if there are no intersections between the segments If the segments are concurrent, the intersecting point is returned as a list with a single point


List<Vector2> intersections(LineSegment otherSegment) {
  final result = toLine().intersections(otherSegment.toLine());
  if (result.isNotEmpty) {
    // The lines are not parallel
    final intersection = result.first;
    if (containsPoint(intersection) &&
        otherSegment.containsPoint(intersection)) {
      // The intersection point is on both line segments
      return result;
  } else {
    // In here we know that the lines are parallel
    final overlaps = {
      if (otherSegment.containsPoint(from)) from,
      if (otherSegment.containsPoint(to)) to,
      if (containsPoint(otherSegment.from)) otherSegment.from,
      if (containsPoint(,
    if (overlaps.isNotEmpty) {
      final sum =;
      return [sum..scale(1 / overlaps.length)];
  return [];