From 720f468f39ff2c51f40d8084e79313dac5e6fea8 Mon Sep 17 00:00:00 2001 From: Ryan Di Date: Wed, 24 Aug 2022 14:44:59 +0800 Subject: [PATCH] fix: improve solveQuadratic when a = 0 (#5618) --- src/element/bounds.ts | 34 +++++++++++++--------------------- 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/src/element/bounds.ts b/src/element/bounds.ts index 90d75eeb..b6f655ec 100644 --- a/src/element/bounds.ts +++ b/src/element/bounds.ts @@ -107,12 +107,19 @@ const solveQuadratic = ( return false; } - const t1 = (-b + Math.sqrt(sqrtPart)) / (2 * a); - const t2 = (-b - Math.sqrt(sqrtPart)) / (2 * a); - let s1 = null; let s2 = null; + let t1 = Infinity; + let t2 = Infinity; + + if (a === 0) { + t1 = t2 = -c / b; + } else { + t1 = (-b + Math.sqrt(sqrtPart)) / (2 * a); + t2 = (-b - Math.sqrt(sqrtPart)) / (2 * a); + } + if (t1 >= 0 && t1 <= 1) { s1 = getBezierValueForT(t1, p0, p1, p2, p3); } @@ -152,11 +159,6 @@ const getCubicBezierCurveBound = ( return [minX, minY, maxX, maxY]; }; -// TODO: https://github.com/excalidraw/excalidraw/issues/5617 -const getRandomOffset = () => { - return Math.random() / 1000000; -}; - const getMinMaxXYFromCurvePathOps = ( ops: Op[], transformXY?: (x: number, y: number) => [number, number], @@ -173,19 +175,9 @@ const getMinMaxXYFromCurvePathOps = ( // move operation does not draw anything; so, it always // returns false } else if (op === "bcurveTo") { - // random offset is needed to fix https://github.com/excalidraw/excalidraw/issues/5585 - const _p1 = [ - data[0] + getRandomOffset(), - data[1] + getRandomOffset(), - ] as Point; - const _p2 = [ - data[2] + getRandomOffset(), - data[3] + getRandomOffset(), - ] as Point; - const _p3 = [ - data[4] + getRandomOffset(), - data[5] + getRandomOffset(), - ] as Point; + const _p1 = [data[0], data[1]] as Point; + const _p2 = [data[2], data[3]] as Point; + const _p3 = [data[4], data[5]] as Point; const p1 = transformXY ? transformXY(..._p1) : _p1; const p2 = transformXY ? transformXY(..._p2) : _p2;