wip-计算折线平行线

This commit is contained in:
Yuan 2023-07-31 16:31:46 +08:00
parent 91eb4a1d08
commit 6cb5d39e43

View File

@ -2,6 +2,7 @@ import {
Container, Container,
DisplayObject, DisplayObject,
IPointData, IPointData,
Matrix,
Point, Point,
Rectangle, Rectangle,
} from 'pixi.js'; } from 'pixi.js';
@ -693,3 +694,37 @@ export function splitLineEvenly(
]; ];
}); });
} }
/** 计算折线的平行线 */
export function getParallelOfPolyline(
points: IPointData[],
direction: 'L' | 'R',
offset: number
) {
if (points.length < 2) throw Error('折线点不能少于2个');
const normalVecs = points.map((p, i) => {
if (points[i - 1] && points[i + 1]) {
} 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);
}
});
return points.map((p, i) => ({
x: p.x + offset * normalVecs[i]?.x,
y: p.y + offset * normalVecs[i]?.y,
}));
}