Skip to content

Instantly share code, notes, and snippets.

@Mehran1022mm
Last active November 15, 2024 11:14
Show Gist options
  • Save Mehran1022mm/f14e62e5b63a7a541a3bf7a51ec92c0e to your computer and use it in GitHub Desktop.
Save Mehran1022mm/f14e62e5b63a7a541a3bf7a51ec92c0e to your computer and use it in GitHub Desktop.
calculates trigonometric ratios and their inverses.
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const colors = {
blue: "\x1b[34m",
green: "\x1b[32m",
cyan: "\x1b[36m",
magenta: "\x1b[35m",
yellow: "\x1b[33m",
red: "\x1b[31m",
reset: "\x1b[0m"
};
const degreesToRadians = (degrees) => degrees * (Math.PI / 180);
const formatNumber = (num, decimalPlaces = 4) => parseFloat(num.toFixed(decimalPlaces));
function calculateRatios(angle, unit = 'degrees') {
const angleInRadians = unit === 'degrees' ? degreesToRadians(angle) : angle;
const sine = formatNumber(Math.sin(angleInRadians));
const cosine = formatNumber(Math.cos(angleInRadians));
const tangent = Math.abs(angle % 180 === 90) ? "Undefined" : formatNumber(Math.tan(angleInRadians));
const cotangent = tangent === "Undefined" || tangent === 0 ? "Undefined" : formatNumber(1 / Math.tan(angleInRadians));
return { sine, cosine, tangent, cotangent };
}
function calculateInverseRatios(value) {
const radToDeg = 180 / Math.PI;
return {
asin: formatNumber(Math.asin(value) * radToDeg),
acos: formatNumber(Math.acos(value) * radToDeg),
atan: formatNumber(Math.atan(value) * radToDeg)
};
}
const displayResults = (angle, unit, ratios) => {
console.log(`${colors.blue}\nFor an angle of ${angle} ${unit}:${colors.reset}`);
console.log(`${colors.green}Sine: ${ratios.sine}${colors.reset}`);
console.log(`${colors.cyan}Cosine: ${ratios.cosine}${colors.reset}`);
console.log(`${colors.magenta}Tangent: ${ratios.tangent}${colors.reset}`);
console.log(`${colors.yellow}Cotangent: ${ratios.cotangent}${colors.reset}`);
console.log(`${colors.yellow}\nInverse Ratios for Sine (asin), Cosine (acos), Tangent (atan):${colors.reset}`);
console.log(`${colors.green}asin(${ratios.sine}): ${calculateInverseRatios(ratios.sine).asin} degrees${colors.reset}`);
console.log(`${colors.cyan}acos(${ratios.cosine}): ${calculateInverseRatios(ratios.cosine).acos} degrees${colors.reset}`);
console.log(`${colors.magenta}atan(${ratios.tangent !== "Undefined" ? ratios.tangent : 0}): ${calculateInverseRatios(ratios.tangent !== "Undefined" ? ratios.tangent : 0).atan} degrees${colors.reset}`);
};
rl.question("Enter an angle: ", (angleInput) => {
const angle = parseFloat(angleInput);
rl.question("Enter unit (degrees/radians): ", (unitInput) => {
const unit = unitInput.toLowerCase();
if (isNaN(angle) || (unit !== 'degrees' && unit !== 'radians')) {
console.log(`${colors.red}Invalid input. Please provide a valid number for the angle and unit as 'degrees' or 'radians'.${colors.reset}`);
} else {
const ratios = calculateRatios(angle, unit);
displayResults(angle, unit, ratios);
}
rl.close();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment