From 3cb91d10adbc8e90f7ae1e2d58f1990c42d5db67 Mon Sep 17 00:00:00 2001 From: Yuan Date: Tue, 1 Aug 2023 15:45:19 +0800 Subject: [PATCH] =?UTF-8?q?utils=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/jlgraphic/utils/GraphicUtils.ts | 37 +++++++++++++---------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/src/jlgraphic/utils/GraphicUtils.ts b/src/jlgraphic/utils/GraphicUtils.ts index ffc9d9c..84a83fb 100644 --- a/src/jlgraphic/utils/GraphicUtils.ts +++ b/src/jlgraphic/utils/GraphicUtils.ts @@ -695,33 +695,30 @@ export function splitLineEvenly( }); } -/** 计算折线的平行线 */ -export function getParallelOfPolyline( +/** 计算直线的平行线 */ +export function getParallelOfLine( points: IPointData[], direction: 'L' | 'R', offset: number ) { - if (points.length < 2) throw Error('折线点不能少于2个'); + if (points.length !== 2) throw Error('直线点的个数需为2'); const normalVecs = points.map((p, i) => { - if (points[i - 1] && points[i + 1]) { + let point; + if (points[i + 1]) { + point = new Vector2([ + points[i + 1].x - p.x, + points[i + 1].y - p.y, + ]).normalize(); } else { - let point; - if (points[i + 1]) { - point = new Vector2([ - points[i + 1].x - p.x, - points[i + 1].y - p.y, - ]).normalize(); - } else { - point = new Vector2([ - p.x - points[i - 1].x, - p.y - points[i - 1].y, - ]).normalize(); - } - const rotate = new Matrix().rotate( - (direction === 'L' ? Math.PI : -Math.PI) / 2 - ); - return rotate.apply(point); + point = new Vector2([ + p.x - points[i - 1].x, + p.y - points[i - 1].y, + ]).normalize(); } + const rotate = new Matrix().rotate( + (direction === 'L' ? Math.PI : -Math.PI) / 2 + ); + return rotate.apply(point); }); return points.map((p, i) => ({ x: p.x + offset * normalVecs[i]?.x,