From aa87e3b77ebd62dd2fd853458655f2641827c3dd Mon Sep 17 00:00:00 2001 From: Atul Varma Date: Tue, 16 Feb 2021 07:04:16 -0500 Subject: [PATCH] Sort attachment points top to bottom, left to right. --- lib/specs.ts | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/lib/specs.ts b/lib/specs.ts index cdc2797..a70a6a1 100644 --- a/lib/specs.ts +++ b/lib/specs.ts @@ -89,8 +89,28 @@ function getBoundingBoxes(path: string): BBox[] { return bboxes; } -function concat(first: T[] | undefined, second: T[]): T[] { - return first ? [...first, ...second] : second; +/** + * Sort points from top to bottom, left to right. + */ +function sortPoints(a: PointWithNormal, b: PointWithNormal): number { + if (a.point.y < b.point.y) return -1; + if (a.point.y > b.point.y) return 1; + if (a.point.x < b.point.x) return -1; + if (a.point.x > b.point.x) return 1; + return 0; +} + +function sortedPoints(points: PointWithNormal[]): PointWithNormal[] { + const copy = [...points]; + copy.sort(sortPoints); + return copy; +} + +function sortedPointConcat( + first: PointWithNormal[] | undefined, + second: PointWithNormal[] +): PointWithNormal[] { + return first ? sortedPoints([...first, ...second]) : sortedPoints(second); } const ATTACHMENT_COLOR_MAP = new Map( @@ -106,14 +126,17 @@ function updateSpecs(fill: string, path: string, specs: Specs): Specs { if (attachmentType) { return { ...specs, - [attachmentType]: concat(specs[attachmentType], getArrowPoints(path)), + [attachmentType]: sortedPointConcat( + specs[attachmentType], + getArrowPoints(path) + ), }; } if (fill === colors.NESTING_BOUNDING_BOX_COLOR) { return { ...specs, - nesting: concat(specs.nesting, getBoundingBoxes(path)), + nesting: sortedPointConcat(specs.nesting, getBoundingBoxes(path)), }; }