Skip to content

Instantly share code, notes, and snippets.

@kmsheng
Created May 18, 2018 14:21
Show Gist options
  • Save kmsheng/cb02071d9633fd051291a9997edeadf6 to your computer and use it in GitHub Desktop.
Save kmsheng/cb02071d9633fd051291a9997edeadf6 to your computer and use it in GitHub Desktop.
function getPointAlongALine(p0, p1, t) {
return Math.round(((1 - t) * p0 + (t * p1)) * 10) / 10;
}
function reduce(points, t) {
if (points.length === 1) {
return points;
}
const reducedPoints = [];
for (let i = 1; i < points.length; i++) {
const p0 = points[i - 1];
const p1 = points[i];
const px = getPointAlongALine(p0.x, p1.x, t);
const py = getPointAlongALine(p0.y, p1.y, t);
reducedPoints.push({x: px, y: py});
}
return reduce(reducedPoints, t);
}
function getBezierCurvePoints(p0, c0, c1, p1) {
const result = [];
for (let t = 0; t <= 1; t += 0.1) {
result.push(reduce([p0, c0, c1, p1], t));
}
return result;
}
const result = getBezierCurvePoints(
{x: 0, y: 5},
{x: 2, y: 8},
{x: 8, y: 8},
{x: 10, y: 5}
);
console.log('result', result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment