From b401a1ffcdfb7949263602326ad76a90252b2039 Mon Sep 17 00:00:00 2001 From: fan Date: Fri, 16 Jun 2023 10:45:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B7=A5=E5=85=B7=E6=96=B9=E6=B3=95=E6=8F=90?= =?UTF-8?q?=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/graphics/runLine/RunLine.ts | 103 +++++++------------------------- 1 file changed, 21 insertions(+), 82 deletions(-) diff --git a/src/graphics/runLine/RunLine.ts b/src/graphics/runLine/RunLine.ts index 98ca1b9..28fcbe6 100644 --- a/src/graphics/runLine/RunLine.ts +++ b/src/graphics/runLine/RunLine.ts @@ -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] )