diff --git a/README.md b/README.md index d1fd4f5..42d5a6e 100644 --- a/README.md +++ b/README.md @@ -163,9 +163,21 @@ expect(point).toEqual({ x: 0, y: 0 }); 计算路径包围的面积。内部实现中首先通过 [path2Curve](#path2Curve) 转曲,再计算 cubic curve 面积,[详见](https://stackoverflow.com/a/15845996)。 +方法签名如下: + +```js +function getPathArea(path: PathArray): number; +``` + ### isPointInStroke -判断一个点是否在路径上,仅通过几何定义,不考虑其他样式属性例如线宽、lineJoin、miter 等。 +判断一个点是否在路径上,仅通过几何定义计算,不考虑其他样式属性例如线宽、lineJoin、miter 等。 + +方法签名如下: + +```js +isPointInStroke(pathInput: string | PathArray, point: Point): boolean; +``` ```js const result = isPointInStroke(segments, { x: 10, y: 10 }); diff --git a/__tests__/unit/path/path-2-curve.spec.ts b/__tests__/unit/path/path-2-curve.spec.ts index bebf1d5..54ebf14 100644 --- a/__tests__/unit/path/path-2-curve.spec.ts +++ b/__tests__/unit/path/path-2-curve.spec.ts @@ -161,29 +161,29 @@ describe('test path to curve', () => { ), ).toEqual([ ['M', 2, 4], - ['C', 0.8954304997175604, 3.9999999991219815, -1.3527075029566811e-16, 4.895430499717561, 0, 6], + ['C', 0.8954305003384135, 4, -1.3527075021963556e-16, 4.8954305003384135, 0, 6], ['C', 0, 6, 0, 9.9375, 0, 12], - ['C', 1.3527075029566811e-16, 13.10456950028244, 0.8954304997175604, 14.00000000087802, 2, 14], + ['C', 1.3527075021963556e-16, 13.104569499661586, 0.8954305003384133, 14, 2, 14], ['C', 8, 14, 10.25, 14, 14, 14], - ['C', 15.104569499040734, 13.99999999912198, 16, 13.104569499040734, 16, 12], + ['C', 15.104569499661586, 14, 16, 13.104569499661586, 16, 12], ['C', 16, 9, 16, 7.875, 16, 6], - ['C', 16, 4.895430500959266, 15.104569499040734, 4.0000000008780185, 14, 4], + ['C', 16, 4.8954305003384135, 15.104569499661586, 4, 14, 4], ['C', 13.414, 4, 13.194249999999998, 4, 12.828, 4], - ['C', 12.297610373455704, 3.9998867247945213, 11.788985462367364, 3.7890987493850155, 11.414, 3.414], + ['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', 10.211014537632636, 2.210901250614985, 9.702389626544296, 2.0001132752054787, 9.172, 2.0000000000000004], + ['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', - 6.297610373455706, - 2.0001132752054787, - 5.788985462367367, - 2.210901250614985, + 6.29761100158054, + 2.000113755768992, + 5.788985018308269, + 2.210901694713076, 5.4140000000000015, 2.5860000000000003, ], ['C', 5.000000000000002, 3, 4.844750000000001, 3.1552499999999997, 4.586000000000001, 3.414], - ['C', 4.211014537632636, 3.7890987493850155, 3.7023896265442966, 3.9998867247945213, 3.1720000000000015, 4], + ['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], ['M', 10.5, 8.5], @@ -193,26 +193,10 @@ describe('test path to curve', () => { ['C', 10.023502691896258, 10.218481522075892, 10.5, 9.39316397477041, 10.5, 8.5], ['C', 10.5, 8.5, 10.5, 8.5, 10.5, 8.5], ['M', 2.5, 6], - [ - 'C', - 2.1150998205402494, - 6.000000000305956, - 1.874537208444147, - 5.583333333830511, - 2.0669872979090567, - 5.2500000003442, - ], - ['C', 2.1563036954051213, 5.095299461648009, 2.321367204761929, 4.999999999858005, 2.5, 5], - [ - 'C', - 2.8849001794597506, - 5.000000000305956, - 3.125462791688336, - 5.416666667163845, - 2.933012701693495, - 5.7500000003442, - ], - ['C', 2.8436963042354777, 5.904700538406512, 2.6786327946700927, 5.999999999858005, 2.5, 6], + ['C', 2.1150998205402494, 6, 1.8745372083779053, 5.583333333333333, 2.0669872981077804, 5.25], + ['C', 2.156303695584821, 5.0952994616207485, 2.321367205045918, 5, 2.5, 5], + ['C', 2.8849001794597506, 5, 3.1254627916220943, 5.416666666666667, 2.933012701892219, 5.75], + ['C', 2.8436963044151784, 5.9047005383792515, 2.678632794954082, 6, 2.5, 6], ['C', 2.5, 6, 2.5, 6, 2.5, 6], ['M', 11.5, 8.5], ['C', 11.5, 11.194301256218253, 8.583333333333334, 12.878239541354663, 6.250000000000001, 11.531088913245537], diff --git a/package.json b/package.json index 3033ad0..441689e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@antv/util", - "version": "3.1.1", + "version": "3.1.2", "license": "MIT", "sideEffects": false, "main": "lib/index.js", diff --git a/src/path/process/arc-2-cubic.ts b/src/path/process/arc-2-cubic.ts index 3f54bb3..3379d34 100644 --- a/src/path/process/arc-2-cubic.ts +++ b/src/path/process/arc-2-cubic.ts @@ -63,9 +63,9 @@ export function arcToCubic( cx = (k * rx * y) / ry + (x1 + x2) / 2; cy = (k * -ry * x) / rx + (y1 + y2) / 2; // eslint-disable-next-line no-bitwise -- Impossible to satisfy no-bitwise - f1 = ((Math.asin((y1 - cy) / ry) * 10 ** 9) >> 0) / 10 ** 9; + f1 = Math.asin(((((y1 - cy) / ry) * 10 ** 9) >> 0) / 10 ** 9); // eslint-disable-next-line no-bitwise -- Impossible to satisfy no-bitwise - f2 = ((Math.asin((y2 - cy) / ry) * 10 ** 9) >> 0) / 10 ** 9; + f2 = Math.asin(((((y2 - cy) / ry) * 10 ** 9) >> 0) / 10 ** 9); f1 = x1 < cx ? Math.PI - f1 : f1; f2 = x2 < cx ? Math.PI - f2 : f2; diff --git a/src/path/util/segment-arc-factory.ts b/src/path/util/segment-arc-factory.ts index 56bca95..215f021 100644 --- a/src/path/util/segment-arc-factory.ts +++ b/src/path/util/segment-arc-factory.ts @@ -143,7 +143,7 @@ export function segmentArcFactory( let POINT = { x: 0, y: 0 }; let POINTS = [{ x, y }]; - if (distanceIsNumber && distance === 0) { + if (distanceIsNumber && distance <= 0) { POINT = { x, y }; } diff --git a/src/path/util/segment-cubic-factory.ts b/src/path/util/segment-cubic-factory.ts index e6f0f9b..10bc4d3 100644 --- a/src/path/util/segment-cubic-factory.ts +++ b/src/path/util/segment-cubic-factory.ts @@ -48,7 +48,7 @@ export function segmentCubicFactory( let POINT = { x: 0, y: 0 }; let POINTS = [{ x, y }]; - if (distanceIsNumber && distance === 0) { + if (distanceIsNumber && distance <= 0) { POINT = { x, y }; } diff --git a/src/path/util/segment-quad-factory.ts b/src/path/util/segment-quad-factory.ts index 0a5cf7a..dd5e54b 100644 --- a/src/path/util/segment-quad-factory.ts +++ b/src/path/util/segment-quad-factory.ts @@ -46,7 +46,7 @@ export function segmentQuadFactory( let POINT = { x: 0, y: 0 }; let POINTS = [{ x, y }]; - if (distanceIsNumber && distance === 0) { + if (distanceIsNumber && distance <= 0) { POINT = { x, y }; }