lineIntersection function

Vector3 lineIntersection(
  1. List<Vector3> line1,
  2. List<Vector3> line2
)

Implementation

Vector3 lineIntersection(List<Vector3> line1, List<Vector3> line2) {
  var xDiff = [line1[0].x - line1[1].x, line2[0].x - line2[1].x];
  var yDiff = [line1[0].y - line1[1].y, line2[0].y - line2[1].y];

  var det = (List<double> a, List<double> b) => a[0] * b[1] - a[1] * b[0];

  var div = det(xDiff, yDiff);

  if (div == 0) {
    throw 'Lines do not intersect';
  }

  var d = [
    det(line1[0].toList(), line1[1].toList()),
    det(line2[0].toList(), line2[1].toList()),
  ];

  var x = det(d, xDiff) / div;
  var y = det(d, yDiff) / div;

  return Vector3(x, y, 0);
}