svg-path-commander
Version:
🛹 Modern TypeScript tools for SVG
1 lines • 223 kB
Source Map (JSON)
{"version":3,"sources":["../src/main.ts","../src/math/midPoint.ts","../src/math/distanceSquareRoot.ts","../src/math/lineTools.ts","../src/math/arcTools.ts","../src/math/bezier.ts","../src/math/cubicTools.ts","../src/math/quadTools.ts","../src/math/polygonTools.ts","../src/math/rotateVector.ts","../src/math/roundTo.ts","../src/options/options.ts","../src/parser/paramsCount.ts","../src/parser/finalizeSegment.ts","../src/parser/error.ts","../src/parser/scanFlag.ts","../src/parser/isDigit.ts","../src/parser/invalidPathValue.ts","../src/parser/scanParam.ts","../src/parser/isSpace.ts","../src/parser/skipSpaces.ts","../src/parser/isPathCommand.ts","../src/parser/isDigitStart.ts","../src/parser/isArcCommand.ts","../src/parser/isMoveCommand.ts","../src/parser/scanSegment.ts","../src/parser/pathParser.ts","../src/parser/parsePathString.ts","../src/process/absolutizeSegment.ts","../src/process/iterate.ts","../src/convert/pathToAbsolute.ts","../src/process/relativizeSegment.ts","../src/convert/pathToRelative.ts","../src/process/arcToCubic.ts","../src/process/quadToCubic.ts","../src/process/lineToCubic.ts","../src/process/segmentToCubic.ts","../src/process/normalizeSegment.ts","../src/parser/paramsParser.ts","../src/convert/pathToCurve.ts","../src/convert/pathToString.ts","../src/util/getPathBBox.ts","../src/util/getTotalLength.ts","../src/util/distanceEpsilon.ts","../src/process/normalizePath.ts","../src/util/getPointAtLength.ts","../src/util/getPropertiesAtLength.ts","../src/util/getPropertiesAtPoint.ts","../src/util/getClosestPoint.ts","../src/util/getPathArea.ts","../src/util/getDrawDirection.ts","../src/util/getSegmentAtLength.ts","../src/util/getSegmentOfPoint.ts","../src/util/isPathArray.ts","../src/util/isAbsoluteArray.ts","../src/util/isNormalizedArray.ts","../src/util/isCurveArray.ts","../src/util/isPointInStroke.ts","../src/util/isRelativeArray.ts","../src/util/isValidPath.ts","../src/util/shapeParams.ts","../src/util/isElement.ts","../src/util/shapeToPathArray.ts","../src/util/shapeToPath.ts","../src/process/shortenSegment.ts","../src/process/roundSegment.ts","../src/process/optimizePath.ts","../src/process/reversePath.ts","../src/process/splitPath.ts","../src/process/getSVGMatrix.ts","../src/process/projection2d.ts","../src/process/transformPath.ts","../src/process/reverseCurve.ts","../src/process/roundPath.ts","../src/process/splitCubic.ts","../src/index.ts"],"sourcesContent":["\"use strict\";\nimport CSSMatrix from \"@thednp/dommatrix\";\nimport { arcTools } from \"./math/arcTools\";\nimport { bezierTools } from \"./math/bezier\";\nimport { cubicTools } from \"./math/cubicTools\";\nimport { lineTools } from \"./math/lineTools\";\nimport { quadTools } from \"./math/quadTools\";\nimport { polygonTools } from \"./math/polygonTools\";\n\nimport distanceSquareRoot from \"./math/distanceSquareRoot\";\nimport midPoint from \"./math/midPoint\";\nimport rotateVector from \"./math/rotateVector\";\nimport roundTo from \"./math/roundTo\";\n\nimport type { PathArray, PointTuple, TransformObjectValues } from \"./types\";\nimport type { Options, TransformEntries, TransformObject } from \"./interface\";\nimport defaultOptions from \"./options/options\";\n\nimport pathToAbsolute from \"./convert/pathToAbsolute\";\nimport pathToRelative from \"./convert/pathToRelative\";\nimport pathToCurve from \"./convert/pathToCurve\";\nimport pathToString from \"./convert/pathToString\";\n\nimport error from \"./parser/error\";\nimport parsePathString from \"./parser/parsePathString\";\nimport finalizeSegment from \"./parser/finalizeSegment\";\nimport invalidPathValue from \"./parser/invalidPathValue\";\nimport isArcCommand from \"./parser/isArcCommand\";\nimport isDigit from \"./parser/isDigit\";\nimport isDigitStart from \"./parser/isDigitStart\";\nimport isMoveCommand from \"./parser/isMoveCommand\";\nimport isPathCommand from \"./parser/isPathCommand\";\nimport isSpace from \"./parser/isSpace\";\nimport paramsCount from \"./parser/paramsCount\";\nimport paramsParser from \"./parser/paramsParser\";\nimport pathParser from \"./parser/pathParser\";\nimport scanFlag from \"./parser/scanFlag\";\nimport scanParam from \"./parser/scanParam\";\nimport scanSegment from \"./parser/scanSegment\";\nimport skipSpaces from \"./parser/skipSpaces\";\nimport getPathBBox from \"./util/getPathBBox\";\nimport getTotalLength from \"./util/getTotalLength\";\nimport distanceEpsilon from \"./util/distanceEpsilon\";\nimport getClosestPoint from \"./util/getClosestPoint\";\nimport getDrawDirection from \"./util/getDrawDirection\";\nimport getPathArea from \"./util/getPathArea\";\nimport getPointAtLength from \"./util/getPointAtLength\";\nimport getPropertiesAtLength from \"./util/getPropertiesAtLength\";\nimport getPropertiesAtPoint from \"./util/getPropertiesAtPoint\";\nimport getSegmentAtLength from \"./util/getSegmentAtLength\";\nimport getSegmentOfPoint from \"./util/getSegmentOfPoint\";\nimport isAbsoluteArray from \"./util/isAbsoluteArray\";\nimport isCurveArray from \"./util/isCurveArray\";\nimport isNormalizedArray from \"./util/isNormalizedArray\";\nimport isPathArray from \"./util/isPathArray\";\nimport isPointInStroke from \"./util/isPointInStroke\";\nimport isRelativeArray from \"./util/isRelativeArray\";\nimport isValidPath from \"./util/isValidPath\";\nimport shapeParams from \"./util/shapeParams\";\nimport shapeToPath from \"./util/shapeToPath\";\nimport shapeToPathArray from \"./util/shapeToPathArray\";\nimport normalizePath from \"./process/normalizePath\";\nimport optimizePath from \"./process/optimizePath\";\nimport reversePath from \"./process/reversePath\";\nimport splitPath from \"./process/splitPath\";\nimport transformPath from \"./process/transformPath\";\nimport absolutizeSegment from \"./process/absolutizeSegment\";\nimport arcToCubic from \"./process/arcToCubic\";\nimport getSVGMatrix from \"./process/getSVGMatrix\";\nimport iterate from \"./process/iterate\";\nimport lineToCubic from \"./process/lineToCubic\";\nimport normalizeSegment from \"./process/normalizeSegment\";\nimport projection2d from \"./process/projection2d\";\nimport quadToCubic from \"./process/quadToCubic\";\nimport relativizeSegment from \"./process/relativizeSegment\";\nimport reverseCurve from \"./process/reverseCurve\";\nimport roundPath from \"./process/roundPath\";\nimport roundSegment from \"./process/roundSegment\";\nimport segmentToCubic from \"./process/segmentToCubic\";\nimport shortenSegment from \"./process/shortenSegment\";\nimport splitCubic from \"./process/splitCubic\";\n/**\n * Creates a new SVGPathCommander instance with the following properties:\n * * segments: `pathArray`\n * * round: number\n * * origin: [number, number, number?]\n *\n * @class\n * @author thednp <https://github.com/thednp/svg-path-commander>\n * @returns a new SVGPathCommander instance\n */\nclass SVGPathCommander {\n // declare class properties\n declare segments: PathArray;\n declare round: number | \"off\";\n declare origin: [number, number, number];\n\n /**\n * @constructor\n * @param pathValue the path string\n * @param config instance options\n */\n constructor(pathValue: string, config?: Partial<Options>) {\n const instanceOptions = config || {};\n const undefPath = typeof pathValue === \"undefined\";\n\n if (undefPath || !pathValue.length) {\n throw TypeError(\n `${error}: \"pathValue\" is ${undefPath ? \"undefined\" : \"empty\"}`,\n );\n }\n\n this.segments = parsePathString(pathValue);\n\n // // set instance options.round\n const { round: roundOption, origin: originOption } = instanceOptions;\n let round: number | \"off\";\n\n if (Number.isInteger(roundOption) || roundOption === \"off\") {\n round = roundOption as number | \"off\";\n } else {\n round = defaultOptions.round as number;\n }\n\n // set instance options.origin\n // the SVGPathCommander class will always override the default origin\n let origin = defaultOptions.origin as [number, number, number];\n /* istanbul ignore else @preserve */\n if (Array.isArray(originOption) && originOption.length >= 2) {\n const [originX, originY, originZ] = originOption.map(Number);\n origin = [\n !Number.isNaN(originX) ? originX : 0,\n !Number.isNaN(originY) ? originY : 0,\n !Number.isNaN(originZ) ? originZ : 0,\n ];\n }\n\n this.round = round;\n this.origin = origin;\n\n return this;\n }\n get bbox() {\n return getPathBBox(this.segments);\n }\n get length() {\n return getTotalLength(this.segments);\n }\n\n /**\n * Returns the path bounding box, equivalent to native `path.getBBox()`.\n *\n * @public\n * @returns the pathBBox\n */\n getBBox() {\n return this.bbox;\n }\n\n /**\n * Returns the total path length, equivalent to native `path.getTotalLength()`.\n *\n * @public\n * @returns the path total length\n */\n getTotalLength() {\n return this.length;\n }\n\n /**\n * Returns an `{x,y}` point in the path stroke at a given length,\n * equivalent to the native `path.getPointAtLength()`.\n *\n * @public\n * @param length the length\n * @returns the requested point\n */\n getPointAtLength(length: number) {\n return getPointAtLength(this.segments, length);\n }\n\n /**\n * Convert path to absolute values\n *\n * @public\n */\n toAbsolute() {\n const { segments } = this;\n this.segments = pathToAbsolute(segments);\n return this;\n }\n\n /**\n * Convert path to relative values\n *\n * @public\n */\n toRelative() {\n const { segments } = this;\n this.segments = pathToRelative(segments);\n return this;\n }\n\n /**\n * Convert path to cubic-bezier values. In addition, un-necessary `Z`\n * segment is removed if previous segment extends to the `M` segment.\n *\n * @public\n */\n toCurve() {\n const { segments } = this;\n this.segments = pathToCurve(segments);\n return this;\n }\n\n /**\n * Reverse the order of the segments and their values.\n *\n * @param onlySubpath option to reverse all sub-paths except first\n * @public\n */\n reverse(onlySubpath?: boolean) {\n const { segments } = this;\n const split = splitPath(segments);\n const subPath = split.length > 1 ? split : false;\n\n const absoluteMultiPath = subPath\n ? subPath.map((x, i) => {\n if (onlySubpath) {\n return i ? reversePath(x) : x.slice(0);\n }\n return reversePath(x);\n })\n : segments.slice(0);\n\n let path = [] as unknown as PathArray;\n if (subPath) {\n path = absoluteMultiPath.flat(1) as PathArray;\n } else {\n path = onlySubpath ? segments : reversePath(segments);\n }\n\n this.segments = path.slice(0) as PathArray;\n return this;\n }\n\n /**\n * Normalize path in 2 steps:\n * * convert `pathArray`(s) to absolute values\n * * convert shorthand notation to standard notation\n *\n * @public\n */\n normalize() {\n const { segments } = this;\n this.segments = normalizePath(segments);\n return this;\n }\n\n /**\n * Optimize `pathArray` values:\n * * convert segments to absolute and/or relative values\n * * select segments with shortest resulted string\n * * round values to the specified `decimals` option value\n *\n * @public\n */\n optimize() {\n const { segments } = this;\n const round = this.round === \"off\" ? 2 : this.round;\n\n this.segments = optimizePath(segments, round);\n return this;\n }\n\n /**\n * Transform path using values from an `Object` defined as `transformObject`.\n *\n * @see TransformObject for a quick refference\n *\n * @param source a `transformObject`as described above\n * @public\n */\n transform(source?: Partial<TransformObject>) {\n if (\n !source ||\n typeof source !== \"object\" ||\n (typeof source === \"object\" &&\n ![\"translate\", \"rotate\", \"skew\", \"scale\"].some((x) => x in source))\n ) {\n return this;\n }\n\n const {\n segments,\n origin: [cx, cy, cz],\n } = this;\n const transform = {} as TransformObjectValues;\n for (const [k, v] of Object.entries(source) as TransformEntries) {\n // istanbul ignore else @preserve\n if (k === \"skew\" && Array.isArray(v)) {\n transform[k] = v.map(Number) as PointTuple;\n } else if (\n (k === \"rotate\" ||\n k === \"translate\" ||\n k === \"origin\" ||\n k === \"scale\") &&\n Array.isArray(v)\n ) {\n transform[k] = v.map(Number) as [number, number, number];\n } else if (k !== \"origin\" && typeof Number(v) === \"number\") {\n transform[k] = Number(v);\n }\n }\n\n // if origin is not specified\n // it's important that we have one\n const { origin } = transform;\n\n if (Array.isArray(origin) && origin.length >= 2) {\n const [originX, originY, originZ] = origin.map(Number);\n transform.origin = [\n !Number.isNaN(originX) ? originX : cx,\n !Number.isNaN(originY) ? originY : cy,\n originZ || cz,\n ];\n } else {\n transform.origin = [cx, cy, cz];\n }\n\n this.segments = transformPath(segments, transform);\n return this;\n }\n\n /**\n * Rotate path 180deg vertically\n *\n * @public\n */\n flipX() {\n const { cx, cy } = this.bbox;\n this.transform({ rotate: [0, 180, 0], origin: [cx, cy, 0] });\n return this;\n }\n\n /**\n * Rotate path 180deg horizontally\n *\n * @public\n */\n flipY() {\n const { cx, cy } = this.bbox;\n this.transform({ rotate: [180, 0, 0], origin: [cx, cy, 0] });\n return this;\n }\n\n /**\n * Export the current path to be used\n * for the `d` (description) attribute.\n *\n * @public\n * @return the path string\n */\n toString() {\n return pathToString(this.segments, this.round);\n }\n\n /**\n * Remove the instance.\n *\n * @public\n * @return void\n */\n dispose() {\n Object.keys(this).forEach((key) => delete this[key as keyof typeof this]);\n }\n\n static get options() {\n return defaultOptions;\n }\n static get CSSMatrix() {\n return CSSMatrix;\n }\n static get arcTools() {\n return arcTools;\n }\n static get bezierTools() {\n return bezierTools;\n }\n\n static get cubicTools() {\n return cubicTools;\n }\n static get lineTools() {\n return lineTools;\n }\n static get polygonTools() {\n return polygonTools;\n }\n static get quadTools() {\n return quadTools;\n }\n static get pathToAbsolute() {\n return pathToAbsolute;\n }\n static get pathToRelative() {\n return pathToRelative;\n }\n static get pathToCurve() {\n return pathToCurve;\n }\n static get pathToString() {\n return pathToString;\n }\n static get distanceSquareRoot() {\n return distanceSquareRoot;\n }\n static get midPoint() {\n return midPoint;\n }\n static get rotateVector() {\n return rotateVector;\n }\n static get roundTo() {\n return roundTo;\n }\n static get parsePathString() {\n return parsePathString;\n }\n static get finalizeSegment() {\n return finalizeSegment;\n }\n static get invalidPathValue() {\n return invalidPathValue;\n }\n static get isArcCommand() {\n return isArcCommand;\n }\n static get isDigit() {\n return isDigit;\n }\n static get isDigitStart() {\n return isDigitStart;\n }\n static get isMoveCommand() {\n return isMoveCommand;\n }\n static get isPathCommand() {\n return isPathCommand;\n }\n static get isSpace() {\n return isSpace;\n }\n static get paramsCount() {\n return paramsCount;\n }\n static get paramsParser() {\n return paramsParser;\n }\n static get pathParser() {\n return pathParser;\n }\n static get scanFlag() {\n return scanFlag;\n }\n static get scanParam() {\n return scanParam;\n }\n static get scanSegment() {\n return scanSegment;\n }\n static get skipSpaces() {\n return skipSpaces;\n }\n static get distanceEpsilon() {\n return distanceEpsilon;\n }\n static get getClosestPoint() {\n return getClosestPoint;\n }\n static get getDrawDirection() {\n return getDrawDirection;\n }\n static get getPathArea() {\n return getPathArea;\n }\n static get getPathBBox() {\n return getPathBBox;\n }\n static get getPointAtLength() {\n return getPointAtLength;\n }\n static get getPropertiesAtLength() {\n return getPropertiesAtLength;\n }\n static get getPropertiesAtPoint() {\n return getPropertiesAtPoint;\n }\n static get getSegmentAtLength() {\n return getSegmentAtLength;\n }\n static get getSegmentOfPoint() {\n return getSegmentOfPoint;\n }\n static get getTotalLength() {\n return getTotalLength;\n }\n static get isAbsoluteArray() {\n return isAbsoluteArray;\n }\n static get isCurveArray() {\n return isCurveArray;\n }\n static get isNormalizedArray() {\n return isNormalizedArray;\n }\n static get isPathArray() {\n return isPathArray;\n }\n static get isPointInStroke() {\n return isPointInStroke;\n }\n static get isRelativeArray() {\n return isRelativeArray;\n }\n static get isValidPath() {\n return isValidPath;\n }\n static get shapeParams() {\n return shapeParams;\n }\n static get shapeToPath() {\n return shapeToPath;\n }\n static get shapeToPathArray() {\n return shapeToPathArray;\n }\n static get absolutizeSegment() {\n return absolutizeSegment;\n }\n static get arcToCubic() {\n return arcToCubic;\n }\n static get getSVGMatrix() {\n return getSVGMatrix;\n }\n static get iterate() {\n return iterate;\n }\n static get lineToCubic() {\n return lineToCubic;\n }\n static get normalizePath() {\n return normalizePath;\n }\n static get normalizeSegment() {\n return normalizeSegment;\n }\n static get optimizePath() {\n return optimizePath;\n }\n static get projection2d() {\n return projection2d;\n }\n static get quadToCubic() {\n return quadToCubic;\n }\n static get relativizeSegment() {\n return relativizeSegment;\n }\n static get reverseCurve() {\n return reverseCurve;\n }\n static get reversePath() {\n return reversePath;\n }\n static get roundPath() {\n return roundPath;\n }\n static get roundSegment() {\n return roundSegment;\n }\n static get segmentToCubic() {\n return segmentToCubic;\n }\n static get shortenSegment() {\n return shortenSegment;\n }\n static get splitCubic() {\n return splitCubic;\n }\n static get splitPath() {\n return splitPath;\n }\n static get transformPath() {\n return transformPath;\n }\n}\n\nexport default SVGPathCommander;\n","import { PointTuple } from \"../types\";\n\n/**\n * Returns the coordinates of a specified distance\n * ratio between two points.\n *\n * @param a the first point coordinates\n * @param b the second point coordinates\n * @param t the ratio\n * @returns the midpoint coordinates\n */\nconst midPoint = (a: PointTuple, b: PointTuple, t: number): PointTuple => {\n const [ax, ay] = a;\n const [bx, by] = b;\n return [ax + (bx - ax) * t, ay + (by - ay) * t];\n};\n\nexport default midPoint;\n","import { type PointTuple } from \"../types\";\n\n/**\n * Returns the square root of the distance\n * between two given points.\n *\n * @param a the first point coordinates\n * @param b the second point coordinates\n * @returns the distance value\n */\nconst distanceSquareRoot = (a: PointTuple, b: PointTuple) => {\n return Math.sqrt(\n (a[0] - b[0]) * (a[0] - b[0]) + (a[1] - b[1]) * (a[1] - b[1]),\n );\n};\n\nexport default distanceSquareRoot;\n","import midPoint from \"./midPoint\";\nimport distanceSquareRoot from \"./distanceSquareRoot\";\n\n/**\n * Returns length for line segments (MoveTo, LineTo).\n *\n * @param x1 the starting point X\n * @param y1 the starting point Y\n * @param x2 the ending point X\n * @param y2 the ending point Y\n * @returns the line segment length\n */\nconst getLineLength = (x1: number, y1: number, x2: number, y2: number) => {\n return distanceSquareRoot([x1, y1], [x2, y2]);\n};\n\n/**\n * Returns a point along the line segments (MoveTo, LineTo).\n *\n * @param x1 the starting point X\n * @param y1 the starting point Y\n * @param x2 the ending point X\n * @param y2 the ending point Y\n * @param distance the distance to point in [0-1] range\n * @returns the point at length\n */\nconst getPointAtLineLength = (\n x1: number,\n y1: number,\n x2: number,\n y2: number,\n distance?: number,\n) => {\n let point = { x: x1, y: y1 };\n\n /* istanbul ignore else @preserve */\n if (typeof distance === \"number\") {\n const length = distanceSquareRoot([x1, y1], [x2, y2]);\n if (distance <= 0) {\n point = { x: x1, y: y1 };\n } else if (distance >= length) {\n point = { x: x2, y: y2 };\n } else {\n const [x, y] = midPoint([x1, y1], [x2, y2], distance / length);\n point = { x, y };\n }\n }\n return point;\n};\n\n/**\n * Returns bounding box for line segments (MoveTo, LineTo).\n *\n * @param x1 the starting point X\n * @param y1 the starting point Y\n * @param x2 the ending point X\n * @param y2 the ending point Y\n * @param distance the distance to point in [0-1] range\n * @returns the extrema for line segments\n */\nconst getLineBBox = (x1: number, y1: number, x2: number, y2: number) => {\n const { min, max } = Math;\n\n return [min(x1, x2), min(y1, y2), max(x1, x2), max(y1, y2)] as [\n number,\n number,\n number,\n number,\n ];\n};\n\nconst lineTools = {\n getLineBBox,\n getLineLength,\n getPointAtLineLength,\n};\n\nexport { getLineBBox, getLineLength, getPointAtLineLength, lineTools };\n","import { getPointAtLineLength } from \"./lineTools\";\nimport type { Point, PointTuple } from \"../types\";\n\n/**\n * Returns the Arc segment length.\n * @param rx radius along X axis\n * @param ry radius along Y axis\n * @param theta the angle in radians\n * @returns the arc length\n */\nconst arcLength = (rx: number, ry: number, theta: number) => {\n const halfTheta = theta / 2;\n const sinHalfTheta = Math.sin(halfTheta);\n const cosHalfTheta = Math.cos(halfTheta);\n const term1 = rx ** 2 * sinHalfTheta ** 2;\n const term2 = ry ** 2 * cosHalfTheta ** 2;\n const length = Math.sqrt(term1 + term2) * theta;\n return Math.abs(length);\n};\n\n/**\n * Find point on ellipse at given angle around ellipse (theta);\n * @param cx the center X\n * @param cy the center Y\n * @param rx the radius X\n * @param ry the radius Y\n * @param alpha the arc rotation angle in radians\n * @param theta the arc sweep angle in radians\n * @returns a point around ellipse at given angle\n */\nconst arcPoint = (\n cx: number,\n cy: number,\n rx: number,\n ry: number,\n alpha: number,\n theta: number,\n) => {\n const { sin, cos } = Math;\n // theta is angle in radians around arc\n // alpha is angle of rotation of ellipse in radians\n const cosA = cos(alpha);\n const sinA = sin(alpha);\n const x = rx * cos(theta);\n const y = ry * sin(theta);\n\n return [cx + cosA * x - sinA * y, cy + sinA * x + cosA * y] as PointTuple;\n};\n\n/**\n * Returns the angle between two points.\n * @param v0 starting point\n * @param v1 ending point\n * @returns the angle in radian\n */\nconst angleBetween = (v0: Point, v1: Point) => {\n const { x: v0x, y: v0y } = v0;\n const { x: v1x, y: v1y } = v1;\n const p = v0x * v1x + v0y * v1y;\n const n = Math.sqrt((v0x ** 2 + v0y ** 2) * (v1x ** 2 + v1y ** 2));\n const sign = v0x * v1y - v0y * v1x < 0 ? -1 : 1;\n return sign * Math.acos(p / n);\n};\n\n/**\n * Returns the following properties for an Arc segment: center, start angle,\n * end angle, and radiuses on X and Y axis.\n *\n * @param x1 the starting point X\n * @param y1 the starting point Y\n * @param RX the radius on X axis\n * @param RY the radius on Y axis\n * @param angle the ellipse rotation in degrees\n * @param LAF the large arc flag\n * @param SF the sweep flag\n * @param x2 the ending point X\n * @param y2 the ending point Y\n * @returns properties specific to Arc segments\n */\nconst getArcProps = (\n x1: number,\n y1: number,\n RX: number,\n RY: number,\n angle: number,\n LAF: number,\n SF: number,\n x: number,\n y: number,\n) => {\n const { abs, sin, cos, sqrt, PI } = Math;\n let rx = abs(RX);\n let ry = abs(RY);\n const xRot = ((angle % 360) + 360) % 360;\n const xRotRad = xRot * (PI / 180);\n\n // istanbul ignore next @preserve\n if (x1 === x && y1 === y) {\n return {\n rx,\n ry,\n startAngle: 0,\n endAngle: 0,\n center: { x, y },\n };\n }\n\n if (rx === 0 || ry === 0) {\n return {\n rx,\n ry,\n startAngle: 0,\n endAngle: 0,\n center: { x: (x + x1) / 2, y: (y + y1) / 2 },\n };\n }\n\n const dx = (x1 - x) / 2;\n const dy = (y1 - y) / 2;\n\n const transformedPoint = {\n x: cos(xRotRad) * dx + sin(xRotRad) * dy,\n y: -sin(xRotRad) * dx + cos(xRotRad) * dy,\n };\n\n const radiiCheck = transformedPoint.x ** 2 / rx ** 2 +\n transformedPoint.y ** 2 / ry ** 2;\n\n if (radiiCheck > 1) {\n rx *= sqrt(radiiCheck);\n ry *= sqrt(radiiCheck);\n }\n\n const cSquareNumerator = rx ** 2 * ry ** 2 -\n rx ** 2 * transformedPoint.y ** 2 - ry ** 2 * transformedPoint.x ** 2;\n const cSquareRootDenom = rx ** 2 * transformedPoint.y ** 2 +\n ry ** 2 * transformedPoint.x ** 2;\n\n let cRadicand = cSquareNumerator / cSquareRootDenom;\n /* istanbul ignore next @preserve */\n cRadicand = cRadicand < 0 ? 0 : cRadicand;\n const cCoef = (LAF !== SF ? 1 : -1) * sqrt(cRadicand);\n const transformedCenter = {\n x: cCoef * ((rx * transformedPoint.y) / ry),\n y: cCoef * (-(ry * transformedPoint.x) / rx),\n };\n\n const center = {\n x: cos(xRotRad) * transformedCenter.x - sin(xRotRad) * transformedCenter.y +\n (x1 + x) / 2,\n y: sin(xRotRad) * transformedCenter.x + cos(xRotRad) * transformedCenter.y +\n (y1 + y) / 2,\n };\n\n const startVector = {\n x: (transformedPoint.x - transformedCenter.x) / rx,\n y: (transformedPoint.y - transformedCenter.y) / ry,\n };\n\n const startAngle = angleBetween({ x: 1, y: 0 }, startVector);\n\n const endVector = {\n x: (-transformedPoint.x - transformedCenter.x) / rx,\n y: (-transformedPoint.y - transformedCenter.y) / ry,\n };\n\n let sweepAngle = angleBetween(startVector, endVector);\n if (!SF && sweepAngle > 0) {\n sweepAngle -= 2 * PI;\n } else if (SF && sweepAngle < 0) {\n sweepAngle += 2 * PI;\n }\n sweepAngle %= 2 * PI;\n\n const endAngle = startAngle + sweepAngle;\n\n // point.ellipticalArcStartAngle = startAngle;\n // point.ellipticalArcEndAngle = startAngle + sweepAngle;\n // point.ellipticalArcAngle = alpha;\n\n // point.ellipticalArcCenter = center;\n // point.resultantRx = rx;\n // point.resultantRy = ry;\n\n return {\n center,\n startAngle,\n endAngle,\n rx,\n ry,\n };\n};\n\n/**\n * Returns the length of an Arc segment.\n *\n * @param x1 the starting point X\n * @param y1 the starting point Y\n * @param c1x the first control point X\n * @param c1y the first control point Y\n * @param c2x the second control point X\n * @param c2y the second control point Y\n * @param x2 the ending point X\n * @param y2 the ending point Y\n * @returns the length of the Arc segment\n */\nconst getArcLength = (\n x1: number,\n y1: number,\n RX: number,\n RY: number,\n angle: number,\n LAF: number,\n SF: number,\n x: number,\n y: number,\n) => {\n const { rx, ry, startAngle, endAngle } = getArcProps(\n x1,\n y1,\n RX,\n RY,\n angle,\n LAF,\n SF,\n x,\n y,\n );\n return arcLength(rx, ry, endAngle - startAngle);\n};\n\n/**\n * Returns a point along an Arc segment at a given distance.\n *\n * @param x1 the starting point X\n * @param y1 the starting point Y\n * @param RX the radius on X axis\n * @param RY the radius on Y axis\n * @param angle the ellipse rotation in degrees\n * @param LAF the large arc flag\n * @param SF the sweep flag\n * @param x2 the ending point X\n * @param y2 the ending point Y\n * @param distance a [0-1] ratio\n * @returns a point along the Arc segment\n */\nconst getPointAtArcLength = (\n x1: number,\n y1: number,\n RX: number,\n RY: number,\n angle: number,\n LAF: number,\n SF: number,\n x: number,\n y: number,\n distance?: number,\n) => {\n let point = { x: x1, y: y1 };\n const { center, rx, ry, startAngle, endAngle } = getArcProps(\n x1,\n y1,\n RX,\n RY,\n angle,\n LAF,\n SF,\n x,\n y,\n );\n\n /* istanbul ignore else @preserve */\n if (typeof distance === \"number\") {\n const length = arcLength(rx, ry, endAngle - startAngle);\n if (distance <= 0) {\n point = { x: x1, y: y1 };\n } else if (distance >= length) {\n point = { x, y };\n } else {\n /* istanbul ignore next @preserve */\n if (x1 === x && y1 === y) {\n return { x, y };\n }\n /* istanbul ignore next @preserve */\n if (rx === 0 || ry === 0) {\n return getPointAtLineLength(x1, y1, x, y, distance);\n }\n const { PI, cos, sin } = Math;\n const sweepAngle = endAngle - startAngle;\n const xRot = ((angle % 360) + 360) % 360;\n const xRotRad = xRot * (PI / 180);\n const alpha = startAngle + sweepAngle * (distance / length);\n const ellipseComponentX = rx * cos(alpha);\n const ellipseComponentY = ry * sin(alpha);\n\n point = {\n x: cos(xRotRad) * ellipseComponentX - sin(xRotRad) * ellipseComponentY +\n center.x,\n y: sin(xRotRad) * ellipseComponentX + cos(xRotRad) * ellipseComponentY +\n center.y,\n };\n }\n }\n\n return point;\n};\n\n/**\n * Returns the extrema for an Arc segment in the following format:\n * [MIN_X, MIN_Y, MAX_X, MAX_Y]\n *\n * @see https://github.com/herrstrietzel/svg-pathdata-getbbox\n *\n * @param x1 the starting point X\n * @param y1 the starting point Y\n * @param RX the radius on X axis\n * @param RY the radius on Y axis\n * @param angle the ellipse rotation in degrees\n * @param LAF the large arc flag\n * @param SF the sweep flag\n * @param x2 the ending point X\n * @param y2 the ending point Y\n * @returns the extrema of the Arc segment\n */\nconst getArcBBox = (\n x1: number,\n y1: number,\n RX: number,\n RY: number,\n angle: number,\n LAF: number,\n SF: number,\n x: number,\n y: number,\n) => {\n const { center, rx, ry, startAngle, endAngle } = getArcProps(\n x1,\n y1,\n RX,\n RY,\n angle,\n LAF,\n SF,\n x,\n y,\n );\n const deltaAngle = endAngle - startAngle;\n const { min, max, tan, atan2, PI } = Math;\n\n // circle/elipse center coordinates\n const { x: cx, y: cy } = center;\n\n // rotation to radians\n const alpha = (angle * PI) / 180;\n const tangent = tan(alpha);\n\n /**\n * find min/max from zeroes of directional derivative along x and y\n * along x axis\n */\n const theta = atan2(-ry * tangent, rx);\n const angle1 = theta;\n const angle2 = theta + PI;\n const angle3 = atan2(ry, rx * tangent);\n const angle4 = angle3 + PI;\n const xArray = [x];\n const yArray = [y];\n\n // inner bounding box\n let xMin = min(x1, x);\n let xMax = max(x1, x);\n let yMin = min(y1, y);\n let yMax = max(y1, y);\n\n // on path point close after start\n const angleAfterStart = endAngle - deltaAngle * 0.00001;\n const pP2 = arcPoint(cx, cy, rx, ry, alpha, angleAfterStart);\n\n // on path point close before end\n const angleBeforeEnd = endAngle - deltaAngle * 0.99999;\n const pP3 = arcPoint(cx, cy, rx, ry, alpha, angleBeforeEnd);\n\n /**\n * expected extremes\n * if leaving inner bounding box\n * (between segment start and end point)\n * otherwise exclude elliptic extreme points\n */\n\n // right\n if (pP2[0] > xMax || pP3[0] > xMax) {\n // get point for this theta\n const p1 = arcPoint(cx, cy, rx, ry, alpha, angle1);\n xArray.push(p1[0]);\n yArray.push(p1[1]);\n }\n\n // left\n if (pP2[0] < xMin || pP3[0] < xMin) {\n // get anti-symmetric point\n const p2 = arcPoint(cx, cy, rx, ry, alpha, angle2);\n xArray.push(p2[0]);\n yArray.push(p2[1]);\n }\n\n // top\n if (pP2[1] < yMin || pP3[1] < yMin) {\n // get anti-symmetric point\n const p4 = arcPoint(cx, cy, rx, ry, alpha, angle4);\n xArray.push(p4[0]);\n yArray.push(p4[1]);\n }\n\n // bottom\n if (pP2[1] > yMax || pP3[1] > yMax) {\n // get point for this theta\n const p3 = arcPoint(cx, cy, rx, ry, alpha, angle3);\n xArray.push(p3[0]);\n yArray.push(p3[1]);\n }\n\n xMin = min.apply([], xArray);\n yMin = min.apply([], yArray);\n xMax = max.apply([], xArray);\n yMax = max.apply([], yArray);\n\n return [xMin, yMin, xMax, yMax] as [number, number, number, number];\n};\n\nconst arcTools = {\n angleBetween,\n arcLength,\n arcPoint,\n getArcBBox,\n getArcLength,\n getArcProps,\n getPointAtArcLength,\n};\n\nexport {\n angleBetween,\n arcLength,\n arcPoint,\n arcTools,\n getArcBBox,\n getArcLength,\n getArcProps,\n getPointAtArcLength,\n};\n","import type {\n CubicCoordinates,\n CubicPoints,\n DeriveCallback,\n DerivedCubicPoints,\n DerivedPoint,\n DerivedQuadPoints,\n PointTuple,\n QuadCoordinates,\n QuadPoints,\n} from \"../types\";\n\n/**\n * Tools from bezier.js by Mike 'Pomax' Kamermans\n * @see https://github.com/Pomax/bezierjs\n */\n\nconst Tvalues = [\n -0.0640568928626056260850430826247450385909,\n 0.0640568928626056260850430826247450385909,\n -0.1911188674736163091586398207570696318404,\n 0.1911188674736163091586398207570696318404,\n -0.3150426796961633743867932913198102407864,\n 0.3150426796961633743867932913198102407864,\n -0.4337935076260451384870842319133497124524,\n 0.4337935076260451384870842319133497124524,\n -0.5454214713888395356583756172183723700107,\n 0.5454214713888395356583756172183723700107,\n -0.6480936519369755692524957869107476266696,\n 0.6480936519369755692524957869107476266696,\n -0.7401241915785543642438281030999784255232,\n 0.7401241915785543642438281030999784255232,\n -0.8200019859739029219539498726697452080761,\n 0.8200019859739029219539498726697452080761,\n -0.8864155270044010342131543419821967550873,\n 0.8864155270044010342131543419821967550873,\n -0.9382745520027327585236490017087214496548,\n 0.9382745520027327585236490017087214496548,\n -0.9747285559713094981983919930081690617411,\n 0.9747285559713094981983919930081690617411,\n -0.9951872199970213601799974097007368118745,\n 0.9951872199970213601799974097007368118745,\n];\n\nconst Cvalues = [\n 0.1279381953467521569740561652246953718517,\n 0.1279381953467521569740561652246953718517,\n 0.1258374563468282961213753825111836887264,\n 0.1258374563468282961213753825111836887264,\n 0.121670472927803391204463153476262425607,\n 0.121670472927803391204463153476262425607,\n 0.1155056680537256013533444839067835598622,\n 0.1155056680537256013533444839067835598622,\n 0.1074442701159656347825773424466062227946,\n 0.1074442701159656347825773424466062227946,\n 0.0976186521041138882698806644642471544279,\n 0.0976186521041138882698806644642471544279,\n 0.086190161531953275917185202983742667185,\n 0.086190161531953275917185202983742667185,\n 0.0733464814110803057340336152531165181193,\n 0.0733464814110803057340336152531165181193,\n 0.0592985849154367807463677585001085845412,\n 0.0592985849154367807463677585001085845412,\n 0.0442774388174198061686027482113382288593,\n 0.0442774388174198061686027482113382288593,\n 0.0285313886289336631813078159518782864491,\n 0.0285313886289336631813078159518782864491,\n 0.0123412297999871995468056670700372915759,\n 0.0123412297999871995468056670700372915759,\n];\n\n/**\n * @param points\n * @returns\n */\nconst deriveBezier = (points: QuadPoints | CubicPoints) => {\n const dpoints = [] as (DerivedCubicPoints | DerivedQuadPoints)[];\n for (let p = points, d = p.length, c = d - 1; d > 1; d -= 1, c -= 1) {\n const list = [] as unknown as DerivedCubicPoints | DerivedQuadPoints;\n for (let j = 0; j < c; j += 1) {\n list.push({\n x: c * (p[j + 1].x - p[j].x),\n y: c * (p[j + 1].y - p[j].y),\n t: 0,\n });\n }\n dpoints.push(list);\n p = list;\n }\n return dpoints;\n};\n\n/**\n * @param points\n * @param t\n */\nconst computeBezier = (\n points: DerivedQuadPoints | DerivedCubicPoints,\n t: number,\n) => {\n // shortcuts\n /* istanbul ignore next @preserve */\n if (t === 0) {\n points[0].t = 0;\n return points[0];\n }\n\n const order = points.length - 1;\n\n /* istanbul ignore next @preserve */\n if (t === 1) {\n points[order].t = 1;\n return points[order];\n }\n\n const mt = 1 - t;\n let p = points as typeof points | [\n DerivedPoint,\n DerivedPoint,\n DerivedPoint,\n DerivedPoint,\n ];\n\n // constant?\n /* istanbul ignore next @preserve */\n if (order === 0) {\n points[0].t = t;\n return points[0];\n }\n\n // linear?\n /* istanbul ignore else @preserve */\n if (order === 1) {\n return {\n x: mt * p[0].x + t * p[1].x,\n y: mt * p[0].y + t * p[1].y,\n t,\n };\n }\n\n // quadratic/cubic curve?\n const mt2 = mt * mt;\n const t2 = t * t;\n let a = 0;\n let b = 0;\n let c = 0;\n let d = 0;\n /* istanbul ignore else @preserve */\n if (order === 2) {\n p = [p[0], p[1], p[2], { x: 0, y: 0 } as DerivedPoint];\n a = mt2;\n b = mt * t * 2;\n c = t2;\n } else if (order === 3) {\n a = mt2 * mt;\n b = mt2 * t * 3;\n c = mt * t2 * 3;\n d = t * t2;\n }\n return {\n x: a * p[0].x + b * p[1].x + c * p[2].x + d * p[3].x,\n y: a * p[0].y + b * p[1].y + c * p[2].y + d * p[3].y,\n t,\n };\n};\n\nconst calculateBezier = (derivativeFn: DeriveCallback, t: number) => {\n const d = derivativeFn(t);\n const l = d.x * d.x + d.y * d.y;\n\n return Math.sqrt(l);\n};\n\nconst bezierLength = (derivativeFn: DeriveCallback) => {\n const z = 0.5;\n const len = Tvalues.length;\n\n let sum = 0;\n\n for (let i = 0, t; i < len; i++) {\n t = z * Tvalues[i] + z;\n sum += Cvalues[i] * calculateBezier(derivativeFn, t);\n }\n return z * sum;\n};\n\n/**\n * Returns the length of CubicBezier / Quad segment.\n * @param curve cubic / quad bezier segment\n */\nconst getBezierLength = (curve: CubicCoordinates | QuadCoordinates) => {\n const points = [] as unknown as CubicPoints | QuadPoints;\n for (let idx = 0, len = curve.length, step = 2; idx < len; idx += step) {\n points.push({\n x: curve[idx],\n y: curve[idx + 1],\n });\n }\n const dpoints = deriveBezier(points);\n return bezierLength((t: number) => {\n return computeBezier(dpoints[0], t);\n });\n};\n\n// Precision for consider cubic polynom as quadratic one\nconst CBEZIER_MINMAX_EPSILON = 0.00000001;\n\n/**\n * Returns the most extreme points in a Quad Bezier segment.\n * @param A an array which consist of X/Y values\n */\n// https://github.com/kpym/SVGPathy/blob/acd1a50c626b36d81969f6e98e8602e128ba4302/lib/box.js#L89\nconst minmaxQ = ([v1, cp, v2]: [number, number, number]) => {\n const min = Math.min(v1, v2);\n const max = Math.max(v1, v2);\n\n /* istanbul ignore next @preserve */\n if (cp >= v1 ? v2 >= cp : v2 <= cp) {\n // if no extremum in ]0,1[\n return [min, max] as PointTuple;\n }\n\n // check if the extremum E is min or max\n const E = (v1 * v2 - cp * cp) / (v1 - 2 * cp + v2);\n return (E < min ? [E, max] : [min, E]) as PointTuple;\n};\n\n/**\n * Returns the most extreme points in a Cubic Bezier segment.\n * @param A an array which consist of X/Y values\n * @see https://github.com/kpym/SVGPathy/blob/acd1a50c626b36d81969f6e98e8602e128ba4302/lib/box.js#L127\n */\nconst minmaxC = ([v1, cp1, cp2, v2]: [number, number, number, number]) => {\n const K = v1 - 3 * cp1 + 3 * cp2 - v2;\n\n // if the polynomial is (almost) quadratic and not cubic\n /* istanbul ignore next @preserve */\n if (Math.abs(K) < CBEZIER_MINMAX_EPSILON) {\n if (v1 === v2 && v1 === cp1) {\n // no curve, point targeting same location\n return [v1, v2] as PointTuple;\n }\n\n return minmaxQ([v1, -0.5 * v1 + 1.5 * cp1, v1 - 3 * cp1 + 3 * cp2]);\n }\n\n // the reduced discriminant of the derivative\n const T = -v1 * cp2 + v1 * v2 - cp1 * cp2 - cp1 * v2 + cp1 * cp1 + cp2 * cp2;\n\n // if the polynomial is monotone in [0,1]\n if (T <= 0) {\n return [Math.min(v1, v2), Math.max(v1, v2)] as PointTuple;\n }\n const S = Math.sqrt(T);\n\n // potential extrema\n let min = Math.min(v1, v2);\n let max = Math.max(v1, v2);\n\n const L = v1 - 2 * cp1 + cp2;\n // check local extrema\n for (let R = (L + S) / K, i = 1; i <= 2; R = (L - S) / K, i++) {\n // istanbul ignore next @preserve\n if (R > 0 && R < 1) {\n // if the extrema is for R in [0,1]\n const Q = v1 * (1 - R) * (1 - R) * (1 - R) +\n cp1 * 3 * (1 - R) * (1 - R) * R + cp2 * 3 * (1 - R) * R * R +\n v2 * R * R * R;\n if (Q < min) {\n min = Q;\n }\n if (Q > max) {\n max = Q;\n }\n }\n }\n\n return [min, max] as PointTuple;\n};\nconst bezierTools = {\n bezierLength,\n calculateBezier,\n CBEZIER_MINMAX_EPSILON,\n computeBezier,\n Cvalues,\n deriveBezier,\n getBezierLength,\n minmaxC,\n minmaxQ,\n Tvalues,\n};\n\nexport {\n bezierLength,\n bezierTools,\n calculateBezier,\n CBEZIER_MINMAX_EPSILON,\n computeBezier,\n Cvalues,\n deriveBezier,\n getBezierLength,\n minmaxC,\n minmaxQ,\n Tvalues,\n};\n","import { getBezierLength, minmaxC } from \"./bezier\";\nimport { type CubicCoordinates } from \"../types\";\n\n/**\n * Returns a point at a given length of a CubicBezier segment.\n *\n * @param x1 the starting point X\n * @param y1 the starting point Y\n * @param c1x the first control point X\n * @param c1y the first control point Y\n * @param c2x the second control point X\n * @param c2y the second control point Y\n * @param x2 the ending point X\n * @param y2 the ending point Y\n * @param t a [0-1] ratio\n * @returns the point at cubic-bezier segment length\n */\nconst getPointAtCubicSegmentLength = (\n [x1, y1, c1x, c1y, c2x, c2y, x2, y2]: CubicCoordinates,\n t: number,\n) => {\n const t1 = 1 - t;\n return {\n x: t1 ** 3 * x1 + 3 * t1 ** 2 * t * c1x + 3 * t1 * t ** 2 * c2x +\n t ** 3 * x2,\n y: t1 ** 3 * y1 + 3 * t1 ** 2 * t * c1y + 3 * t1 * t ** 2 * c2y +\n t ** 3 * y2,\n };\n};\n\n/**\n * Returns the length of a CubicBezier segment.\n *\n * @param x1 the starting point X\n * @param y1 the starting point Y\n * @param c1x the first control point X\n * @param c1y the first control point Y\n * @param c2x the second control point X\n * @param c2y the second control point Y\n * @param x2 the ending point X\n * @param y2 the ending point Y\n * @returns the CubicBezier segment length\n */\nconst getCubicLength = (\n x1: number,\n y1: number,\n c1x: number,\n c1y: number,\n c2x: number,\n c2y: number,\n x2: number,\n y2: number,\n) => {\n return getBezierLength([x1, y1, c1x, c1y, c2x, c2y, x2, y2]);\n};\n\n/**\n * Returns the point along a CubicBezier segment at a given distance.\n *\n * @param x1 the starting point X\n * @param y1 the starting point Y\n * @param c1x the first control point X\n * @param c1y the first control point Y\n * @param c2x the second control point X\n * @param c2y the second control point Y\n * @param x2 the ending point X\n * @param y2 the ending point Y\n * @param distance the distance to look at\n * @returns the point at CubicBezier length\n */\nconst getPointAtCubicLength = (\n x1: number,\n y1: number,\n c1x: number,\n c1y: number,\n c2x: number,\n c2y: number,\n x2: number,\n y2: number,\n distance?: number,\n) => {\n const distanceIsNumber = typeof distance === \"number\";\n let point = { x: x1, y: y1 };\n /* istanbul ignore else @preserve */\n if (distanceIsNumber) {\n const currentLength = getBezierLength([x1, y1, c1x, c1y, c2x, c2y, x2, y2]);\n if (distance <= 0) {\n // first point already defined\n } else if (distance >= currentLength) {\n point = { x: x2, y: y2 };\n } else {\n point = getPointAtCubicSegmentLength(\n [x1, y1, c1x, c1y, c2x, c2y, x2, y2],\n distance / currentLength,\n );\n }\n }\n return point;\n};\n\n/**\n * Returns the boundig box of a CubicBezier segment in the following format:\n * [MIN_X, MIN_Y, MAX_X, MAX_Y]\n *\n * @param x1 the starting point X\n * @param y1 the starting point Y\n * @param c1x the first control point X\n * @param c1y the first control point Y\n * @param c2x the second control point X\n * @param c2y the second control point Y\n * @param x2 the ending point X\n * @param y2 the ending point Y\n * @returns the extrema of the CubicBezier segment\n */\nconst getCubicBBox = (\n x1: number,\n y1: number,\n c1x: number,\n c1y: number,\n c2x: number,\n c2y: number,\n x2: number,\n y2: number,\n) => {\n const cxMinMax = minmaxC([x1, c1x, c2x, x2]);\n const cyMinMax = minmaxC([y1, c1y, c2y, y2]);\n\n return [cxMinMax[0], cyMinMax[0], cxMinMax[1], cyMinMax[1]] as [\n number,\n number,\n number,\n number,\n ];\n};\n\nconst cubicTools = {\n getCubicBBox,\n getCubicLength,\n getPointAtCubicLength,\n getPointAtCubicSegmentLength,\n};\n\nexport {\n cubicTools,\n getCubicBBox,\n getCubicLength,\n getPointAtCubicLength,\n getPointAtCubicSegmentLength,\n};\n","import { getBezierLength, minmaxQ } from \"./bezier\";\nimport { type QuadCoordinates } from \"../types\";\n\n/**\n * Returns the {x,y} coordinates of a point at a\n * given length of a quadratic-bezier segment.\n *\n * @see https://github.com/substack/point-at-length\n *\n * @param x1 the starting point X\n * @param y1 the starting point Y\n * @param cx the control point X\n * @param cy the control point Y\n * @param x2 the ending point X\n * @param y2 the ending point Y\n * @param t a [0-1] ratio\n * @returns the requested {x,y} coordinates\n */\nconst getPointAtQuadSegmentLength = (\n [x1, y1, cx, cy, x2, y2]: QuadCoordinates,\n t: number,\n) => {\n const t1 = 1 - t;\n return {\n x: t1 ** 2 * x1 + 2 * t1 * t * cx + t ** 2 * x2,\n y: t1 ** 2 * y1 + 2 * t1 * t * cy + t ** 2 * y2,\n };\n};\n\n/**\n * Returns the length of a QuadraticBezier segment.\n *\n * @param x1 the starting point X\n * @param y1 the starting point Y\n * @param cx the control point X\n * @param cy the control point Y\n * @param x2 the ending point X\n * @param y2 the ending point Y\n * @returns the QuadraticBezier segment length\n */\nconst getQuadLength = (\n x1: number,\n y1: number,\n cx: number,\n cy: number,\n x2: number,\n y2: number,\n) => {\n return getBezierLength([x1, y1, cx, cy, x2, y2]);\n};\n\n/**\n * Returns the point along a QuadraticBezier segment at a given distance.\n *\n * @param x1 the starting point X\n * @param y1 the starting point Y\n * @param cx the control point X\n * @param cy the control point Y\n * @param x2 the ending point X\n * @param y2 the ending point Y\n * @param distance the distance to look at\n * @returns the point at QuadraticBezier length\n */\nconst getPointAtQuadLength = (\n x1: number,\n y1: number,\n cx: number,\n cy: number,\n x2: number,\n y2: number,\n distance?: number,\n) => {\n const distanceIsNumber = typeof distance === \"number\";\n let point = { x: x1, y: y1 };\n\n /* istanbul ignore else @preserve */\n if (distanceIsNumber) {\n const currentLength = getBezierLength([x1, y1, cx, cy, x2, y2]);\n if (distance <= 0) {\n // first point already defined\n } else if (distance >= currentLength) {\n point = { x: x2, y: y2 };\n } else {\n point = getPointAtQuadSegmentLength(\n [x1, y1, cx, cy, x2, y2],\n distance / currentLength,\n );\n }\n }\n return point;\n};\n\n/**\n * Returns the boundig box of a QuadraticBezier segment in the following format:\n * [MIN_X, MIN_Y, MAX_X, MAX_Y]\n *\n * @param x1 the starting point X\n * @param y1 the starting point Y\n * @param cx the control point X\n * @param cy the control point Y\n * @param x2 the ending point X\n * @param y2 the ending point Y\n * @returns the extrema of the QuadraticBezier segment\n */\nconst getQuadBBox = (\n x1: number,\n y1: number,\n cx: number,\n cy: number,\n x2: number,\n y2: number,\n) => {\n const cxMinMax = minmaxQ([x1, cx, x2]);\n const cyMinMax = minmaxQ([y1, cy, y2]);\n return [cxMinMax[0], cyMinMax[0], cxMinMax[1], cyMinMax[1]] as [\n number,\n number,\n number,\n number,\n ];\n};\nconst quadTools = {\n getPointAtQuadLength,\n getPointAtQuadSegmentLength,\n getQuadBBox,\n getQuadLength,\n};\n\nexport {\n getPointAtQuadLength,\n getPointAtQuadSegmentLength,\n getQuadBBox,\n getQuadLength,\n quadTools,\n};\n","import distanceSquareRoot from \"./distanceSquareRoot\";\nimport { type PointTuple } from \"../types\";\n\n/**\n * d3-polygon-area\n * https://github.com/d3/d3-polygon\n *\n * Returns the area of a polygon.\n *\n * @param polygon an array of coordinates\n * @returns the polygon area\n */\nconst polygonArea = (polygon: PointTuple[]) => {\n const n = polygon.length;\n let i = -1;\n let a: PointTuple;\n let b = polygon[n - 1];\n let area = 0;\n\n /* eslint-disable-next-line */\n while (++i < n) {\n a = b;\n b = polygon[i];\n area += a[1] * b[0] - a[0] * b[1];\n }\n\n return area / 2;\n};\n\n/**\n * d3-polygon-length\n * https://github.com/d3/d3-polygon\n *\n * Returns the perimeter of a polygon.\n *\n * @param polygon an array of coordinates\n * @returns the polygon length\n */\nconst polygonLength = (polygon: PointTuple[]) => {\n return polygon.reduce((length, point, i) => {\n if (i) {\n return length + distanceSquareRoot(polygon[i - 1], point);\n }\n return 0;\n }, 0);\n};\nconst polygonTools = {\n polygonArea,\n polygonLength,\n};\n\