60 lines
1.1 KiB
JavaScript
60 lines
1.1 KiB
JavaScript
|
export default function(a, b, x0, y0, x1, y1) {
|
||
|
var ax = a[0],
|
||
|
ay = a[1],
|
||
|
bx = b[0],
|
||
|
by = b[1],
|
||
|
t0 = 0,
|
||
|
t1 = 1,
|
||
|
dx = bx - ax,
|
||
|
dy = by - ay,
|
||
|
r;
|
||
|
|
||
|
r = x0 - ax;
|
||
|
if (!dx && r > 0) return;
|
||
|
r /= dx;
|
||
|
if (dx < 0) {
|
||
|
if (r < t0) return;
|
||
|
if (r < t1) t1 = r;
|
||
|
} else if (dx > 0) {
|
||
|
if (r > t1) return;
|
||
|
if (r > t0) t0 = r;
|
||
|
}
|
||
|
|
||
|
r = x1 - ax;
|
||
|
if (!dx && r < 0) return;
|
||
|
r /= dx;
|
||
|
if (dx < 0) {
|
||
|
if (r > t1) return;
|
||
|
if (r > t0) t0 = r;
|
||
|
} else if (dx > 0) {
|
||
|
if (r < t0) return;
|
||
|
if (r < t1) t1 = r;
|
||
|
}
|
||
|
|
||
|
r = y0 - ay;
|
||
|
if (!dy && r > 0) return;
|
||
|
r /= dy;
|
||
|
if (dy < 0) {
|
||
|
if (r < t0) return;
|
||
|
if (r < t1) t1 = r;
|
||
|
} else if (dy > 0) {
|
||
|
if (r > t1) return;
|
||
|
if (r > t0) t0 = r;
|
||
|
}
|
||
|
|
||
|
r = y1 - ay;
|
||
|
if (!dy && r < 0) return;
|
||
|
r /= dy;
|
||
|
if (dy < 0) {
|
||
|
if (r > t1) return;
|
||
|
if (r > t0) t0 = r;
|
||
|
} else if (dy > 0) {
|
||
|
if (r < t0) return;
|
||
|
if (r < t1) t1 = r;
|
||
|
}
|
||
|
|
||
|
if (t0 > 0) a[0] = ax + t0 * dx, a[1] = ay + t0 * dy;
|
||
|
if (t1 < 1) b[0] = ax + t1 * dx, b[1] = ay + t1 * dy;
|
||
|
return true;
|
||
|
}
|