Given the end points and control points along a bezier curve, this code shows how to get any point and any angle along the curve.
Last active
May 25, 2022 17:34
-
-
Save pbeshai/72c446033a98f99ce1e1371c6eee9644 to your computer and use it in GitHub Desktop.
Compute points and angles along a bezier curve
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: mit | |
height: 320 | |
border: no |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function quadraticPath(t){return"M"+t.start[0]+","+t.start[1]+" Q"+t.control[0]+","+t.control[1]+" "+t.end[0]+","+t.end[1]}function interpolateQuadraticBezier(t,r,a){return function(n){return[Math.pow(1-n,2)*t[0]+2*(1-n)*n*r[0]+Math.pow(n,2)*a[0],Math.pow(1-n,2)*t[1]+2*(1-n)*n*r[1]+Math.pow(n,2)*a[1]]}}function interpolateQuadraticBezierAngle(t,r,a){return function(n){var e=2*(1-n)*(r[0]-t[0])+2*n*(a[0]-r[0]),o=2*(1-n)*(r[1]-t[1])+2*n*(a[1]-r[1]);return Math.atan2(o,e)*(180/Math.PI)}}function cubicPath(t){return"M"+t.start[0]+","+t.start[1]+" C"+t.control1[0]+","+t.control1[1]+" "+t.control2[0]+","+t.control2[1]+" "+t.end[0]+","+t.end[1]}function interpolateCubicBezier(t,r,a,n){return function(e){return[Math.pow(1-e,3)*t[0]+3*Math.pow(1-e,2)*e*r[0]+3*(1-e)*Math.pow(e,2)*a[0]+Math.pow(e,3)*n[0],Math.pow(1-e,3)*t[1]+3*Math.pow(1-e,2)*e*r[1]+3*(1-e)*Math.pow(e,2)*a[1]+Math.pow(e,3)*n[1]]}}function interpolateCubicBezierAngle(t,r,a,n){return function(e){var o=3*Math.pow(1-e,2)*(r[0]-t[0])+6*(1-e)*e*(a[0]-r[0])+3*Math.pow(e,2)*(n[0]-a[0]),c=3*Math.pow(1-e,2)*(r[1]-t[1])+6*(1-e)*e*(a[1]-r[1])+3*Math.pow(e,2)*(n[1]-a[1]);return Math.atan2(c,o)*(180/Math.PI)}}function drawQuadratic(t){var r=g.append("g").attr("class","quadratic");r.append("text").text("Quadratic Bezier").attr("dy","0.4em"),r.append("circle").attr("r",5).attr("class","start-point").attr("cx",t.start[0]).attr("cy",t.start[1]),r.append("circle").attr("r",5).attr("class","end-point").attr("cx",t.end[0]).attr("cy",t.end[1]),r.append("circle").attr("r",3).attr("class","control-point").attr("cx",t.control[0]).attr("cy",t.control[1]),r.append("path").attr("class","curve").attr("d",quadraticPath(t));var a=interpolateQuadraticBezier(t.start,t.control,t.end),n=d3.range(10).map(function(t,r,n){return a(t/(n.length-1))});r.selectAll(".interpolated-point").data(n).enter().append("circle").attr("class","interpolated-point").attr("r",3).attr("cx",function(t){return t[0]}).attr("cy",function(t){return t[1]});var e=interpolateQuadraticBezierAngle(t.start,t.control,t.end),o=d3.range(3).map(function(t,r,n){var o=t/(n.length-1);return{t:o,position:a(o),angle:e(o)}});return r.selectAll(".rotated-point").data(o).enter().append("path").attr("d","M12,0 L-5,-8 L0,0 L-5,8 Z").attr("class","rotated-point").attr("transform",function(t){return"translate("+t.position[0]+", "+t.position[1]+") rotate("+t.angle+")"}),console.log("QUADRATIC"),console.log(JSON.stringify(t)),console.log(JSON.stringify(o,null,2)),r}function drawCubic(t){var r=g.append("g").attr("class","cubic").attr("transform","translate(250, 0)");r.append("text").text("Cubic Bezier").attr("dy","0.4em"),r.append("circle").attr("r",5).attr("class","start-point").attr("cx",t.start[0]).attr("cy",t.start[1]),r.append("circle").attr("r",5).attr("class","end-point").attr("cx",t.end[0]).attr("cy",t.end[1]),r.append("circle").attr("r",3).attr("class","control-point").attr("cx",t.control1[0]).attr("cy",t.control1[1]),r.append("circle").attr("r",3).attr("class","control-point").attr("cx",t.control2[0]).attr("cy",t.control2[1]),r.append("path").attr("class","curve").attr("d",cubicPath(t));var a=interpolateCubicBezier(t.start,t.control1,t.control2,t.end),n=d3.range(10).map(function(t,r,n){return a(t/(n.length-1))});r.selectAll(".interpolated-point").data(n).enter().append("circle").attr("class","interpolated-point").attr("r",3).attr("cx",function(t){return t[0]}).attr("cy",function(t){return t[1]});var e=interpolateCubicBezierAngle(t.start,t.control1,t.control2,t.end),o=d3.range(3).map(function(t,r,n){var o=t/(n.length-1);return{t:o,position:a(o),angle:e(o)}});return r.selectAll(".rotated-point").data(o).enter().append("path").attr("d","M12,0 L-5,-8 L0,0 L-5,8 Z").attr("class","rotated-point").attr("transform",function(t){return"translate("+t.position[0]+", "+t.position[1]+") rotate("+t.angle+")"}),console.log("CUBIC"),console.log(JSON.stringify(t)),console.log(JSON.stringify(o,null,2)),r}var width=500,height=300,padding={top:10,right:10,bottom:10,left:10},svg=d3.select("body").append("svg").attr("width",width).attr("height",height),g=svg.append("g").attr("transform","translate("+padding.left+", "+padding.top+")");drawQuadratic({start:[0,70],end:[200,100],control:[160,20]}),drawCubic({start:[0,70],end:[200,100],control1:[40,180],control2:[160,20]}); | |
//# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInNjcmlwdC5qcyJdLCJuYW1lcyI6WyJxdWFkcmF0aWNQYXRoIiwicSIsInN0YXJ0IiwiY29udHJvbCIsImVuZCIsImludGVycG9sYXRlUXVhZHJhdGljQmV6aWVyIiwidCIsIk1hdGgiLCJwb3ciLCJpbnRlcnBvbGF0ZVF1YWRyYXRpY0JlemllckFuZ2xlIiwiY29uc3QiLCJ0YW5nZW50WCIsInRhbmdlbnRZIiwiYXRhbjIiLCJQSSIsImN1YmljUGF0aCIsImMiLCJjb250cm9sMSIsImNvbnRyb2wyIiwiaW50ZXJwb2xhdGVDdWJpY0JlemllciIsImludGVycG9sYXRlQ3ViaWNCZXppZXJBbmdsZSIsImRyYXdRdWFkcmF0aWMiLCJxdWFkcmF0aWMiLCJnUXVhZHJhdGljIiwiZyIsImFwcGVuZCIsImF0dHIiLCJ0ZXh0IiwicXVhZHJh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nQ3ViaWMuc2VsZWN0QWxsKCcucm90YXRlZC1wb2ludCcpLmRhdGEocm90YXRlZFBvaW50cylcblx0XHQuZW50ZXIoKVxuXHRcdFx0LmFwcGVuZCgncGF0aCcpXG5cdFx0XHRcdC5hdHRyKCdkJywgJ00xMiwwIEwtNSwtOCBMMCwwIEwtNSw4IFonKVxuXHRcdFx0XHQuYXR0cignY2xhc3MnLCAncm90YXRlZC1wb2ludCcpXG5cdFx0XHRcdC5hdHRyKCd0cmFuc2Zvcm0nLCBkID0+IGB0cmFuc2xhdGUoJHtkLnBvc2l0aW9uWzBdfSwgJHtkLnBvc2l0aW9uWzFdfSkgcm90YXRlKCR7ZC5hbmdsZX0pYClcblxuXG5cdGNvbnNvbGUubG9nKCdDVUJJQycpO1xuXHRjb25zb2xlLmxvZyhKU09OLnN0cmluZ2lmeShjdWJpYykpO1xuXHRjb25zb2xlLmxvZyhKU09OLnN0cmluZ2lmeShyb3RhdGVkUG9pbnRzLCBudWxsLCAyKSlcblxuXHRyZXR1cm4gZ0N1YmljO1xufVxuXG5cbmNvbnN0IHF1YWRyYXRpYyA9IHsgc3RhcnQ6IFswLCA3MF0sIGVuZDogWzIwMCwgMTAwXSwgY29udHJvbDogWzE2MCwgMjBdIH07XG5kcmF3UXVhZHJhdGljKHF1YWRyYXRpYyk7XG5cbmNvbnN0IGN1YmljID0geyBzdGFydDogWzAsIDcwXSwgZW5kOiBbMjAwLCAxMDBdLCBjb250cm9sMTogWzQwLCAxODBdLCBjb250cm9sMjogWzE2MCwgMjBdIH07XG5kcmF3Q3ViaWMoY3ViaWMpO1xuIl19 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<title>Compute points and angles along a bezier curve</title> | |
<link href='style.css' rel='stylesheet' /> | |
<body> | |
<script src='https://d3js.org/d3.v4.min.js'></script> | |
<script src='dist.js'></script> | |
</body> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const width = 500; | |
const height = 300; | |
const padding = { top: 10, right: 10, bottom: 10, left: 10 }; | |
const svg = d3.select('body').append('svg') | |
.attr('width', width) | |
.attr('height', height); | |
const g = svg.append('g') | |
.attr('transform', `translate(${padding.left}, ${padding.top})`) | |
function quadraticPath(q) { | |
return `M${q.start[0]},${q.start[1]} Q${q.control[0]},${q.control[1]} ${q.end[0]},${q.end[1]}`; | |
} | |
// B(t) = (1 - t)^2P0 + 2(1 - t)tP1 + t^2P2 | |
function interpolateQuadraticBezier(start, control, end) { | |
// 0 <= t <= 1 | |
return function interpolator(t) { | |
return [ | |
(Math.pow(1 - t, 2) * start[0]) + | |
(2 * (1 - t) * t * control[0]) + | |
(Math.pow(t, 2) * end[0]), | |
(Math.pow(1 - t, 2) * start[1]) + | |
(2 * (1 - t) * t * control[1]) + | |
(Math.pow(t, 2) * end[1]), | |
]; | |
}; | |
} | |
// B'(t) = 2(1 - t)(P1 - P0) + 2t(P2 - P1) | |
function interpolateQuadraticBezierAngle(start, control, end) { | |
// 0 <= t <= 1 | |
return function interpolator(t) { | |
const tangentX = (2 * (1 - t) * (control[0] - start[0])) + | |
(2 * t * (end[0] - control[0])); | |
const tangentY = (2 * (1 - t) * (control[1] - start[1])) + | |
(2 * t * (end[1] - control[1])); | |
return Math.atan2(tangentY, tangentX) * (180 / Math.PI); | |
} | |
} | |
function cubicPath(c) { | |
return `M${c.start[0]},${c.start[1]} C${c.control1[0]},${c.control1[1]} ${c.control2[0]},${c.control2[1]} ${c.end[0]},${c.end[1]}`; | |
} | |
// B(t) = (1 - t)^3P0 + 3(1 - t)^2tP1 + 3(1 - t)t^2P2 + t^3P3 | |
function interpolateCubicBezier(start, control1, control2, end) { | |
// 0 <= t <= 1 | |
return function interpolator(t) { | |
return [ | |
(Math.pow(1 - t, 3) * start[0]) + | |
(3 * Math.pow(1 - t, 2) * t * control1[0]) + | |
(3 * (1 - t) * Math.pow(t, 2) * control2[0]) + | |
(Math.pow(t, 3) * end[0]), | |
(Math.pow(1 - t, 3) * start[1]) + | |
(3 * Math.pow(1 - t, 2) * t * control1[1]) + | |
(3 * (1 - t) * Math.pow(t, 2) * control2[1]) + | |
(Math.pow(t, 3) * end[1]), | |
]; | |
}; | |
} | |
// B'(t) = 3(1- t)^2(P1 - P0) + 6(1 - t)t(P2 - P1) + 3t^2(P3 - P2) | |
function interpolateCubicBezierAngle(start, control1, control2, end) { | |
// 0 <= t <= 1 | |
return function interpolator(t) { | |
const tangentX = (3 * Math.pow(1 - t, 2) * (control1[0] - start[0])) + | |
(6 * (1 - t) * t * (control2[0] - control1[0])) + | |
(3 * Math.pow(t, 2) * (end[0] - control2[0])); | |
const tangentY = (3 * Math.pow(1 - t, 2) * (control1[1] - start[1])) + | |
(6 * (1 - t) * t * (control2[1] - control1[1])) + | |
(3 * Math.pow(t, 2) * (end[1] - control2[1])); | |
return Math.atan2(tangentY, tangentX) * (180 / Math.PI); | |
} | |
} | |
// draw a quadratic bezier curve | |
function drawQuadratic(quadratic) { | |
const gQuadratic = g.append('g') | |
.attr('class', 'quadratic'); | |
gQuadratic.append('text') | |
.text('Quadratic Bezier') | |
.attr('dy', '0.4em') | |
// draw the points | |
gQuadratic.append('circle') | |
.attr('r', 5) | |
.attr('class', 'start-point') | |
.attr('cx', quadratic.start[0]) | |
.attr('cy', quadratic.start[1]) | |
gQuadratic.append('circle') | |
.attr('r', 5) | |
.attr('class', 'end-point') | |
.attr('cx', quadratic.end[0]) | |
.attr('cy', quadratic.end[1]) | |
gQuadratic.append('circle') | |
.attr('r', 3) | |
.attr('class', 'control-point') | |
.attr('cx', quadratic.control[0]) | |
.attr('cy', quadratic.control[1]) | |
// draw the path | |
gQuadratic.append('path') | |
.attr('class', 'curve') | |
.attr('d', quadraticPath(quadratic)); | |
const quadraticInterpolator = interpolateQuadraticBezier(quadratic.start, quadratic.control, quadratic.end); | |
const interpolatedPoints = d3.range(10).map((d, i, a) => quadraticInterpolator(d / (a.length - 1))); | |
gQuadratic.selectAll('.interpolated-point').data(interpolatedPoints) | |
.enter() | |
.append('circle') | |
.attr('class', 'interpolated-point') | |
.attr('r', 3) | |
.attr('cx', d => d[0]) | |
.attr('cy', d => d[1]); | |
const quadraticAngleInterpolator = interpolateQuadraticBezierAngle(quadratic.start, quadratic.control, quadratic.end); | |
const rotatedPoints = d3.range(3).map((d, i, a) => { | |
const t = d / (a.length - 1); | |
return { | |
t: t, | |
position: quadraticInterpolator(t), | |
angle: quadraticAngleInterpolator(t), | |
}; | |
}); | |
gQuadratic.selectAll('.rotated-point').data(rotatedPoints) | |
.enter() | |
.append('path') | |
.attr('d', 'M12,0 L-5,-8 L0,0 L-5,8 Z') | |
.attr('class', 'rotated-point') | |
.attr('transform', d => `translate(${d.position[0]}, ${d.position[1]}) rotate(${d.angle})`) | |
console.log('QUADRATIC'); | |
console.log(JSON.stringify(quadratic)); | |
console.log(JSON.stringify(rotatedPoints, null, 2)) | |
return gQuadratic; | |
} | |
function drawCubic(cubic) { | |
const gCubic = g.append('g') | |
.attr('class', 'cubic') | |
.attr('transform', 'translate(250, 0)'); | |
gCubic.append('text') | |
.text('Cubic Bezier') | |
.attr('dy', '0.4em') | |
// draw the points | |
gCubic.append('circle') | |
.attr('r', 5) | |
.attr('class', 'start-point') | |
.attr('cx', cubic.start[0]) | |
.attr('cy', cubic.start[1]) | |
gCubic.append('circle') | |
.attr('r', 5) | |
.attr('class', 'end-point') | |
.attr('cx', cubic.end[0]) | |
.attr('cy', cubic.end[1]) | |
gCubic.append('circle') | |
.attr('r', 3) | |
.attr('class', 'control-point') | |
.attr('cx', cubic.control1[0]) | |
.attr('cy', cubic.control1[1]) | |
gCubic.append('circle') | |
.attr('r', 3) | |
.attr('class', 'control-point') | |
.attr('cx', cubic.control2[0]) | |
.attr('cy', cubic.control2[1]) | |
// draw the path | |
gCubic.append('path') | |
.attr('class', 'curve') | |
.attr('d', cubicPath(cubic)); | |
const cubicInterpolator = interpolateCubicBezier(cubic.start, cubic.control1, cubic.control2, cubic.end); | |
const interpolatedPoints = d3.range(10).map((d, i, a) => cubicInterpolator(d / (a.length - 1))); | |
gCubic.selectAll('.interpolated-point').data(interpolatedPoints) | |
.enter() | |
.append('circle') | |
.attr('class', 'interpolated-point') | |
.attr('r', 3) | |
.attr('cx', d => d[0]) | |
.attr('cy', d => d[1]); | |
const cubicAngleInterpolator = interpolateCubicBezierAngle(cubic.start, cubic.control1, cubic.control2, cubic.end); | |
const rotatedPoints = d3.range(3).map((d, i, a) => { | |
const t = d / (a.length - 1); | |
return { | |
t: t, | |
position: cubicInterpolator(t), | |
angle: cubicAngleInterpolator(t), | |
}; | |
}); | |
gCubic.selectAll('.rotated-point').data(rotatedPoints) | |
.enter() | |
.append('path') | |
.attr('d', 'M12,0 L-5,-8 L0,0 L-5,8 Z') | |
.attr('class', 'rotated-point') | |
.attr('transform', d => `translate(${d.position[0]}, ${d.position[1]}) rotate(${d.angle})`) | |
console.log('CUBIC'); | |
console.log(JSON.stringify(cubic)); | |
console.log(JSON.stringify(rotatedPoints, null, 2)) | |
return gCubic; | |
} | |
const quadratic = { start: [0, 70], end: [200, 100], control: [160, 20] }; | |
drawQuadratic(quadratic); | |
const cubic = { start: [0, 70], end: [200, 100], control1: [40, 180], control2: [160, 20] }; | |
drawCubic(cubic); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.start-point { | |
fill: #fff; | |
stroke: #0bb; | |
} | |
.end-point { | |
fill: #fff; | |
stroke: #0bb; | |
} | |
.control-point { | |
fill: tomato; | |
} | |
.curve { | |
fill: none; | |
stroke: #0bb; | |
stroke-width: 2px; | |
} | |
.interpolated-point { | |
fill: #077; | |
} | |
.rotated-point { | |
fill: #af84e6; | |
stroke: #660cd0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome, thanks for sharing!