This commit is contained in:
Yuan 2023-08-15 11:40:59 +08:00
parent e5f048bc9a
commit 31b48e9858
2 changed files with 37 additions and 1 deletions

@ -1 +1 @@
Subproject commit 8af2d71795ac49b3a8ec9ac516fd029569463452 Subproject commit 1e09b4c3c44f87bbf79e9cf325dc1eb10d0b5f06

View File

@ -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( export function getParallelOfPolyline(
points: IPointData[], points: IPointData[],
offset: number, offset: number,