fix floating point issue in bezier
This commit is contained in:
@@ -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) {
|
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 = [];
|
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 ab = lerpPoint(a, b, p);
|
||||||
const bc = lerpPoint(b, c, p);
|
const bc = lerpPoint(b, c, p);
|
||||||
const abc = lerpPoint(ab, bc, 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) {
|
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 = [];
|
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 ab = lerpPoint(a, b, p);
|
||||||
const bc = lerpPoint(b, c, p);
|
const bc = lerpPoint(b, c, p);
|
||||||
const cd = lerpPoint(c, d, p);
|
const cd = lerpPoint(c, d, p);
|
||||||
|
|||||||
@@ -84,7 +84,7 @@ function update(ctx: CanvasRenderingContext2D, time: number) {
|
|||||||
if (lastTime == null) lastTime = time;
|
if (lastTime == null) lastTime = time;
|
||||||
const dt = (time - lastTime) / 1000; // s
|
const dt = (time - lastTime) / 1000; // s
|
||||||
lastTime = time;
|
lastTime = time;
|
||||||
apple = apple as Vec2d;
|
if (apple == null) return;
|
||||||
|
|
||||||
if (!paused) {
|
if (!paused) {
|
||||||
// update head position
|
// update head position
|
||||||
|
|||||||
Reference in New Issue
Block a user