math-base-geometry
Version:
utils for math geometry
105 lines (76 loc) • 2.45 kB
JavaScript
const utils = require("./../utils");
const handleDecimal = utils.handleDecimal;
const isNumber = utils.isNumber;
const radianToDegree = utils.radianToDegree;
function areaBaseHeight(base, height, decimalNr) {
if (!isNumber(base) || !isNumber(height)) {
throw "base and height should be numbers";
}
const formula = 1 / 2 * base * height;
const resp = handleDecimal(formula, decimalNr);
return resp;
}
function areaHeron(a, b, c, decimalNr) {
if (!isNumber(a) || !isNumber(b) || !isNumber(c)) {
throw "a, b and c should be numbers";
}
const sFormula = (a + b + c) / 2;
const s = handleDecimal(sFormula, decimalNr);
const areaFormula = Math.sqrt(s * (s - a) * (s - b) * (s - c));
const resp = handleDecimal(areaFormula, decimalNr);
return resp;
}
/**
*
* @param {(Integer|Double)} side1 - side A|B|C of triangle
* @param {(Integer|Double)} side2 - side A|B|C of triangle
* @param {(Integer|Double)} angle - angle A|B|C of triangle in degree
* @param {Bool} decimalNr - limit deciaml number
*/
function areaTwoSides(side1, side2, angle, decimalNr) {
if (!isNumber(side1) || !isNumber(side2) || !isNumber(angle)) {
throw "side1, side2 and angle should be numbers";
}
const formula = (1 / 2) * side1 * side2 * (Math.sin(radianToDegree(angle)));
const resp = handleDecimal(formula, decimalNr);
return resp;
}
/**
*
* @param {(Integer|Double)} a
* @param {(Integer|Double)} b
* @param {(Integer|Double)} c
* @param {Bool} decimalNr
*/
function perimeter(a, b, c, decimalNr) {
if (!isNumber(a) || !isNumber(b) || !isNumber(c)) {
throw "a, b and c should be numbers";
}
const checkAB = a + b;
const checkBC = b + c;
if (checkAB < c) {
throw "make sure a+b > c";
}
if (checkBC > a) {
throw "make sure b+c > a";
}
const formula = a + b + c;
const resp = handleDecimal(formula, decimalNr);
return resp;
}
function base(area, side, gamma, decimalNr) {
if (!isNumber(area) || !isNumber(side) || !isNumber(gamma)) {
throw "area and height should be numbers";
}
const convertedGamma = Math.sin(radianToDegree(gamma));
const formula = (2 * area) / (side * convertedGamma);
const resp = handleDecimal(formula, decimalNr);
return resp;
}
module.exports = {
areaBaseHeight,
areaHeron,
areaTwoSides,
perimeter,
base
}