Law of Cosines Calculator
Compute a missing side or angle using the Law of Cosines. Enter two sides and the included angle to find the third side, or enter three sides to find an angle.
Law of Cosines Calculator: a web tool that computes a missing side or angle of a triangle using the Law of Cosines.
How this calculator works and why it helps
The Law of Cosines is a core trigonometric identity used to relate the lengths of sides of a triangle to the cosine of one of its angles. In its common form, it states: c² = a² + b² − 2ab·cos(C). This calculator implements that formula in an easy-to-use interface: enter two side lengths and the included angle to find the third side, or enter three side lengths to compute the angle opposite one of them. It also draws a simple scale triangle with Plotly.js so you can immediately see the geometric result.
Who should use it
This tool is ideal for students, teachers, engineers, hobbyists, and anyone who needs a quick, reliable computation. It is particularly handy when solving non-right triangles where the Pythagorean theorem does not apply. It helps verify homework, cross-check manual calculations, and visualize triangles for presentations or learning.
Step-by-step guide: Using the calculator
- Choose the mode. The interface offers two modes: “Find missing side (c)” and “Find angle (C)”.
- For finding a side: enter side a and side b (positive numbers), then the included angle C in degrees. Click Calculate. The calculator uses c² = a² + b² − 2ab·cos(C) and returns c.
- For finding an angle: enter sides a, b, and c. Click Calculate. The calculator computes cos(C) = (a² + b² − c²) / (2ab) and returns the angle in degrees.
- Inspect the result panel for the numeric answer and a short explanation of the formula used.
- View the Plotly visualization below the result. The tool places one side on the x-axis and shows the triangle shape to scale (when possible). If the inputs violate the triangle inequality or lead to impossible values, the calculator warns you.
Practical tips and examples
- When finding a side, ensure the angle you provide is the included angle between the two known sides. An incorrect angle will produce a wrong result.
- Units matter but cancel out: input meters, feet, or any consistent unit for all sides—results keep the same unit.
- Example 1: a = 5, b = 7, C = 60°. The calculator computes c ≈ √(25 + 49 − 70·cos60°) = √(74 − 70·0.5) = √(74 − 35) = √39 ≈ 6.245. Try the sample values.
- Example 2: a = 4, b = 6, c = 7. To find angle C: cos(C) = (16 + 36 − 49)/(2·4·6) = 3/(48) ≈ 0.0625 → C ≈ arccos(0.0625) ≈ 86.42°.
About the visual output (Plotly.js)
The embedded Plotly.js chart provides a minimal, responsive triangle drawing to help you visualize results. It draws the triangle with equal scaling on both axes so angles and shape are preserved. This is helpful to spot impossible triangles that do not render properly because the inputs break geometric constraints.
Accessibility and layout considerations for WordPress
The calculator HTML is sized to fit a standard WordPress content column: it uses width:100% with max-width:760px and responsive behavior for mobile screens. The layout is intentionally compact so it will sit comfortably between two sidebars on common WordPress themes. The background is white and styling is kept simple to blend with most themes. Inputs and controls use native form elements for keyboard and screen-reader friendliness.
Debugging common input errors
- Nonpositive sides: the calculator expects positive values for sides. If you enter zero or negative numbers the tool will prompt you to correct them.
- Angle range: included angles must be between 0° and 180°. Angles outside this range are geometrically invalid.
- Triangle inequality: when computing an angle from three sides, ensure a + b > c, a + c > b, and b + c > a. If the inequality fails, the tool will indicate an impossible triangle.
Implementation notes
- The calculator uses JavaScript for numeric work and Plotly.js for plotting. JavaScript functions convert degrees to radians and back, and apply the Law of Cosines formulas precisely. The calculator returns results to six decimal places for clarity; you can easily tweak precision in the code.
- The visual plot places one side along the x-axis and computes the third vertex coordinates by solving the circle intersection formula. This ensures the triangle shown corresponds to the computed numeric values.
- All computations are done client-side; no server roundtrip is required. That preserves privacy and speed, but remember that computations rely on the browser’s numeric precision.
Disclaimer
This calculator is provided for educational and illustrative purposes only. It may be used to check math work, learn triangle relationships, or visualize geometry. It is not a substitute for professional engineering, surveying, or legal advice. Always validate critical measurements with appropriate tools and experts before relying on them for safety-critical work.
FAQ
Q: Can I use different units for each side?
A: No. Use the same unit for all sides (e.g., meters or feet). Units will cancel algebraically, but mixing units will produce incorrect results.
Q: Why does the calculator sometimes say the triangle is impossible?
A: That happens when inputs violate the triangle inequality or lead to cos values outside [-1,1]. Double-check your side lengths and angle.
Q: Can the calculator find angles other than the included angle C?
A: The current interface computes the angle opposite the specified side using the Law of Cosines. You can rearrange inputs or compute other angles by running the calculation with different side assignments.
Q: Is the plot exact to scale?
A: The plot is to scale based on the numeric results and uses equal axis scaling; however, display rounding and pixel rendering may make small visual differences. For precise drafting use CAD tools.
Q: How do I add this to my WordPress post?
A: Paste the HTML code into a Custom HTML block, or place the file in a theme partial or a shortcode handler. Ensure Plotly.js CDN is accessible (the code uses a CDN link). The code is self-contained (CSS + JS + Plotly CDN) and sized to fit most content columns.