split linear segments as curves

This commit is contained in:
Preet 2023-10-23 21:29:53 -07:00
parent bf7c91536f
commit 7f5b7bab69

View File

@ -235,27 +235,38 @@ export const _generateElementShape = (
// curve is always the first element // curve is always the first element
// this simplifies finding the curve for an element // this simplifies finding the curve for an element
const splits = element.segmentSplitIndices || [];
if (!element.roundness) { if (!element.roundness) {
if (splits.length === 0) {
if (options.fill) { if (options.fill) {
shape = [generator.polygon(points as [number, number][], options)]; shape = [generator.polygon(points as [number, number][], options)];
} else { } else {
shape = [generator.linearPath(points as [number, number][], options)]; shape = [
generator.linearPath(points as [number, number][], options),
];
} }
} else { } else {
const pointList: Point[][] = []; const splitInverse: number[] = [];
const splits = element.segmentSplitIndices || []; const splitSet = new Set(splits);
let currentIndex = 0; for (let i = 0; i < points.length; i++) {
for (const index of splits) { if (!splitSet.has(i)) {
const slice = points.slice(currentIndex, index + 1); splitInverse.push(i);
if (slice.length) {
pointList.push([...slice]);
} }
currentIndex = index;
} }
if (currentIndex < points.length - 1) { shape = [
pointList.push(points.slice(currentIndex)); generator.curve(
computeMultipleCurvesFromSplits(points, splitInverse),
options,
),
];
} }
shape = [generator.curve(pointList as [number, number][][], options)]; } else {
shape = [
generator.curve(
computeMultipleCurvesFromSplits(points, splits),
options,
),
];
} }
// add lines only in arrow // add lines only in arrow
@ -392,3 +403,22 @@ export const _generateElementShape = (
} }
} }
}; };
const computeMultipleCurvesFromSplits = (
points: readonly Point[],
splits: readonly number[],
): [number, number][][] => {
const pointList: Point[][] = [];
let currentIndex = 0;
for (const index of splits) {
const slice = points.slice(currentIndex, index + 1);
if (slice.length) {
pointList.push([...slice]);
}
currentIndex = index;
}
if (currentIndex < points.length - 1) {
pointList.push(points.slice(currentIndex));
}
return pointList as [number, number][][];
};