One Hypotenuse to rule them all

This code snippet was published on .

Here’s a fun little demo of some math I found interesting.

If a right triangle has two side-lengths both equal to √2 / 2, what is the length of its hypotenuse?

We’ll build a function to calculate the length of the hypotenuse given the length of the triangle’s other two sides.

For this, we’ll use a bit of algebra you’re probably quite familiar with:

Pythagorean Theorum

a² + b² = c²

Where c represents the hypotenuse, and a and b the other two sides.

Let’s put everything together and calculate this with some JavaScript:

const side = Math.sqrt(2) / 2

// We’re looking for `c` in the equation:
// a² + b² = c²
const calculateHypotenuse = (a, b) => {
	// Start with getting the square of the sides,
	// `a` and `b`
	const a2 = Math.pow(a, 2)
	const b2 = Math.pow(b, 2)

	// Then, to isolate `c`, take the square root of
	// both sides of the equation
	return Math.sqrt(a2 + b2)

const hypotenuse = calculateHypotenuse(side, side)

Which gives us… 1

Interesting! 🤔