Skip to content

Instantly share code, notes, and snippets.

@stephanbogner
Created June 18, 2017 23:39
Show Gist options
  • Save stephanbogner/2c5ddb34596bc3228bd38e8302934f18 to your computer and use it in GitHub Desktop.
Save stephanbogner/2c5ddb34596bc3228bd38e8302934f18 to your computer and use it in GitHub Desktop.
Takes a line of vectors and calculates its movement (handy if you animate a line and want to control its speed depending on its complexity)
example();
function calculateBendWay(vectorLengths, deltaAngles){
if (vectorLengths.length !== deltaAngles.length) {
throw 'the number of vectors don\'t match up with the number of angles (one less angle than the number of vectors)'
}
var entireBendWay = 0;
for (var i = 0; i < vectorLengths.length; i++) {
var vectorLength = vectorLengths[i];
var deltaAngle = deltaAngles[i];
var radius = vectorLength;
var circumference = radius * 2 * Math.PI;
var circleFraction = circumference/360*deltaAngle;
var bendWay = circleFraction;
entireBendWay += bendWay;
}
return entireBendWay;
}
function example(){
console.log( calculateBendWay([5,5],[0,0]) );
console.log( calculateBendWay([5,5],[0,-90]) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment