Skip to content

Commit

Permalink
feat: merge duplicate codes
Browse files Browse the repository at this point in the history
  • Loading branch information
neuqzxy committed Jul 30, 2024
1 parent bdab99d commit 6af878b
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 40 deletions.
44 changes: 23 additions & 21 deletions packages/vrender-core/src/common/segment/catmull-rom-close.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
// }
66 changes: 47 additions & 19 deletions packages/vrender-core/src/common/segment/catmull-rom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down Expand Up @@ -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;
// }

0 comments on commit 6af878b

Please sign in to comment.