From 30f95a10e7fb307ec9a8ed09680dfd94a832c400 Mon Sep 17 00:00:00 2001 From: xiaoiver Date: Tue, 25 Jul 2023 11:34:03 +0800 Subject: [PATCH] fix: update line2cubic algorithm #102 (#107) * fix: update line2cubic algorithm #102 * chore: bump version --- __tests__/unit/path/path-2-curve.spec.ts | 32 ++++++++++++------------ package.json | 2 +- src/path/convert/path-2-absolute.ts | 3 +-- src/path/convert/path-2-curve.ts | 3 +-- src/path/parser/parse-path-string.ts | 3 +-- src/path/process/line-2-cubic.ts | 23 +++-------------- src/path/process/normalize-path.ts | 3 +-- src/path/process/round-path.ts | 3 +-- 8 files changed, 26 insertions(+), 46 deletions(-) diff --git a/__tests__/unit/path/path-2-curve.spec.ts b/__tests__/unit/path/path-2-curve.spec.ts index 54ebf14..d105394 100644 --- a/__tests__/unit/path/path-2-curve.spec.ts +++ b/__tests__/unit/path/path-2-curve.spec.ts @@ -6,13 +6,13 @@ describe('test path to curve', () => { const [pathArray, zCommandIndexes] = path2Curve('M 10,10 L -10,0 L 10,-10 Z M 10,10 L -10,0 L 10,-10 Z', true); expect(pathArray).toEqual([ ['M', 10, 10], - ['C', 10, 10, -3.125, 3.4375, -10, 0], - ['C', -7.76393202250021, -1.118033988749895, 3.75, -6.875, 10, -10], - ['C', 10, 0, 10, 3.75, 10, 10], + ['C', 0, 5, -10, 0, -10, 0], + ['C', 0, -5, 10, -10, 10, -10], + ['C', 10, 0, 10, 10, 10, 10], ['M', 10, 10], - ['C', 10, 10, -3.125, 3.4375, -10, 0], - ['C', -7.76393202250021, -1.118033988749895, 3.75, -6.875, 10, -10], - ['C', 10, 0, 10, 3.75, 10, 10], + ['C', 0, 5, -10, 0, -10, 0], + ['C', 0, -5, 10, -10, 10, -10], + ['C', 10, 0, 10, 10, 10, 10], ]); expect(zCommandIndexes).toEqual([3, 7]); }); @@ -38,7 +38,7 @@ describe('test path to curve', () => { ]), ).toEqual([ ['M', 0, 0], - ['C', 44.194173824159215, 44.194173824159215, 68.75, 68.75, 100, 100], + ['C', 50, 50, 100, 100, 100, 100], ]); }); @@ -162,17 +162,17 @@ describe('test path to curve', () => { ).toEqual([ ['M', 2, 4], ['C', 0.8954305003384135, 4, -1.3527075021963556e-16, 4.8954305003384135, 0, 6], - ['C', 0, 6, 0, 9.9375, 0, 12], + ['C', 0, 9, 0, 12, 0, 12], ['C', 1.3527075021963556e-16, 13.104569499661586, 0.8954305003384133, 14, 2, 14], - ['C', 8, 14, 10.25, 14, 14, 14], + ['C', 8, 14, 14, 14, 14, 14], ['C', 15.104569499661586, 14, 16, 13.104569499661586, 16, 12], - ['C', 16, 9, 16, 7.875, 16, 6], + ['C', 16, 9, 16, 6, 16, 6], ['C', 16, 4.8954305003384135, 15.104569499661586, 4, 14, 4], - ['C', 13.414, 4, 13.194249999999998, 4, 12.828, 4], + ['C', 13.414, 4, 12.828, 4, 12.828, 4], ['C', 12.297611001580538, 3.999886244231009, 11.788985018308267, 3.7890983052869243, 11.414, 3.414], - ['C', 11, 3, 10.84475, 2.8447500000000003, 10.586, 2.5860000000000003], + ['C', 11, 3, 10.586, 2.5860000000000003, 10.586, 2.5860000000000003], ['C', 10.211014981691733, 2.2109016947130766, 9.702388998419462, 2.0001137557689916, 9.172, 2.0000000000000004], - ['C', 8, 2.0000000000000004, 7.560500000000001, 2.0000000000000004, 6.828000000000001, 2.0000000000000004], + ['C', 8, 2.0000000000000004, 6.828000000000001, 2.0000000000000004, 6.828000000000001, 2.0000000000000004], [ 'C', 6.29761100158054, @@ -182,10 +182,10 @@ describe('test path to curve', () => { 5.4140000000000015, 2.5860000000000003, ], - ['C', 5.000000000000002, 3, 4.844750000000001, 3.1552499999999997, 4.586000000000001, 3.414], + ['C', 5.000000000000002, 3, 4.586000000000001, 3.414, 4.586000000000001, 3.414], ['C', 4.211014981691734, 3.7890983052869243, 3.7023889984194627, 3.9998862442310084, 3.1720000000000015, 4], - ['C', 2.5860000000000016, 4, 2.3662500000000017, 4, 2.0000000000000018, 4], - ['C', 2.000000000000001, 4, 2.000000000000001, 4, 2, 4], + ['C', 2.5860000000000016, 4, 2.0000000000000018, 4, 2.0000000000000018, 4], + ['C', 2.000000000000001, 4, 2, 4, 2, 4], ['M', 10.5, 8.5], ['C', 10.5, 6.575499102701247, 8.416666666666666, 5.372686041889527, 6.75, 6.334936490538903], ['C', 5.976497308103742, 6.781518477924107, 5.5, 7.606836025229591, 5.5, 8.5], diff --git a/package.json b/package.json index 42fb7cd..931260b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@antv/util", - "version": "3.3.3", + "version": "3.3.4", "license": "MIT", "sideEffects": false, "main": "lib/index.js", diff --git a/src/path/convert/path-2-absolute.ts b/src/path/convert/path-2-absolute.ts index 4a02eec..1981990 100644 --- a/src/path/convert/path-2-absolute.ts +++ b/src/path/convert/path-2-absolute.ts @@ -1,11 +1,10 @@ -import { clonePath } from '../process/clone-path'; import { isAbsoluteArray } from '../util/is-absolute-array'; import { parsePathString } from '../parser/parse-path-string'; import type { PathArray, AbsoluteArray, AbsoluteSegment } from '../types'; export function path2Absolute(pathInput: string | PathArray): AbsoluteArray { if (isAbsoluteArray(pathInput)) { - return clonePath(pathInput) as AbsoluteArray; + return [].concat(pathInput) as AbsoluteArray; } const path = parsePathString(pathInput as PathArray) as PathArray; diff --git a/src/path/convert/path-2-curve.ts b/src/path/convert/path-2-curve.ts index 6d37223..30e5cf4 100644 --- a/src/path/convert/path-2-curve.ts +++ b/src/path/convert/path-2-curve.ts @@ -1,5 +1,4 @@ import { paramsParser } from '../parser/params-parser'; -import { clonePath } from '../process/clone-path'; import { fixArc } from '../process/fix-arc'; import { normalizePath } from '../process/normalize-path'; import { isCurveArray } from '../util/is-curve-array'; @@ -12,7 +11,7 @@ export function path2Curve( needZCommandIndexes = false, ): CurveArray | [CurveArray, number[]] { if (isCurveArray(pathInput)) { - const cloned = clonePath(pathInput) as CurveArray; + const cloned = [].concat(pathInput) as CurveArray; if (needZCommandIndexes) { return [cloned, []]; } else { diff --git a/src/path/parser/parse-path-string.ts b/src/path/parser/parse-path-string.ts index 4cf3ecd..ce4c467 100644 --- a/src/path/parser/parse-path-string.ts +++ b/src/path/parser/parse-path-string.ts @@ -1,4 +1,3 @@ -import { clonePath } from '../process/clone-path'; import { isPathArray } from '../util/is-path-array'; import type { PathArray } from '../types'; import { scanSegment } from './scan-segment'; @@ -11,7 +10,7 @@ import { PathParser } from './path-parser'; */ export function parsePathString(pathInput: PathArray | string): PathArray | string { if (isPathArray(pathInput)) { - return clonePath(pathInput) as PathArray; + return [].concat(pathInput) as PathArray; } const path = new PathParser(pathInput); diff --git a/src/path/process/line-2-cubic.ts b/src/path/process/line-2-cubic.ts index 3517408..f03fea3 100644 --- a/src/path/process/line-2-cubic.ts +++ b/src/path/process/line-2-cubic.ts @@ -1,22 +1,7 @@ -import { segmentLineFactory } from '../util/segment-line-factory'; import { midPoint } from '../util/mid-point'; -export function lineToCubic(x1: number, y1: number, x2: number, y2: number) { +export const lineToCubic = (x1: number, y1: number, x2: number, y2: number) => { const t = 0.5; - const p0 = [x1, y1]; - const p1 = [x2, y2]; - const p2 = midPoint(p0, p1, t); - const p3 = midPoint(p1, p2, t); - const p4 = midPoint(p2, p3, t); - const p5 = midPoint(p3, p4, t); - const p6 = midPoint(p4, p5, t); - - // const seg1 = [...p0, ...p2, ...p4, ...p6, t]; - // @ts-ignore - const cp1 = segmentLineFactory(p0[0], p0[1], p2[0], p2[1], p4[0]).point; - // const seg2 = [...p6, ...p5, ...p3, ...p1, 0]; - // @ts-ignore - const cp2 = segmentLineFactory(p6[0], p6[1], p5[0], p5[1], p3[0]).point; - - return [cp1.x, cp1.y, cp2.x, cp2.y, x2, y2]; -} + const mid = midPoint([x1, y1], [x2, y2], t); + return [...mid, x2, y2, x2, y2]; +}; diff --git a/src/path/process/normalize-path.ts b/src/path/process/normalize-path.ts index af5ab22..0c7402a 100644 --- a/src/path/process/normalize-path.ts +++ b/src/path/process/normalize-path.ts @@ -2,7 +2,6 @@ import { isNormalizedArray } from '../util/is-normalized-array'; import { paramsParser } from '../parser/params-parser'; import { path2Absolute } from '../convert/path-2-absolute'; import type { PathArray, NormalArray } from '../types'; -import { clonePath } from './clone-path'; import { normalizeSegment } from './normalize-segment'; /** @@ -13,7 +12,7 @@ import { normalizeSegment } from './normalize-segment'; */ export function normalizePath(pathInput: string | PathArray): NormalArray { if (isNormalizedArray(pathInput)) { - return clonePath(pathInput) as NormalArray; + return [].concat(pathInput) as NormalArray; } const path = path2Absolute(pathInput); diff --git a/src/path/process/round-path.ts b/src/path/process/round-path.ts index c56b89f..033605c 100644 --- a/src/path/process/round-path.ts +++ b/src/path/process/round-path.ts @@ -1,12 +1,11 @@ import type { PathArray } from '../types'; -import { clonePath } from './clone-path'; /** * Rounds the values of a `PathArray` instance to * a specified amount of decimals and returns it. */ export function roundPath(path: PathArray, round: number | 'off'): PathArray { - if (round === 'off') return clonePath(path) as PathArray; + if (round === 'off') return [].concat(path) as PathArray; // to round values to the power // the `round` value must be integer