diginext-utils
Version:
README.md
93 lines (92 loc) • 3.37 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.distance2Point = exports.angleBetweenPoints = exports.degBetweenPoints = exports.degBetweenPoints360 = exports.clamp = exports.radToDeg = exports.degToRad = exports.randFloat = exports.randInt = exports.randHalt = exports.rand = exports.randRound = void 0;
const DEG2RAD = Math.PI / 180;
const RAD2DEG = 180 / Math.PI;
const randRound = (number) => {
return Math.round(Math.random() * number);
};
exports.randRound = randRound;
const rand = (number) => {
return (Math.random() - Math.random()) * number;
};
exports.rand = rand;
const randHalt = (number) => {
var rand = Math.random() - Math.random();
var res;
if (rand > 0) {
res = rand * (number / 2) + number / 2;
}
else {
res = rand * (number / 2) - number / 2;
}
return Math.abs(res);
};
exports.randHalt = randHalt;
const randInt = (low, high) => {
return low + Math.floor(Math.random() * (high - low + 1));
};
exports.randInt = randInt;
const randFloat = (low, high) => {
return low + Math.random() * (high - low);
};
exports.randFloat = randFloat;
const degToRad = (degrees) => {
return degrees * DEG2RAD;
};
exports.degToRad = degToRad;
const radToDeg = (radians) => {
return radians * RAD2DEG;
};
exports.radToDeg = radToDeg;
const clamp = (value, min, max) => {
const result = Math.max(min, Math.min(max, value));
return Number.isFinite(result) ? result : 0;
};
exports.clamp = clamp;
const degBetweenPoints360 = (cx, cy, ex, ey) => {
let theta = (0, exports.degBetweenPoints)(cx, cy, ex, ey); // range (-180, 180]
if (theta < 0)
theta = 360 + theta; // range [0, 360)
return Number.isFinite(theta) ? theta : 0;
};
exports.degBetweenPoints360 = degBetweenPoints360;
const degBetweenPoints = (cx, cy, ex, ey) => {
const dy = ey - cy;
const dx = ex - cx;
let theta = Math.atan2(dy, dx); // range (-PI, PI]
theta *= 180 / Math.PI; // rads to degs, range (-180, 180]
return Number.isFinite(theta) ? theta : 0;
};
exports.degBetweenPoints = degBetweenPoints;
const angleBetweenPoints = (cx, cy, ex, ey) => {
const dy = ey - cy;
const dx = ex - cx;
let theta = Math.atan2(dy, dx); // range (-PI, PI]
theta *= 180 / Math.PI; // rads to degs, range (-180, 180]
return Number.isFinite(theta) ? theta : 0;
};
exports.angleBetweenPoints = angleBetweenPoints;
const distance2Point = (x1, y1, x2, y2) => {
const dist = Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
return Number.isFinite(dist) ? dist : 0;
};
exports.distance2Point = distance2Point;
const diffDate_1 = require("./diffDate");
const positiveNumber_1 = require("./positiveNumber");
const xmath = {
rand: exports.rand,
randRound: exports.randRound,
randHalt: exports.randHalt,
randInt: exports.randInt,
randFloat: exports.randFloat,
degToRad: exports.degToRad,
radToDeg: exports.radToDeg,
degBetweenPoints360: exports.degBetweenPoints360,
degBetweenPoints: exports.degBetweenPoints,
angleBetweenPoints: exports.angleBetweenPoints,
distance2Point: exports.distance2Point,
diffDate: diffDate_1.diffDate,
positiveNumber: positiveNumber_1.positiveNumber,
};
exports.default = xmath;