Skip to content
Tutorials Center
My learning

Topics

Lesson 38 of 38 · Projects and Practice

Project: Build a Triangle Solver

Build a program that solves any triangle from three known values (SSS, SAS, ASA, AAS, SSA), handles the ambiguous case, tests itself and draws the result.

In this project you will build a small program that solves any triangle. You give it any three values, at least one of them a side, and it returns every side and angle. It will use the law of sines, the law of cosines, the angle sum and the ambiguous-case rules. It also tests itself: every answer is checked against three independent facts. This is a good way to see how the pieces of the course fit together.

Design

Name angles A, B, C and sides a, b, c. The solver sorts the input into cases:

Given Case Method
three sides SSS law of cosines for angles
two angles and a side AAS or ASA third angle, then law of sines
two sides and the angle between them SAS law of cosines for the third side, then SSS
two sides and an angle not between them SSA law of sines, possibly two answers
flowchart TD
    I["Input: 3 values"] --> N{"How many sides?"}
    N -- "3" --> SSS["SSS: law of cosines"]
    N -- "1" --> AAS["AAS / ASA: angle sum + law of sines"]
    N -- "2" --> L{"Is the angle between the two sides?"}
    L -- yes --> SAS["SAS: law of cosines"]
    L -- no --> SSA["SSA: law of sines, 0, 1 or 2 answers"]

How the solver chooses a method.

The solver returns a list of solutions, because SSA can have two (or none).

Step 1: the solver and its self-check

Python triangle_solver: solve any triangle

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Run it and compare with the earlier lessons: the 5-6-7 triangle matches the law of cosines lesson, A = 40°, B = 65°, a = 10 matches the law of sines lesson, and A = 30°, a = 6, b = 10 gives the two ambiguous triangles. The impossible inputs return an empty list, not an exception.

Step 2: random testing

A solver is only trustworthy if it is tested widely. This test builds random triangles, feeds different subsets of three values into the solver, and checks that the original triangle appears among the answers. Copy the solver from Step 1 above the tests (the TryIt blocks are separate programs, so this block repeats a compact version of the code it needs).

Python Self-test with random triangles

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Every subset of data from a real triangle must produce a solution that equals the original triangle, so any failure signals a bug. In SSA, the original triangle should appear among one or two answers.

Step 3: draw the result

Place A at the origin and B at (c, 0). Then C = (b cos A, b sin A). The picture below shows both triangles from the ambiguous case A = 30°, a = 6, b = 10.

Python Draw both ambiguous triangles

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Both triangles share the vertices A and C, and side a = BC = 6 swings to meet the base line in two different places. This is the geometric picture behind the "ambiguous case".

Extensions

  • Add the area (½ab sin C), the perimeter, the circumradius R = a / (2 sin A) and the inradius.
  • Classify the triangle as acute, right or obtuse, and as scalene, isosceles or equilateral.
  • Add a command that accepts bearings and distances instead of sides and angles.
  • Write the solver in JavaScript and compare the results.

Check your understanding

Quick check · 3 questions

of 3 answered You scored / 3 · perfect!

  1. 1. Why does the solver return a list of solutions?

    The ambiguous case means the number of solutions varies, so a list (possibly empty) is the honest return value.
  2. 2. What are the three independent checks used in is_valid?

    The angles must add to 180°, a/sin A must be equal for all sides, and the law of cosines must hold.
  3. 3. Given a, b and the angle A opposite a, which case is it?

    The angle is not between the two given sides: it is opposite one of them, so this is SSA, possibly ambiguous.

Summary

  • A general solver classifies the input as SSS, SAS, ASA/AAS or SSA and applies the law of sines or cosines.
  • SSA returns 0, 1 or 2 solutions, so return a list.
  • Test with independent checks (angle sum, law of sines, law of cosines) and with random triangles.
  • Plot with coordinates: A = (0, 0), B = (c, 0), C = (b cos A, b sin A).

You have reached the end of this slice of the course. Return to the Trigonometry course page for the full list of chapters.