Last active
February 15, 2023 02:31
-
-
Save qdsang/e9b0b3391886476131b7a29bcdea1b78 to your computer and use it in GitHub Desktop.
BezierCurve
This file contains hidden or 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 BezierCurve(p1x, p1y, p2x, p2y, t) { | |
let mt = 1 - t; | |
let mt2 = mt * mt, | |
t2 = t * t, | |
a, | |
b, | |
c, | |
d = 0; | |
let p0x = 0, p0y = 0; | |
let p3x = 100, p3y = 100; | |
a = mt2 * mt; | |
b = mt2 * t * 3; | |
c = mt * t2 * 3; | |
d = t * t2; | |
const ret = { | |
x: a * p0x + b * p1x + c * p2x + d * p3x, | |
y: a * p0y + b * p1y + c * p2y + d * p3y, | |
t: t, | |
}; | |
return ret; | |
} | |
// TEST | |
let t= 0.9; | |
let p1x = 30, p1y = 30, p2x = 90, p2y=90; | |
BezierCurve(p1x, p1y, p2x, p2y, t) | |
for (var i = 0; i < 100; i++) { | |
(function(i){ | |
setTimeout(function(){ | |
var x = BezierCurve(p1x, p1y, p2x, p2y, i/100); | |
console.log(x); | |
}, i * 50) | |
})(i); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment