Skip to content

Commit

Permalink
fix: update line2cubic algorithm #102 (#107)
Browse files Browse the repository at this point in the history
* fix: update line2cubic algorithm #102

* chore: bump version
  • Loading branch information
xiaoiver authored Jul 25, 2023
1 parent daca29f commit 30f95a1
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 46 deletions.
32 changes: 16 additions & 16 deletions __tests__/unit/path/path-2-curve.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
});
Expand All @@ -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],
]);
});

Expand Down Expand Up @@ -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,
Expand All @@ -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],
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@antv/util",
"version": "3.3.3",
"version": "3.3.4",
"license": "MIT",
"sideEffects": false,
"main": "lib/index.js",
Expand Down
3 changes: 1 addition & 2 deletions src/path/convert/path-2-absolute.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
3 changes: 1 addition & 2 deletions src/path/convert/path-2-curve.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions src/path/parser/parse-path-string.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);
Expand Down
23 changes: 4 additions & 19 deletions src/path/process/line-2-cubic.ts
Original file line number Diff line number Diff line change
@@ -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];
};
3 changes: 1 addition & 2 deletions src/path/process/normalize-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand All @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions src/path/process/round-path.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 30f95a1

Please sign in to comment.