You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1 line
14 KiB
JavaScript

4 months ago
(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){var bezierCurve=require("../lib/index");window.bezierCurve=bezierCurve},{"../lib/index":4}],2:[function(require,module,exports){"use strict";var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.bezierCurveToPolyline=bezierCurveToPolyline;exports.getBezierCurveLength=getBezierCurveLength;exports["default"]=void 0;var _slicedToArray2=_interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));var _toConsumableArray2=_interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));var sqrt=Math.sqrt,pow=Math.pow,ceil=Math.ceil,abs=Math.abs;var defaultSegmentPointsNum=50;function abstractBezierCurveToPolyline(bezierCurve){var precision=arguments.length>1&&arguments[1]!==undefined?arguments[1]:5;var segmentsNum=bezierCurve.length-1;var startPoint=bezierCurve[0];var endPoint=bezierCurve[segmentsNum][2];var segments=bezierCurve.slice(1);var getSegmentTPointFuns=segments.map(function(seg,i){var beginPoint=i===0?startPoint:segments[i-1][2];return createGetBezierCurveTPointFun.apply(void 0,[beginPoint].concat((0,_toConsumableArray2["default"])(seg)))});var segmentPointsNum=new Array(segmentsNum).fill(defaultSegmentPointsNum);var segmentPoints=getSegmentPointsByNum(getSegmentTPointFuns,segmentPointsNum);var result=calcUniformPointsByIteration(segmentPoints,getSegmentTPointFuns,segments,precision);result.segmentPoints.push(endPoint);return result}function createGetBezierCurveTPointFun(beginPoint,controlPoint1,controlPoint2,endPoint){return function(t){var tSubed1=1-t;var tSubed1Pow3=pow(tSubed1,3);var tSubed1Pow2=pow(tSubed1,2);var tPow3=pow(t,3);var tPow2=pow(t,2);return[beginPoint[0]*tSubed1Pow3+3*controlPoint1[0]*t*tSubed1Pow2+3*controlPoint2[0]*tPow2*tSubed1+endPoint[0]*tPow3,beginPoint[1]*tSubed1Pow3+3*controlPoint1[1]*t*tSubed1Pow2+3*controlPoint2[1]*tPow2*tSubed1+endPoint[1]*tPow3]}}function getTwoPointDistance(_ref,_ref2){var _ref3=(0,_slicedToArray2["default"])(_ref,2),ax=_ref3[0],ay=_ref3[1];var _ref4=(0,_slicedToArray2["default"])(_ref2,2),bx=_ref4[0],by=_ref4[1];return sqrt(pow(ax-bx,2)+pow(ay-by,2))}function getNumsSum(nums){return nums.reduce(function(sum,num){return sum+num},0)}function getSegmentPointsDistance(segmentPoints){return segmentPoints.map(function(points,i){return new Array(points.length-1).fill(0).map(function(temp,j){return getTwoPointDistance(points[j],points[j+1])})})}function getSegmentPointsByNum(getSegmentTPointFuns,segmentPointsNum){return getSegmentTPointFuns.map(function(getSegmentTPointFun,i){var tGap=1/segmentPointsNum[i];return new Array(segmentPointsNum[i]).fill("").map(function(foo,j){return getSegmentTPointFun(j*tGap)})})}function getAllDeviations(segmentPointsDistance,avgLength){return segmentPointsDistance.map(function(seg){return seg.map(function(s){return abs(s-avgLength)})}).map(function(seg){return getNumsSum(seg)}).reduce(function(total,v){return total+v},0)}function calcUniformPointsByIteration(segmentPoints,getSegmentTPointFuns,segments,precision){var rounds=4;var cycles=1;var _loop=function _loop(){var totalPointsNum=segmentPoints.reduce(function(total,seg){return total+seg.length},0);segmentPoints.forEach(function(seg,i){return seg.push(segments[i][2])});var segmentPointsDistance=getSegmentPointsDistance(segmentPoints);var lineSegmentNum=segmentPointsDistance.reduce(function(total,seg){return total+seg.length},0);var segmentlength=segmentPointsDistance.map(function(seg){return getNumsSum(seg)});var totalLength=getNumsSum(segmentlength);var avgLength=totalLength/lineSegmentNum