utils增加均分折线

This commit is contained in:
Yuan 2023-08-15 11:40:25 +08:00
parent 8af2d71795
commit 1e09b4c3c4

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