diff --git a/lib/solvers/TraceCleanupSolver/simplifyPath.ts b/lib/solvers/TraceCleanupSolver/simplifyPath.ts index e17bfb52..647cc24b 100644 --- a/lib/solvers/TraceCleanupSolver/simplifyPath.ts +++ b/lib/solvers/TraceCleanupSolver/simplifyPath.ts @@ -1,41 +1,30 @@ import type { Point } from "graphics-debug" -import { - isHorizontal, - isVertical, -} from "lib/solvers/SchematicTraceLinesSolver/SchematicTraceSingleLineSolver2/collisions" + +const isCollinear = (a: Point, b: Point, c: Point): boolean => { + const area = Math.abs( + a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y), + ) + return area < 0.1 +} export const simplifyPath = (path: Point[]): Point[] => { if (path.length < 3) return path - const newPath: Point[] = [path[0]] + + const finalPath: Point[] = [path[0]] + for (let i = 1; i < path.length - 1; i++) { - const p1 = newPath[newPath.length - 1] + const p1 = finalPath[finalPath.length - 1] const p2 = path[i] const p3 = path[i + 1] - if ( - (isVertical(p1, p2) && isVertical(p2, p3)) || - (isHorizontal(p1, p2) && isHorizontal(p2, p3)) - ) { - continue - } - newPath.push(p2) - } - newPath.push(path[path.length - 1]) - if (newPath.length < 3) return newPath - const finalPath: Point[] = [newPath[0]] - for (let i = 1; i < newPath.length - 1; i++) { - const p1 = finalPath[finalPath.length - 1] - const p2 = newPath[i] - const p3 = newPath[i + 1] - if ( - (isVertical(p1, p2) && isVertical(p2, p3)) || - (isHorizontal(p1, p2) && isHorizontal(p2, p3)) - ) { + if (isCollinear(p1, p2, p3)) { continue } + finalPath.push(p2) } - finalPath.push(newPath[newPath.length - 1]) + + finalPath.push(path[path.length - 1]) return finalPath } diff --git a/tests/examples/__snapshots__/example01.snap.svg b/tests/examples/__snapshots__/example01.snap.svg index 293bf05a..4d6fea96 100644 --- a/tests/examples/__snapshots__/example01.snap.svg +++ b/tests/examples/__snapshots__/example01.snap.svg @@ -2,97 +2,79 @@ +x-" data-x="-0.8" data-y="0.2" cx="422.5742574257426" cy="303.36633663366337" r="3" fill="hsl(319, 100%, 50%, 0.8)" /> +x-" data-x="-0.8" data-y="0" cx="422.5742574257426" cy="325.54455445544556" r="3" fill="hsl(320, 100%, 50%, 0.8)" /> +x-" data-x="-0.8" data-y="-0.2" cx="422.5742574257426" cy="347.72277227722776" r="3" fill="hsl(321, 100%, 50%, 0.8)" /> +x+" data-x="0.8" data-y="-0.2" cx="600" cy="347.72277227722776" r="3" fill="hsl(322, 100%, 50%, 0.8)" /> +x+" data-x="0.8" data-y="0" cx="600" cy="325.54455445544556" r="3" fill="hsl(323, 100%, 50%, 0.8)" /> +x+" data-x="0.8" data-y="0.2" cx="600" cy="303.36633663366337" r="3" fill="hsl(324, 100%, 50%, 0.8)" /> +y+" data-x="-2" data-y="0.5" cx="289.50495049504957" cy="270.0990099009901" r="3" fill="hsl(121, 100%, 50%, 0.8)" /> +y-" data-x="-2" data-y="-0.5" cx="289.50495049504957" cy="380.99009900990103" r="3" fill="hsl(122, 100%, 50%, 0.8)" /> +y+" data-x="-4" data-y="0.5" cx="67.72277227722776" cy="270.0990099009901" r="3" fill="hsl(2, 100%, 50%, 0.8)" /> +y-" data-x="-4" data-y="-0.5" cx="67.72277227722776" cy="380.99009900990103" r="3" fill="hsl(3, 100%, 50%, 0.8)" /> - + - + - + - + - + - + - + - + - + - + - + - - - - - - - - - - - - - - - - - - - +