lineSegmentIntersection method

double? lineSegmentIntersection(
  1. LineSegment segment

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.


double? lineSegmentIntersection(LineSegment segment) {
  _v3.setValues(-direction.y, direction.x);

  final dot =;
  final t1 = _v2.cross(_v1) / dot;
  final t2 = / dot;
  if (t1 >= 0 && t2 >= 0 && t2 <= 1) {
    return t1;
  return null;