Skip to content

Instantly share code, notes, and snippets.

@atlefren
Last active February 4, 2016 22:33
Show Gist options
  • Select an option

  • Save atlefren/04022357ed86009afd79 to your computer and use it in GitHub Desktop.

Select an option

Save atlefren/04022357ed86009afd79 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script type="text/javascript">
function midpoint(p1, p2) {
return (p2 + p1) / 2;
}
function split (points) {
return _.reduce(_.range(1, points.length), function (acc, idx) {
var p1 = points[idx - 1];
var p2 = points[idx];
var m = midpoint(p1, p2);
if (idx === 1) {
return acc.concat([p1, m, p2]);
} else {
return acc.concat([m, p2]);
}
}, []);
}
function splitline(points, num) {
var res = split(points);
return res.length < num ? splitline(res, num): res;
}
var res = splitline([0, 10], 8);
console.log(res);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment