Bookmark this: tool.io/geometry/pythagorean-theorem/source-code
Pythagorean Theorem Calculator Source Code
Browse the implementation files for Pythagorean Theorem Calculator.tool.ts typescript
import {
calculateMissingRightTriangleSide,
type PythagoreanInput,
} from '@tool.io/tools/geometry/pythagorean-theorem';
const isMissing = (input: unknown): boolean =>
input === undefined ||
input === null ||
(typeof input === 'string' && input.trim() === '');
const parseOptionalNumber = (input: unknown): number | undefined | null => {
if (isMissing(input)) return undefined;
if (typeof input !== 'string' && typeof input !== 'number') return null;
const value = Number(input);
return Number.isFinite(value) ? value : null;
};
export const pythagorean = (
sideAInput: unknown,
sideBInput: unknown,
hypotenuseInput: unknown,
): string => {
const values = [sideAInput, sideBInput, hypotenuseInput].map(
parseOptionalNumber,
);
if (values.includes(null)) return '';
const [sideA, sideB, hypotenuse] = values as Array<number | undefined>;
const input: PythagoreanInput = {
...(sideA !== undefined ? { sideA } : {}),
...(sideB !== undefined ? { sideB } : {}),
...(hypotenuse !== undefined ? { hypotenuse } : {}),
};
const result = calculateMissingRightTriangleSide(input);
return result === null ? '' : String(result);
};
export const Tool: TypeTool = {
name: 'Pythagorean Theorem Calculator',
slug: 'geometry/pythagorean-theorem',
intro: 'Calculate a missing side of a right triangle from two known sides.',
tool: pythagorean,
i: [
{
example: '3',
help: 'Enter exactly two side lengths.',
label: 'Side a — Leg',
placeholder: 'Side a',
type: 'number',
},
{
example: '4',
help: 'Enter exactly two side lengths.',
label: 'Side b — Leg',
placeholder: 'Side b',
type: 'number',
},
{
example: '5',
help: 'Enter exactly two side lengths.',
label: 'Side c — Hypotenuse',
placeholder: 'Side c',
type: 'number',
},
],
o: [
{
example: '5',
help: 'The calculated missing side length.',
label: 'Missing Side',
placeholder: 'Missing side length',
type: 'number',
render: 'textarea',
},
],
equation: '$$a^2+b^2=c^2$$',
};
export default Tool;
Description
Browse the implementation files for Pythagorean Theorem Calculator.