UNPKG

1.39 kBJavaScriptView Raw
1var rho = Math.SQRT2,
2 rho2 = 2,
3 rho4 = 4,
4 epsilon2 = 1e-12;
5
6function cosh(x) {
7 return ((x = Math.exp(x)) + 1 / x) / 2;
8}
9
10function sinh(x) {
11 return ((x = Math.exp(x)) - 1 / x) / 2;
12}
13
14function tanh(x) {
15 return ((x = Math.exp(2 * x)) - 1) / (x + 1);
16}
17
18// p0 = [ux0, uy0, w0]
19// p1 = [ux1, uy1, w1]
20export default function(p0, p1) {
21 var ux0 = p0[0], uy0 = p0[1], w0 = p0[2],
22 ux1 = p1[0], uy1 = p1[1], w1 = p1[2],
23 dx = ux1 - ux0,
24 dy = uy1 - uy0,
25 d2 = dx * dx + dy * dy,
26 i,
27 S;
28
29 // Special case for u0 ≅ u1.
30 if (d2 < epsilon2) {
31 S = Math.log(w1 / w0) / rho;
32 i = function(t) {
33 return [
34 ux0 + t * dx,
35 uy0 + t * dy,
36 w0 * Math.exp(rho * t * S)
37 ];
38 }
39 }
40
41 // General case.
42 else {
43 var d1 = Math.sqrt(d2),
44 b0 = (w1 * w1 - w0 * w0 + rho4 * d2) / (2 * w0 * rho2 * d1),
45 b1 = (w1 * w1 - w0 * w0 - rho4 * d2) / (2 * w1 * rho2 * d1),
46 r0 = Math.log(Math.sqrt(b0 * b0 + 1) - b0),
47 r1 = Math.log(Math.sqrt(b1 * b1 + 1) - b1);
48 S = (r1 - r0) / rho;
49 i = function(t) {
50 var s = t * S,
51 coshr0 = cosh(r0),
52 u = w0 / (rho2 * d1) * (coshr0 * tanh(rho * s + r0) - sinh(r0));
53 return [
54 ux0 + u * dx,
55 uy0 + u * dy,
56 w0 * coshr0 / cosh(rho * s + r0)
57 ];
58 }
59 }
60
61 i.duration = S * 1000;
62
63 return i;
64}