lineSegmentIntersection method
Returns where (length wise) on the ray that the ray intersects the LineSegment or null if there is no intersection.
A ray that is parallel and overlapping with the segment
is considered to
not intersect. This is due to that a single intersection point can't be
determined and that a LineSegment is almost always connected to another
line segment which will get the intersection on one of its ends instead.
Implementation
double? lineSegmentIntersection(LineSegment segment) {
_v1
..setFrom(origin)
..sub(segment.from);
_v2
..setFrom(segment.to)
..sub(segment.from);
_v3.setValues(-direction.y, direction.x);
final dot = _v2.dot(_v3);
final t1 = _v2.cross(_v1) / dot;
final t2 = _v1.dot(_v3) / dot;
if (t1 >= 0 && t2 >= 0 && t2 <= 1) {
return t1;
}
return null;
}