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 = ({
from: otherSegment.containsPoint(from),
to: otherSegment.containsPoint(to),
otherSegment.from: containsPoint(otherSegment.from),
otherSegment.to: containsPoint(otherSegment.to),
}..removeWhere((_key, onSegment) => !onSegment))
.keys
.toSet();
if (overlaps.isNotEmpty) {
return [
overlaps.fold<Vector2>(
Vector2.zero(),
(sum, point) => sum + point,
) /
overlaps.length.toDouble(),
];
}
}
return [];
}