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
+6 -4
View File
@@ -25,9 +25,10 @@ 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) {
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);
@@ -37,9 +38,10 @@ export function quadraticBezier(a: Point, b: Point, c: Point, res=0.05) {
}
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) {
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);
+1 -1
View File
@@ -84,7 +84,7 @@ function update(ctx: CanvasRenderingContext2D, time: number) {
if (lastTime == null) lastTime = time;
const dt = (time - lastTime) / 1000; // s
lastTime = time;
apple = apple as Vec2d;
if (apple == null) return;
if (!paused) {
// update head position