Skip to content

Instantly share code, notes, and snippets.

@tyt2y3
Last active December 17, 2015 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tyt2y3/5610890 to your computer and use it in GitHub Desktop.
Save tyt2y3/5610890 to your computer and use it in GitHub Desktop.
the simplest snippet for evaluating quadratic bezier curves. only integer computation is involved.
function bezier2(ax, ay, bx, by, cx, cy) {
var minlength = 20;
var dist = Math.sqrt(Math.pow(ax - bx, 2) + Math.pow(ay - by, 2)) + Math.sqrt(Math.pow(bx - cx, 2) + Math.pow(by - cy, 2));
var steps = dist / minlength;
moveTo(ax, ay);
for (var i=0; i<steps; i++) {
var x = getstep(getstep(ax, bx, i, steps), getstep(bx, cx, i, steps), i, steps);
var y = getstep(getstep(ay, by, i, steps), getstep(by, cy, i, steps), i, steps);
lineTo(x, y);
}
lineTo(cx, cy);
}
function getstep(x1, x2, stepcount, numofsteps) {
return ((numofsteps - stepcount) * x1 + stepcount * x2) / numofsteps;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment