Bookmark this: tool.io/geometry/right-triangle/source-code
Right Triangle Calculator Source Code
Browse the implementation files for Right Triangle Calculator.tool.ts typescript
import { calculateRightTriangle } from '@tool.io/tools/geometry/right-triangle';
const parsePositiveNumber = (input: unknown): number | null => {
if (
input === undefined ||
input === null ||
(typeof input === 'string' && input.trim() === '')
) {
return null;
}
if (typeof input !== 'string' && typeof input !== 'number') return null;
const value = Number(input);
return Number.isFinite(value) && value > 0 ? value : null;
};
export const rightTriangle = (
sideAInput: unknown,
sideBInput: unknown,
): string => {
const sideA = parsePositiveNumber(sideAInput);
const sideB = parsePositiveNumber(sideBInput);
if (sideA === null || sideB === null) return '';
const result = calculateRightTriangle(sideA, sideB);
return result === null ? '' : JSON.stringify(result);
};
export const Tool: TypeTool = {
name: 'Right Triangle Calculator',
slug: 'geometry/right-triangle',
intro: 'Calculate a right triangle’s hypotenuse, area, and perimeter.',
tool: rightTriangle,
i: [
{
example: '3',
help: 'Enter the first perpendicular leg.',
label: 'Side a — Leg',
placeholder: 'Side a',
type: 'number',
},
{
example: '4',
help: 'Enter the second perpendicular leg.',
label: 'Side b — Leg',
placeholder: 'Side b',
type: 'number',
},
],
o: [
{
example: '{"hypotenuse":5,"area":6,"perimeter":12}',
help: 'JSON containing the hypotenuse, area, and perimeter.',
label: 'Result',
placeholder: 'Triangle measurements will appear here...',
type: 'text',
render: 'textarea',
},
],
equation:
'$$c=\\sqrt{a^2+b^2},\\quad A=\\frac{ab}{2},\\quad P=a+b+c$$',
};
export default Tool;
Description
Browse the implementation files for Right Triangle Calculator.