getSegmentIntersect method Null safety

Point<num>? getSegmentIntersect(
  1. Line line1,
  2. Line line2
)

Get intersect of 2 line segments

Implementation

static Point? getSegmentIntersect(Line line1, Line line2) {
  final x1 = line1.x1;
  final x2 = line1.x2;
  final x3 = line2.x1;
  final x4 = line2.x2;

  final y1 = line1.y1;
  final y2 = line1.y2;
  final y3 = line2.y1;
  final y4 = line2.y2;

  final t = ((x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4)) /
      ((x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4));

  // final u = ((x1 - x3) * (y1 - y2) - (y1 - y3) * (x1 - x2)) /
  //     ((x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4));

  if (t >= 0 && t <= 1) {
    return Point(x1 + t * (x2 - x1), y1 + t * (y2 - y1));
  } else {
    return null;
  }
}