diff --git a/src/jlgraphic/utils/GraphicUtils.ts b/src/jlgraphic/utils/GraphicUtils.ts index ededb4f..f73549a 100644 --- a/src/jlgraphic/utils/GraphicUtils.ts +++ b/src/jlgraphic/utils/GraphicUtils.ts @@ -695,6 +695,42 @@ export function splitLineEvenly( }); } +export function splitPolyline(points: IPointData[], count: number) { + if (points.length !== 2) { + let totalLen = 0; + const lengths: number[] = []; + for (let i = 1; i < points.length; i++) { + const { x: x1, y: y1 } = points[i - 1], + { x: x2, y: y2 } = points[i]; + const len = new Vector2([x2 - x1, y2 - y1]).length(); + totalLen += len; + lengths.push(len); + } + const counts = lengths.map((length) => + Math.round((count * length) / totalLen) + ); + if (counts.reduce((p, c) => p + c, 0) !== count) { + const intersection = counts.reduce((p, c) => p + c, 0) - count; + let maxCountIndex = 0, + maxCount = 0; + counts.forEach((c, i) => { + if (c > maxCount) { + maxCount = c; + maxCountIndex = i; + } + }); + counts[maxCountIndex] + intersection; + } + return counts + .map((count, i) => { + return splitLineEvenly(points[i], points[i + 1], count); + }) + .flat(); + } else { + return splitLineEvenly(points[0], points[points.length - 1], count); + } +} + export function getParallelOfPolyline( points: IPointData[], offset: number,