intersections method
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
Implementation
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(otherSegment.to)) otherSegment.to,
};
if (overlaps.isNotEmpty) {
final sum = Vector2.zero();
overlaps.forEach(sum.add);
return [sum..scale(1 / overlaps.length)];
}
}
return [];
}