Last active
December 17, 2015 12:39
-
-
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.
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 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