lineIntersection method
Returns the intersection between two line segmentss defined by p0, p1 and q0, q1. If the lines are not intersecting null is returned.
Implementation
static Offset lineIntersection(Offset p0, Offset p1, Offset q0, Offset q1) {
double epsilon = 1e-10;
Vector2 r = new Vector2(p1.dx - p0.dx, p1.dy - p0.dy);
Vector2 s = new Vector2(q1.dx - q0.dx, q1.dy - q0.dy);
Vector2 qp = new Vector2(q0.dx - p0.dx, q0.dy - p0.dy);
double rxs = cross2(r, s);
if (rxs.abs() < epsilon) {
// The lines are linear or collinear
return null;
}
double t = cross2(qp, s) / rxs;
double u = cross2(qp, r) / rxs;
if ((0.0 <= t && t <= 1.0) && (0.0 <= u && u <= 1.0)) {
return new Offset(p0.dx + t * r.x, p0.dy + t * r.y);
}
// No intersection between the lines
return null;
}