@ou-imdt/utils
Version:
Utility library for interactive media development
23 lines (22 loc) • 919 B
JavaScript
/**
* Calculates an angle of a right-angled triangle based on the given type (soh, cah, or toa) and side lengths.
* @param {'soh' | 'cah' | 'toa'} type - The type of angle to calculate ('soh', 'cah', or 'toa').
* @param {object} dimensions - An object containing the known side lengths.
* @param {number} [dimensions.opposite] - The length of the opposite side.
* @param {number} [dimensions.adjacent] - The length of the adjacent side.
* @param {number} [dimensions.hypotenuse] - The length of the hypotenuse.
* @returns {number} The angle in radians.
*/
export default function calculateAngle(type, dimensions) {
const { opposite, adjacent, hypotenuse } = dimensions;
switch(type) {
case 'soh':
return Math.asin(opposite / hypotenuse);
case 'cah':
return Math.acos(adjacent / hypotenuse);
case 'toa':
return Math.atan(opposite / adjacent);
default:
return 0;
}
}