Saturday, June 7, 2008

Pythagorean Theorem

When you're dealing with right-angled triangles, trigonometry is not required to calculate the length of the hypotenuse. Our PHP programmers friends have a function called hypot() which solves for c in the equation: a^2 + b^2 = c^2


  1. function hypot(a, b)
  2.     hypot = sqr(a^2 + b^2)
  3. end function

Using the classic 3-4-5 triangle as an example, for a = 3 and b = 4, the function will return 5.


View this code on Snipplr

No comments: