intersectsLine method

Point? intersectsLine(
  1. Line line
)

Get the intersection point of this ray with a Line segment.

Returns null if they don't intersect.

Implementation

Point? intersectsLine(Line line) {
  final dx = direction.x;
  final dy = direction.y;

  final x1 = line.a.x;
  final y1 = line.a.y;
  final x2 = line.b.x;
  final y2 = line.b.y;

  final denom = dx * (y2 - y1) - dy * (x2 - x1);
  if (denom == 0) return null;

  final t = ((x1 - origin.x) * (y2 - y1) - (y1 - origin.y) * (x2 - x1)) /
      denom;
  final s = ((x1 - origin.x) * dy - (y1 - origin.y) * dx) / denom;

  if (t >= 0 && s >= 0 && s <= 1) {
    return Point(origin.x + t * dx, origin.y + t * dy);
  }
  return null;
}