diff --git a/src/helper/indices.js b/src/helper/indices.js new file mode 100644 index 0000000..0e5f438 --- /dev/null +++ b/src/helper/indices.js @@ -0,0 +1,84 @@ +/** + * + * @param {number} start + * @param {number} end + * @param {number} value + * @param {number} tolerance + * @param {boolean} usePercentage + * @returns {boolean} + */ +function isValueInBounds(start, end, value, tolerance = 0, usePercentage = false) { + if (tolerance !== 0) { + if (usePercentage) { + start = start * (1 - tolerance / 100); + end = end * (1 + tolerance / 100); + } else { + start = start - tolerance; + end = end + tolerance; + } + } + + return value >= start && value <= end; +} + + +/** + * @param {number} x + * @param {number} y + * @param {Map} area + * @param {number} tolerance + * @param {boolean} usePercentage if tolerance is given and this is set to true, + * the tolerance will be calculated by percentage, + * otherwise it will be subtracted/added + * @returns {boolean} + */ +function areXYInArea(x, y, area, tolerance = 0, usePercentage = false) { + return isValueInBounds( + area.get(SideDirections.LEFT), + area.get(SideDirections.RIGHT), + x, + tolerance, + usePercentage + ) && isValueInBounds( + area.get(SideDirections.TOP), + area.get(SideDirections.BOTTOM), + y, + tolerance, + usePercentage + ); +} + +/** + * + * @param {TwoDimPoint} point + * @param {Map} area + * @param {number} tolerance + * @param {boolean} usePercentage if tolerance is given and this is set to true, + * the tolerance will be calculated by percentage, + * otherwise it will be subtracted/added + */ +function isPointInArea(point, area, tolerance = 0, usePercentage = false) { + return areXYInArea( + point.x, + point.y, + area, + tolerance, + usePercentage + ); +} + +/** + * + * @param {HTMLElement} element + * @returns {Object= start && value <= end; -} - - -/** - * @param {number} x - * @param {number} y - * @param {Map} area - * @param {number} tolerance - * @param {boolean} usePercentage if tolerance is given and this is set to true, - * the tolerance will be calculated by percentage, - * otherwise it will be subtracted/added - * @returns {boolean} - */ -function areXYInArea(x, y, area, tolerance = 0, usePercentage = false) { - return isValueInBounds( - area.get(SideDirections.LEFT), - area.get(SideDirections.RIGHT), - x, - tolerance, - usePercentage - ) && isValueInBounds( - area.get(SideDirections.TOP), - area.get(SideDirections.BOTTOM), - y, - tolerance, - usePercentage - ); -} - -/** - * - * @param {TwoDimPoint} point - * @param {Map} area - * @param {number} tolerance - * @param {boolean} usePercentage if tolerance is given and this is set to true, - * the tolerance will be calculated by percentage, - * otherwise it will be subtracted/added - */ -function isPointInArea(point, area, tolerance = 0, usePercentage = false) { - return areXYInArea( - point.x, - point.y, - area, - tolerance, - usePercentage - ); -} - -/** - * - * @param {HTMLElement} element - * @returns {Object