utils增加计算折线的平行线
This commit is contained in:
parent
3cb91d10ad
commit
809876ea5d
@ -695,33 +695,43 @@ export function splitLineEvenly(
|
||||
});
|
||||
}
|
||||
|
||||
/** 计算直线的平行线 */
|
||||
export function getParallelOfLine(
|
||||
export function getParallelOfPolyline(
|
||||
points: IPointData[],
|
||||
direction: 'L' | 'R',
|
||||
offset: number
|
||||
) {
|
||||
if (points.length !== 2) throw Error('直线点的个数需为2');
|
||||
const normalVecs = points.map((p, i) => {
|
||||
let point;
|
||||
if (points[i + 1]) {
|
||||
point = new Vector2([
|
||||
offset: number,
|
||||
side: 'L' | 'R'
|
||||
): IPointData[] {
|
||||
const { PI, cos, acos } = Math;
|
||||
const angleBase = side === 'L' ? -PI / 2 : PI / 2;
|
||||
return points.map((p, i) => {
|
||||
let baseUnitVec: Vector2; //上一段的基准单位向量
|
||||
let angle: number; //偏转角度
|
||||
let len: number; //结合偏转角度的实际偏移量
|
||||
if (!points[i - 1] || !points[i + 1]) {
|
||||
angle = angleBase;
|
||||
len = offset;
|
||||
baseUnitVec = points[i - 1]
|
||||
? new Vector2([
|
||||
p.x - points[i - 1].x,
|
||||
p.y - points[i - 1].y,
|
||||
]).normalize()
|
||||
: 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 vp = new Vector2([p.x - points[i - 1].x, p.y - points[i - 1].y]);
|
||||
const vn = new Vector2([points[i + 1].x - p.x, points[i + 1].y - p.y]);
|
||||
const cosTheta = Vector2.dot(vn, vp) / (vp.length() * vn.length());
|
||||
const direction = vp.x * vn.y - vp.y * vn.x > 0; //det(vp|vn)>0?
|
||||
const theta = direction ? acos(cosTheta) : -acos(cosTheta);
|
||||
angle = angleBase + theta / 2;
|
||||
len = offset / cos(theta / 2);
|
||||
baseUnitVec = Vector2.from(vp).normalize();
|
||||
}
|
||||
const rotate = new Matrix().rotate(
|
||||
(direction === 'L' ? Math.PI : -Math.PI) / 2
|
||||
);
|
||||
return rotate.apply(point);
|
||||
return new Matrix()
|
||||
.scale(len, len)
|
||||
.rotate(angle)
|
||||
.translate(p.x, p.y)
|
||||
.apply(baseUnitVec);
|
||||
});
|
||||
return points.map((p, i) => ({
|
||||
x: p.x + offset * normalVecs[i]?.x,
|
||||
y: p.y + offset * normalVecs[i]?.y,
|
||||
}));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user