Skip to content

Instantly share code, notes, and snippets.

@weswigham
Last active September 16, 2019 18:06
Show Gist options
  • Save weswigham/769cc373765f6cec7eb3 to your computer and use it in GitHub Desktop.
Save weswigham/769cc373765f6cec7eb3 to your computer and use it in GitHub Desktop.
Javascript is functional
var catmullRom = function(p0, p1, p2, p3, distance) {
var points = [p0, p1, p2, p3],
t = function(dest) {
if (dest === 0) return 0;
return Math.pow(
Math.sqrt(
Math.pow(points[dest].x - points[dest-1].x,2) + Math.pow(points[dest].y - points[dest-1].y,2) + Math.pow(points[dest].z - points[dest-1].z,2)
), 0.5) + t(dest-1);
},
t0 = t(0),
t1 = t(1),
t2 = t(2),
t3 = t(3),
innerdist = distance*(t2-t1) + t1,
a1 = p0.clone()
.multiplyScalar((t1 - innerdist)/(t1 - t0))
.add(p1.clone().multiplyScalar((innerdist - t0)/(t1 - t0))),
a2 = p1.clone()
.multiplyScalar((t2 - innerdist)/(t2 - t1))
.add(p2.clone().multiplyScalar((innerdist - t1)/(t2 - t1))),
a3 = p2.clone()
.multiplyScalar((t3 - innerdist)/(t3 - t2))
.add(p3.clone().multiplyScalar((innerdist - t2)/(t3 - t2))),
b1 = a1.clone()
.multiplyScalar((t2 - innerdist)/(t2 - t0))
.add(a2.clone().multiplyScalar((innerdist - t0)/(t2 - t0))),
b2 = a2.clone()
.multiplyScalar((t3 - innerdist)/(t3 - t1))
.add(a3.clone().multiplyScalar((innerdist - t1)/(t3 - t1))),
c = b1.clone()
.multiplyScalar((t2 - innerdist)/(t2 - t1))
.add(b2.clone().multiplyScalar((innerdist - t1)/(t2 - t1)));
return c;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment