fix floating point issue in bezier

This commit is contained in:
2026-09-03 23:36:47 +02:00
parent f1205e3851
commit a04518fb0d
2 changed files with 18 additions and 16 deletions
+17 -15
View File
@@ -25,28 +25,30 @@ export function lerp(a: number, b: number, t: number) {
}
export function quadraticBezier(a: Point, b: Point, c: Point, res=0.05) {
const eps = 0.001; // to prevent issues with float comparaison (p <= 1)
const steps = Math.max(1, Math.ceil(1 / res))
const curve = [];
for (let p = 0; p - 1 < eps; p += res) {
const ab = lerpPoint(a, b, p);
const bc = lerpPoint(b, c, p);
const abc = lerpPoint(ab, bc, p);
curve.push(abc);
for (let i = 0; i <= steps; i++) {
const p = i / steps;
const ab = lerpPoint(a, b, p);
const bc = lerpPoint(b, c, p);
const abc = lerpPoint(ab, bc, p);
curve.push(abc);
}
return curve;
}
export function cubicBezier(a: Point, b: Point, c: Point, d: Point, res=0.05) {
const eps = 0.001; // to prevent issues with float comparaison (p <= 1)
const steps = Math.max(1, Math.ceil(1 / res))
const curve = [];
for (let p = 0; p - 1 < eps; p += res) {
const ab = lerpPoint(a, b, p);
const bc = lerpPoint(b, c, p);
const cd = lerpPoint(c, d, p);
const abc = lerpPoint(ab, bc, p);
const bcd = lerpPoint(bc, cd, p);
const abcd = lerpPoint(abc, bcd, p);
curve.push(abcd);
for (let i = 0; i <= steps; i++) {
const p = i / steps;
const ab = lerpPoint(a, b, p);
const bc = lerpPoint(b, c, p);
const cd = lerpPoint(c, d, p);
const abc = lerpPoint(ab, bc, p);
const bcd = lerpPoint(bc, cd, p);
const abcd = lerpPoint(abc, bcd, p);
curve.push(abcd);
}
return curve;
}