工具方法提出

This commit is contained in:
fan 2023-06-16 10:45:58 +08:00
parent 41c562451a
commit b401a1ffcd

View File

@ -1,9 +1,11 @@
import { Graphics, IPointData, LINE_JOIN, Point } from 'pixi.js';
import {
GraphicData,
JlDrawApp,
JlGraphic,
JlGraphicTemplate,
getNormalVector,
movePointAlongNormal,
getIntersectionPoint,
} from 'src/jl-graphic';
import { RunLineName } from './RunLineName';
import { PathLine } from '../pathLine/PathLine';
@ -95,61 +97,6 @@ export class RunLine extends JlGraphic {
old.points = points;
this.updateData(old);
}
/**
*
* @param point1
* @param point2
* @returns
*/
getNormalVector(point1: Point, point2: Point): number[] {
const x1 = point1.x,
y1 = point1.y;
const x2 = point2.x,
y2 = point2.y;
const length = Math.sqrt((y2 - y1) ** 2 + (x2 - x1) ** 2);
return [(y2 - y1) / length, (x1 - x2) / length];
}
/**
*
* @param point
* @param normal
* @param length
* @returns
*/
movePointAlongNormal(point: Point, normal: number[], length: number): Point {
const newPoint = new Point(
point.x + length * normal[0],
point.y + length * normal[1]
);
return newPoint;
}
/**
* 线(线 )
* @param line1
* @param line2
* @returns
*/
getIntersectionPoint(line1: number[], line2: number[]) {
const a1 = line1[0],
b1 = line1[1];
const a2 = line1[2],
b2 = line1[3];
const a3 = line2[0],
b3 = line2[1];
const a4 = line2[2],
b4 = line2[3];
const denominator = (a3 - a4) * (b1 - b2) - (a1 - a2) * (b3 - b4);
if (denominator === 0) {
return new Point(a1, b1);
}
const x =
((a3 - a4) * (a2 * b1 - a1 * b2) - (a1 - a2) * (a4 * b3 - a3 * b4)) /
denominator;
const y =
((b3 - b4) * (b2 * a1 - b1 * a2) - (b1 - b2) * (b4 * a3 - b3 * a4)) /
-denominator;
return new Point(x, y);
}
generatePathLine(points: Point[]) {
const pointsUp: Point[] = [];
@ -157,94 +104,86 @@ export class RunLine extends JlGraphic {
points.forEach((p, index) => {
// 起始点终止点计算两点法向量 做平移计算,中间点做线段法向量平移就交点
if (index === 0) {
const normalVector = this.getNormalVector(p, points[index + 1]);
const normalVector = getNormalVector(p, points[index + 1]);
const resverNormalVector = [-normalVector[0], -normalVector[1]];
pointsUp.push(
this.movePointAlongNormal(
p,
normalVector,
runLineConsts.pathLineDistance
)
movePointAlongNormal(p, normalVector, runLineConsts.pathLineDistance)
);
pointsDown.push(
this.movePointAlongNormal(
movePointAlongNormal(
p,
resverNormalVector,
runLineConsts.pathLineDistance
)
);
} else if (index === points.length - 1) {
const normalVector = this.getNormalVector(points[index - 1], p);
const normalVector = getNormalVector(points[index - 1], p);
const resverNormalVector = [-normalVector[0], -normalVector[1]];
pointsUp.push(
this.movePointAlongNormal(
p,
normalVector,
runLineConsts.pathLineDistance
)
movePointAlongNormal(p, normalVector, runLineConsts.pathLineDistance)
);
pointsDown.push(
this.movePointAlongNormal(
movePointAlongNormal(
p,
resverNormalVector,
runLineConsts.pathLineDistance
)
);
} else {
const normalVector1 = this.getNormalVector(p, points[index + 1]);
const normalVector1 = getNormalVector(p, points[index + 1]);
const resverNormalVector1 = [-normalVector1[0], -normalVector1[1]];
const curP1 = this.movePointAlongNormal(
const curP1 = movePointAlongNormal(
p,
normalVector1,
runLineConsts.pathLineDistance
);
const nextP1 = this.movePointAlongNormal(
const nextP1 = movePointAlongNormal(
points[index + 1],
normalVector1,
runLineConsts.pathLineDistance
);
const resverCurP1 = this.movePointAlongNormal(
const resverCurP1 = movePointAlongNormal(
p,
resverNormalVector1,
runLineConsts.pathLineDistance
);
const resverNextP1 = this.movePointAlongNormal(
const resverNextP1 = movePointAlongNormal(
points[index + 1],
resverNormalVector1,
runLineConsts.pathLineDistance
);
const normalVector2 = this.getNormalVector(points[index - 1], p);
const normalVector2 = getNormalVector(points[index - 1], p);
const resverNormalVector2 = [-normalVector2[0], -normalVector2[1]];
const curP2 = this.movePointAlongNormal(
const curP2 = movePointAlongNormal(
p,
normalVector2,
runLineConsts.pathLineDistance
);
const nextP2 = this.movePointAlongNormal(
const nextP2 = movePointAlongNormal(
points[index - 1],
normalVector2,
runLineConsts.pathLineDistance
);
const resverCurP2 = this.movePointAlongNormal(
const resverCurP2 = movePointAlongNormal(
p,
resverNormalVector2,
runLineConsts.pathLineDistance
);
const resverNextP2 = this.movePointAlongNormal(
const resverNextP2 = movePointAlongNormal(
points[index - 1],
resverNormalVector2,
runLineConsts.pathLineDistance
);
pointsUp.push(
this.getIntersectionPoint(
getIntersectionPoint(
[curP1.x, curP1.y, nextP1.x, nextP1.y],
[curP2.x, curP2.y, nextP2.x, nextP2.y]
)
);
pointsDown.push(
this.getIntersectionPoint(
getIntersectionPoint(
[resverCurP1.x, resverCurP1.y, resverNextP1.x, resverNextP1.y],
[resverCurP2.x, resverCurP2.y, resverNextP2.x, resverNextP2.y]
)