From 6af878b6d66a8eca5b99b27790e5179e228769b4 Mon Sep 17 00:00:00 2001 From: zhouxinyu Date: Tue, 30 Jul 2024 11:04:49 +0800 Subject: [PATCH] feat: merge duplicate codes --- .../src/common/segment/catmull-rom-close.ts | 44 +++++++------ .../src/common/segment/catmull-rom.ts | 66 +++++++++++++------ 2 files changed, 70 insertions(+), 40 deletions(-) diff --git a/packages/vrender-core/src/common/segment/catmull-rom-close.ts b/packages/vrender-core/src/common/segment/catmull-rom-close.ts index 7987d3878..60d617845 100644 --- a/packages/vrender-core/src/common/segment/catmull-rom-close.ts +++ b/packages/vrender-core/src/common/segment/catmull-rom-close.ts @@ -2,7 +2,7 @@ import { epsilon, type IPointLike } from '@visactor/vutils'; import { genLinearSegments } from './linear'; import { genCurveSegments, genSegContext } from './common'; import type { ICurvedSegment, IGenSegmentParams, ILinearSegment, ISegPath2D } from '../../interface/curve'; -import { point } from './catmull-rom'; +import { commonGenCatmullRomSegments, point } from './catmull-rom'; export class CatmullRomClosed implements ICurvedSegment { private _lastDefined1?: boolean; @@ -132,28 +132,30 @@ export class CatmullRomClosed implements ICurvedSegment { } } -export function genCatmullRomClosedTypeSegments(path: ILinearSegment, points: IPointLike[]): void { - return genCurveSegments(path, points, 2); -} +// export function genCatmullRomClosedTypeSegments(path: ILinearSegment, points: IPointLike[]): void { +// return genCurveSegments(path, points, 2); +// } -export function genCatmullRomClosedSegments( - points: IPointLike[], - alpha: number, - params: IGenSegmentParams = {} -): ISegPath2D | null { - const { direction, startPoint } = params; - if (points.length < 2 - Number(!!startPoint)) { - return null; - } - if (points.length < 3 - Number(!!startPoint)) { - return genLinearSegments(points, params); - } +export const genCatmullRomClosedSegments = commonGenCatmullRomSegments('catmullRomClosed', CatmullRomClosed); - const segContext = genSegContext('catmullRom', direction, points); +// export function genCatmullRomClosedSegments( +// points: IPointLike[], +// alpha: number, +// params: IGenSegmentParams = {} +// ): ISegPath2D | null { +// const { direction, startPoint } = params; +// if (points.length < 2 - Number(!!startPoint)) { +// return null; +// } +// if (points.length < 3 - Number(!!startPoint)) { +// return genLinearSegments(points, params); +// } - const gatmullRom = new CatmullRomClosed(segContext, alpha, startPoint); +// const segContext = genSegContext('catmullRom', direction, points); - genCatmullRomClosedTypeSegments(gatmullRom, points); +// const gatmullRom = new CatmullRomClosed(segContext, alpha, startPoint); - return segContext; -} +// genCatmullRomClosedTypeSegments(gatmullRom, points); + +// return segContext; +// } diff --git a/packages/vrender-core/src/common/segment/catmull-rom.ts b/packages/vrender-core/src/common/segment/catmull-rom.ts index 1430c633a..905528b0b 100644 --- a/packages/vrender-core/src/common/segment/catmull-rom.ts +++ b/packages/vrender-core/src/common/segment/catmull-rom.ts @@ -2,6 +2,7 @@ import { epsilon, type IPointLike } from '@visactor/vutils'; import { genLinearSegments } from './linear'; import { genCurveSegments, genSegContext } from './common'; import type { ICurvedSegment, IGenSegmentParams, ILinearSegment, ISegPath2D } from '../../interface/curve'; +import type { ICurveType } from '../../interface'; /** * 部分源码参考 https://github.com/d3/d3-shape/ @@ -148,28 +149,55 @@ export class CatmullRom implements ICurvedSegment { } } -export function genCatmullRomTypeSegments(path: ILinearSegment, points: IPointLike[]): void { - return genCurveSegments(path, points, 2); +// export function genCatmullRomTypeSegments(path: ILinearSegment, points: IPointLike[]): void { +// return genCurveSegments(path, points, 2); +// } + +export function commonGenCatmullRomSegments(type: ICurveType, cons: any) { + return function genCatmullRomSegments( + points: IPointLike[], + alpha: number, + params: IGenSegmentParams = {} + ): ISegPath2D | null { + const { direction, startPoint } = params; + if (points.length < 2 - Number(!!startPoint)) { + return null; + } + if (points.length < 3 - Number(!!startPoint)) { + return genLinearSegments(points, params); + } + + const segContext = genSegContext(type, direction, points); + + const gatmullRom = new cons(segContext, alpha, startPoint); + + genCurveSegments(gatmullRom, points, 2); + // genCatmullRomTypeSegments(gatmullRom, points); + + return segContext; + }; } -export function genCatmullRomSegments( - points: IPointLike[], - alpha: number, - params: IGenSegmentParams = {} -): ISegPath2D | null { - const { direction, startPoint } = params; - if (points.length < 2 - Number(!!startPoint)) { - return null; - } - if (points.length < 3 - Number(!!startPoint)) { - return genLinearSegments(points, params); - } +export const genCatmullRomSegments = commonGenCatmullRomSegments('catmullRom', CatmullRom); - const segContext = genSegContext('catmullRom', direction, points); +// export function genCatmullRomSegments( +// points: IPointLike[], +// alpha: number, +// params: IGenSegmentParams = {} +// ): ISegPath2D | null { +// const { direction, startPoint } = params; +// if (points.length < 2 - Number(!!startPoint)) { +// return null; +// } +// if (points.length < 3 - Number(!!startPoint)) { +// return genLinearSegments(points, params); +// } - const gatmullRom = new CatmullRom(segContext, alpha, startPoint); +// const segContext = genSegContext('catmullRom', direction, points); - genCatmullRomTypeSegments(gatmullRom, points); +// const gatmullRom = new CatmullRom(segContext, alpha, startPoint); - return segContext; -} +// genCatmullRomTypeSegments(gatmullRom, points); + +// return segContext; +// }