Please enable JavaScript to use CodeHS

Coding for High School Math

Coding for Trigonometry

Trigonometry connects angles and lengths. Code can draw the unit circle, convert between degrees and radians, and solve triangles step by step. Let us build trig tools one function at a time.

High School Math ~12 min Trigonometry
Right Triangles

SOH CAH TOA

For a right triangle, the three main ratios are:

  • Sine: sin(θ) = opposite / hypotenuse
  • Cosine: cos(θ) = adjacent / hypotenuse
  • Tangent: tan(θ) = opposite / adjacent

If you know one angle (other than the right angle) and one side, you can find every other side. The process is always the same: pick the ratio that uses the side you know and the side you want, then solve for the unknown.

Unit Circle

The unit circle

Imagine a circle with radius 1 centered at the origin. Draw a line from the center at angle θ. The point where it hits the circle has coordinates (cos θ, sin θ).

This is why sine and cosine keep showing up. They are not just triangle ratios; they are the x and y coordinates of a spinning point. When you change the angle, the point moves around the circle.

Key idea: cos θ is the x coordinate and sin θ is the y coordinate on the unit circle.

Radians

Degrees and radians

Most math classes start with degrees, but programming languages use radians. A full circle is 360° or 2π radians. The conversion is simple:

radians = degrees × π / 180

degrees = radians × 180 / π

Almost every trig function in Python needs radians, so a good helper function is deg2rad. You write it once and use it everywhere.

Non-Right Triangles

Law of sines and cosines

SOH CAH TOA only works for right triangles. For any triangle, use the law of sines or the law of cosines.

Law of sines:

a / sin(A) = b / sin(B) = c / sin(C)

Law of cosines:

c² = a² + b² - 2ab cos(C)

The law of cosines is like the Pythagorean theorem with an extra term. When C = 90°, cos(C) = 0 and the formula reduces to c² = a² + b².

Interactive

Interactive trig solver

Enter an angle in degrees. The tool converts it to radians, computes sine and cosine, and draws the point on the unit circle.


    
Code It

Trigonometry in Python

Here are the building blocks: conversion, the unit circle, and the law of cosines. Each function does one small job.

trigonometry.py
import math

def deg2rad(degrees):
    return degrees * math.pi / 180

def unit_circle_point(degrees):
    theta = deg2rad(degrees)
    x = math.cos(theta)
    y = math.sin(theta)
    return (x, y)

def law_of_cosines(a, b, C_degrees):
    C = deg2rad(C_degrees)
    c_squared = a**2 + b**2 - 2 * a * b * math.cos(C)
    return math.sqrt(c_squared)

print(unit_circle_point(45))
print(law_of_cosines(3, 4, 90))
Output
(0.7071..., 0.7071...)
5.0

Each function relies on the one before it. law_of_cosines uses deg2rad so the caller can think in degrees. That is how you build a math library: small functions that compose into bigger tools.

Check Yourself

Quick quiz

1. What does the input to math.sin need to be?

2. On the unit circle, what coordinate gives sin(θ)?

3. The law of cosines becomes the Pythagorean theorem when angle C is:

Keep learning

Trigonometry gave us angles and the unit circle. Next, use code to approximate slopes, areas, and rates in calculus.

Part of the “Coding for High School Math” series.