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.

454 lines
14 KiB
JavaScript

"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.deepClone = deepClone;
exports.eliminateBlur = eliminateBlur;
exports.checkPointIsInCircle = checkPointIsInCircle;
exports.getTwoPointDistance = getTwoPointDistance;
exports.checkPointIsInPolygon = checkPointIsInPolygon;
exports.checkPointIsInSector = checkPointIsInSector;
exports.checkPointIsNearPolyline = checkPointIsNearPolyline;
exports.checkPointIsInRect = checkPointIsInRect;
exports.getRotatePointPos = getRotatePointPos;
exports.getScalePointPos = getScalePointPos;
exports.getTranslatePointPos = getTranslatePointPos;
exports.getDistanceBetweenPointAndLine = getDistanceBetweenPointAndLine;
exports.getCircleRadianPoint = getCircleRadianPoint;
exports.getRegularPolygonPoints = getRegularPolygonPoints;
exports["default"] = void 0;
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
var abs = Math.abs,
sqrt = Math.sqrt,
sin = Math.sin,
cos = Math.cos,
max = Math.max,
min = Math.min,
PI = Math.PI;
/**
* @description Clone an object or array
* @param {Object|Array} object Cloned object
* @param {Boolean} recursion Whether to use recursive cloning
* @return {Object|Array} Clone object
*/
function deepClone(object) {
var recursion = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
if (!object) return object;
var parse = JSON.parse,
stringify = JSON.stringify;
if (!recursion) return parse(stringify(object));
var clonedObj = object instanceof Array ? [] : {};
if (object && (0, _typeof2["default"])(object) === 'object') {
for (var key in object) {
if (object.hasOwnProperty(key)) {
if (object[key] && (0, _typeof2["default"])(object[key]) === 'object') {
clonedObj[key] = deepClone(object[key], true);
} else {
clonedObj[key] = object[key];
}
}
}
}
return clonedObj;
}
/**
* @description Eliminate line blur due to 1px line width
* @param {Array} points Line points
* @return {Array} Line points after processed
*/
function eliminateBlur(points) {
return points.map(function (_ref) {
var _ref2 = (0, _slicedToArray2["default"])(_ref, 2),
x = _ref2[0],
y = _ref2[1];
return [parseInt(x) + 0.5, parseInt(y) + 0.5];
});
}
/**
* @description Check if the point is inside the circle
* @param {Array} point Postion of point
* @param {Number} rx Circle x coordinate
* @param {Number} ry Circle y coordinate
* @param {Number} r Circle radius
* @return {Boolean} Result of check
*/
function checkPointIsInCircle(point, rx, ry, r) {
return getTwoPointDistance(point, [rx, ry]) <= r;
}
/**
* @description Get the distance between two points
* @param {Array} point1 point1
* @param {Array} point2 point2
* @return {Number} Distance between two points
*/
function getTwoPointDistance(_ref3, _ref4) {
var _ref5 = (0, _slicedToArray2["default"])(_ref3, 2),
xa = _ref5[0],
ya = _ref5[1];
var _ref6 = (0, _slicedToArray2["default"])(_ref4, 2),
xb = _ref6[0],
yb = _ref6[1];
var minusX = abs(xa - xb);
var minusY = abs(ya - yb);
return sqrt(minusX * minusX + minusY * minusY);
}
/**
* @description Check if the point is inside the polygon
* @param {Array} point Postion of point
* @param {Array} points The points that makes up a polyline
* @return {Boolean} Result of check
*/
function checkPointIsInPolygon(point, polygon) {
var counter = 0;
var _point = (0, _slicedToArray2["default"])(point, 2),
x = _point[0],
y = _point[1];
var pointNum = polygon.length;
for (var i = 1, p1 = polygon[0]; i <= pointNum; i++) {
var p2 = polygon[i % pointNum];
if (x > min(p1[0], p2[0]) && x <= max(p1[0], p2[0])) {
if (y <= max(p1[1], p2[1])) {
if (p1[0] !== p2[0]) {
var xinters = (x - p1[0]) * (p2[1] - p1[1]) / (p2[0] - p1[0]) + p1[1];
if (p1[1] === p2[1] || y <= xinters) {
counter++;
}
}
}
}
p1 = p2;
}
return counter % 2 === 1;
}
/**
* @description Check if the point is inside the sector
* @param {Array} point Postion of point
* @param {Number} rx Sector x coordinate
* @param {Number} ry Sector y coordinate
* @param {Number} r Sector radius
* @param {Number} startAngle Sector start angle
* @param {Number} endAngle Sector end angle
* @param {Boolean} clockWise Whether the sector angle is clockwise
* @return {Boolean} Result of check
*/
function checkPointIsInSector(point, rx, ry, r, startAngle, endAngle, clockWise) {
if (!point) return false;
if (getTwoPointDistance(point, [rx, ry]) > r) return false;
if (!clockWise) {
var _deepClone = deepClone([endAngle, startAngle]);
var _deepClone2 = (0, _slicedToArray2["default"])(_deepClone, 2);
startAngle = _deepClone2[0];
endAngle = _deepClone2[1];
}
var reverseBE = startAngle > endAngle;
if (reverseBE) {
var _ref7 = [endAngle, startAngle];
startAngle = _ref7[0];
endAngle = _ref7[1];
}
var minus = endAngle - startAngle;
if (minus >= PI * 2) return true;
var _point2 = (0, _slicedToArray2["default"])(point, 2),
x = _point2[0],
y = _point2[1];
var _getCircleRadianPoint = getCircleRadianPoint(rx, ry, r, startAngle),
_getCircleRadianPoint2 = (0, _slicedToArray2["default"])(_getCircleRadianPoint, 2),
bx = _getCircleRadianPoint2[0],
by = _getCircleRadianPoint2[1];
var _getCircleRadianPoint3 = getCircleRadianPoint(rx, ry, r, endAngle),
_getCircleRadianPoint4 = (0, _slicedToArray2["default"])(_getCircleRadianPoint3, 2),
ex = _getCircleRadianPoint4[0],
ey = _getCircleRadianPoint4[1];
var vPoint = [x - rx, y - ry];
var vBArm = [bx - rx, by - ry];
var vEArm = [ex - rx, ey - ry];
var reverse = minus > PI;
if (reverse) {
var _deepClone3 = deepClone([vEArm, vBArm]);
var _deepClone4 = (0, _slicedToArray2["default"])(_deepClone3, 2);
vBArm = _deepClone4[0];
vEArm = _deepClone4[1];
}
var inSector = isClockWise(vBArm, vPoint) && !isClockWise(vEArm, vPoint);
if (reverse) inSector = !inSector;
if (reverseBE) inSector = !inSector;
return inSector;
}
/**
* @description Determine if the point is in the clockwise direction of the vector
* @param {Array} vArm Vector
* @param {Array} vPoint Point
* @return {Boolean} Result of check
*/
function isClockWise(vArm, vPoint) {
var _vArm = (0, _slicedToArray2["default"])(vArm, 2),
ax = _vArm[0],
ay = _vArm[1];
var _vPoint = (0, _slicedToArray2["default"])(vPoint, 2),
px = _vPoint[0],
py = _vPoint[1];
return -ay * px + ax * py > 0;
}
/**
* @description Check if the point is inside the polyline
* @param {Array} point Postion of point
* @param {Array} polyline The points that makes up a polyline
* @param {Number} lineWidth Polyline linewidth
* @return {Boolean} Result of check
*/
function checkPointIsNearPolyline(point, polyline, lineWidth) {
var halfLineWidth = lineWidth / 2;
var moveUpPolyline = polyline.map(function (_ref8) {
var _ref9 = (0, _slicedToArray2["default"])(_ref8, 2),
x = _ref9[0],
y = _ref9[1];
return [x, y - halfLineWidth];
});
var moveDownPolyline = polyline.map(function (_ref10) {
var _ref11 = (0, _slicedToArray2["default"])(_ref10, 2),
x = _ref11[0],
y = _ref11[1];
return [x, y + halfLineWidth];
});
var polygon = [].concat((0, _toConsumableArray2["default"])(moveUpPolyline), (0, _toConsumableArray2["default"])(moveDownPolyline.reverse()));
return checkPointIsInPolygon(point, polygon);
}
/**
* @description Check if the point is inside the rect
* @param {Array} point Postion of point
* @param {Number} x Rect start x coordinate
* @param {Number} y Rect start y coordinate
* @param {Number} width Rect width
* @param {Number} height Rect height
* @return {Boolean} Result of check
*/
function checkPointIsInRect(_ref12, x, y, width, height) {
var _ref13 = (0, _slicedToArray2["default"])(_ref12, 2),
px = _ref13[0],
py = _ref13[1];
if (px < x) return false;
if (py < y) return false;
if (px > x + width) return false;
if (py > y + height) return false;
return true;
}
/**
* @description Get the coordinates of the rotated point
* @param {Number} rotate Degree of rotation
* @param {Array} point Postion of point
* @param {Array} origin Rotation center
* @param {Array} origin Rotation center
* @return {Number} Coordinates after rotation
*/
function getRotatePointPos() {
var rotate = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
var point = arguments.length > 1 ? arguments[1] : undefined;
var origin = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [0, 0];
if (!point) return false;
if (rotate % 360 === 0) return point;
var _point3 = (0, _slicedToArray2["default"])(point, 2),
x = _point3[0],
y = _point3[1];
var _origin = (0, _slicedToArray2["default"])(origin, 2),
ox = _origin[0],
oy = _origin[1];
rotate *= PI / 180;
return [(x - ox) * cos(rotate) - (y - oy) * sin(rotate) + ox, (x - ox) * sin(rotate) + (y - oy) * cos(rotate) + oy];
}
/**
* @description Get the coordinates of the scaled point
* @param {Array} scale Scale factor
* @param {Array} point Postion of point
* @param {Array} origin Scale center
* @return {Number} Coordinates after scale
*/
function getScalePointPos() {
var scale = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [1, 1];
var point = arguments.length > 1 ? arguments[1] : undefined;
var origin = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [0, 0];
if (!point) return false;
if (scale === 1) return point;
var _point4 = (0, _slicedToArray2["default"])(point, 2),
x = _point4[0],
y = _point4[1];
var _origin2 = (0, _slicedToArray2["default"])(origin, 2),
ox = _origin2[0],
oy = _origin2[1];
var _scale = (0, _slicedToArray2["default"])(scale, 2),
xs = _scale[0],
ys = _scale[1];
var relativePosX = x - ox;
var relativePosY = y - oy;
return [relativePosX * xs + ox, relativePosY * ys + oy];
}
/**
* @description Get the coordinates of the scaled point
* @param {Array} translate Translation distance
* @param {Array} point Postion of point
* @return {Number} Coordinates after translation
*/
function getTranslatePointPos(translate, point) {
if (!translate || !point) return false;
var _point5 = (0, _slicedToArray2["default"])(point, 2),
x = _point5[0],
y = _point5[1];
var _translate = (0, _slicedToArray2["default"])(translate, 2),
tx = _translate[0],
ty = _translate[1];
return [x + tx, y + ty];
}
/**
* @description Get the distance from the point to the line
* @param {Array} point Postion of point
* @param {Array} lineBegin Line start position
* @param {Array} lineEnd Line end position
* @return {Number} Distance between point and line
*/
function getDistanceBetweenPointAndLine(point, lineBegin, lineEnd) {
if (!point || !lineBegin || !lineEnd) return false;
var _point6 = (0, _slicedToArray2["default"])(point, 2),
x = _point6[0],
y = _point6[1];
var _lineBegin = (0, _slicedToArray2["default"])(lineBegin, 2),
x1 = _lineBegin[0],
y1 = _lineBegin[1];
var _lineEnd = (0, _slicedToArray2["default"])(lineEnd, 2),
x2 = _lineEnd[0],
y2 = _lineEnd[1];
var a = y2 - y1;
var b = x1 - x2;
var c = y1 * (x2 - x1) - x1 * (y2 - y1);
var molecule = abs(a * x + b * y + c);
var denominator = sqrt(a * a + b * b);
return molecule / denominator;
}
/**
* @description Get the coordinates of the specified radian on the circle
* @param {Number} x Circle x coordinate
* @param {Number} y Circle y coordinate
* @param {Number} radius Circle radius
* @param {Number} radian Specfied radian
* @return {Array} Postion of point
*/
function getCircleRadianPoint(x, y, radius, radian) {
return [x + cos(radian) * radius, y + sin(radian) * radius];
}
/**
* @description Get the points that make up a regular polygon
* @param {Number} x X coordinate of the polygon inscribed circle
* @param {Number} y Y coordinate of the polygon inscribed circle
* @param {Number} r Radius of the polygon inscribed circle
* @param {Number} side Side number
* @param {Number} minus Radian offset
* @return {Array} Points that make up a regular polygon
*/
function getRegularPolygonPoints(rx, ry, r, side) {
var minus = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : PI * -0.5;
var radianGap = PI * 2 / side;
var radians = new Array(side).fill('').map(function (t, i) {
return i * radianGap + minus;
});
return radians.map(function (radian) {
return getCircleRadianPoint(rx, ry, r, radian);
});
}
var _default = {
deepClone: deepClone,
eliminateBlur: eliminateBlur,
checkPointIsInCircle: checkPointIsInCircle,
checkPointIsInPolygon: checkPointIsInPolygon,
checkPointIsInSector: checkPointIsInSector,
checkPointIsNearPolyline: checkPointIsNearPolyline,
getTwoPointDistance: getTwoPointDistance,
getRotatePointPos: getRotatePointPos,
getScalePointPos: getScalePointPos,
getTranslatePointPos: getTranslatePointPos,
getCircleRadianPoint: getCircleRadianPoint,
getRegularPolygonPoints: getRegularPolygonPoints,
getDistanceBetweenPointAndLine: getDistanceBetweenPointAndLine
};
exports["default"] = _default;