Python Programming Challenge 5: How to do Trigonometry in Python


Today’s challenge involves doing basic trigonometry in Python. We’ll be using Python to calculate the angles of a triangle using the cosine rule.

First, what is cosine rule?

Suppose you have a triangle ABC as shown below:

If you have the lengths of all three sides of the triangle, you can use cosine rule to find any angle you want. Cosine rule states that:

Cosine Rule Formulas

In order to find the angle, you need to take inverse cosine of the fraction on the right. You can do that easily on your calculator. Depending on whether your calculator is in Degrees or Radian mode, your calculator will give you the angle with the corresponding unit.

Cosine Rule Example

For the triangle below, if we want angle B, here’s how we do it:

If your calculator is in degrees mode, you’ll get 27.6 as the answer. On the other hand, if your calculator is in radian mode, you’ll get 0.481. Both answers represent the same angle but with a different unit of measurement.

Our Challenge Today

For today’s challenge, we need to write a program that prompts users to enter the lengths of all three sides of a triangle. Next, we’ll prompt users to indicate which angle they want to find.

Based on the inputs given, we’ll use cosine rule to calculate the angle and return the answer in degrees.

At this point, some of you may be wondering “Does Python use radians or degrees?”.

By default, Python uses radians. Hence, we’ll need to convert the answer into degrees before displaying it to the user.

Video Demonstration

Here’s a video showing how the program works.

Suggested Solution

import math

AB = float(input('Enter length of AB: '))
BC = float(input('Enter length of BC: '))
AC = float(input('Enter length of AC: '))

angleNeeded = input('Angle Needed [A, B or C]: ').upper()

sumOfSquares = {'A':AB*AB + AC*AC, 'B':BC*BC + AB*AB, 'C': AC*AC + BC*BC}
sqOfOppSide = {'A':BC*BC, 'B':AC*AC, 'C':AB*AB}
denominator = {'A': 2*AB*AC, 'B': 2*BC*AB, 'C': 2*AC*BC}
           
fraction = (sumOfSquares[angleNeeded] - sqOfOppSide[angleNeeded])/denominator[angleNeeded]

angle = math.degrees(math.acos(fraction))

print('Angle %s = %.1f degrees' %(angleNeeded, angle))

Dissecting the Code

import math


On line 1, we first import the math module. This is because if you want to use Python to do trigonometry calculations, you need the math module.

Next, we use the input() function to prompt users to enter the lengths of the three sides of the triangle.

AB = float(input('Enter length of AB: '))
BC = float(input('Enter length of BC: '))
AC = float(input('Enter length of AC: '))


As the input() function returns the result as a string, we pass the result of the input() function into the float() function to cast the inputs into floats (e.g. float(input('Enter length of AB: '))).

Finally, we assign the floats to the three corresponding variables AB, AC and BC.


Next, we prompt users to enter the angle they want. We use the upper() function to change the input into uppercase in case users enter the angle in lower case.

angleNeeded = input('Angle Needed [A, B or C]: ').upper()


For instance, if users enter 'a', the upper() function will convert it into 'A'. We need to convert the angle into uppercase as we’ll be using the angle as a dictionary key later.

Once we have collected the information that we need, we are ready to do our calculations.

Here, we declare three variables, sumofSquares, sqOfOppSide and denominator. We’ll use the triangle below to explain what these three variables do.

sumOfSquares = {'A':AB*AB + AC*AC, 'B':BC*BC + AB*AB, 'C': AC*AC + BC*BC}
sqOfOppSide = {'A':BC*BC, 'B':AC*AC, 'C':AB*AB}
denominator = {'A': 2*AB*AC, 'B': 2*BC*AB, 'C': 2*AC*BC}

The sumOfSquares variable stores a dictionary that maps the angle that the user wants (e.g. angle B) into a sum of squares.

For instance, it maps 'B' into BC*BC + AB*AB.

For the triangle above, it maps 'B' into 986. This is calculated by taking 25*25 + 19*19 (BC*BC + AB*AB) and corresponds to the blue box in the diagram.

Next, we have the sqOfOppSide variable. This stores a dictionary that maps the angle that the user wants into a square.

For the triangle above, it maps 'B' into 144. This is calculated by taking 12*12 (AC*AC) and corresponds to the green box in the diagram above.

Finally, we have the denominator variable. This stores a dictionary that maps the angle that the user wants into a product.

For the triangle above, it maps 'B' into 950. This is calculated by taking 2*25*19 (2*BC*AB) and corresponds to the red box in the diagram above.

Next, we use the line below to calculate the fraction that we need in cosine rule.

fraction = (sumOfSquares[angleNeeded] – sqOfOppSide[angleNeeded])/denominator[angleNeeded]


Finally, we use the acos() method in the math module to take inverse cosine of the fraction. However, as Python returns the angle in radians by default, we need to use the degrees() method in the math module to convert the angle into degrees.

angle = math.degrees(math.acos(fraction))


Once that is done, we simply use the line below to print the result.

print('Angle %s = %.1f degrees' %(angleNeeded, angle))
Written by Jamie | Last Updated October 9, 2019

Recent Posts