Skip to content

Instantly share code, notes, and snippets.

@gisfromscratch
Created December 10, 2023 15:33
Show Gist options
  • Save gisfromscratch/9525fa83743ef822b5e9b6d2d25a7ed0 to your computer and use it in GitHub Desktop.
Save gisfromscratch/9525fa83743ef822b5e9b6d2d25a7ed0 to your computer and use it in GitHub Desktop.
Solve for any one side of a triangle using the Pythagorean Theorem.
import math
# Function to calculate the hypotenuse
def calculate_hypotenuse(a, b):
return math.sqrt(a**2 + b**2)
# Function to calculate one leg (a)
def calculate_leg_a(c, b):
return math.sqrt(c**2 - b**2)
# Function to calculate the other leg (b)
def calculate_leg_b(c, a):
return math.sqrt(c**2 - a**2)
# Example values
a = 3
b = 4
c = calculate_hypotenuse(a, b)
# Print the results
print(f"The hypotenuse (c) is: {c}")
print(f"The length of leg a is: {calculate_leg_a(c, b)}")
print(f"The length of leg b is: {calculate_leg_b(c, a)}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment