Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function getLength(points, steps) {
steps = steps || 20;
var length = 0;
var last = points[0];
var size = points.length;
for (var i = 1; i < size; i += 3) {
var p0 = last;
var p1 = points[i];
var p2 = points[i+1];
var p3 = points[i+2];
var x1 = p0.x;
var y1 = p0.y;
for (var t = 0; t <= 1; t += 1 / steps) {
var x = interpolate(p0.x, p1.x, p2.x, p3.x, t);
var y = interpolate(p0.y, p1.y, p2.y, p3.y, t);
var dx = x1 - x;
var dy = y1 - y;
length += Math.sqrt(dx * dx + dy * dy);
x1 = x;
y1 = y;
}
last = p3;
}
function interpolate(a, b, c, d, t) {
var t2 = t * t;
var t3 = t2 * t;
return a + (-a * 3 + t * (3 * a - a * t)) * t + (3 * b + t * (-6 * b + b * 3 * t)) * t + (c * 3 - c * 3 * t) * t2 + d * t3;
}
return length;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment