Skip to content

Instantly share code, notes, and snippets.

@lcdsantos
Created April 28, 2015 16:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lcdsantos/a9b273e012b9c541262b to your computer and use it in GitHub Desktop.
Save lcdsantos/a9b273e012b9c541262b to your computer and use it in GitHub Desktop.
Get missing parameters from triangle
// http://www.nayuki.io/page/triangle-solver-javascript
function calcTriangle(angleA, angleB, angleC, sideA, sideB, sideC) {
if (angleA == null) angleA = 180 - angleB - angleC;
if (angleB == null) angleB = 180 - angleC - angleA;
if (angleC == null) angleC = 180 - angleA - angleB;
if (angleA <= 0 || angleB <= 0 || angleC <= 0)
throw status + " - No solution";
var sinA = Math.sin(degToRad(angleA)),
sinB = Math.sin(degToRad(angleB)),
sinC = Math.sin(degToRad(angleC));
// Use law of sines to find sides
var ratio; // side / sin(angle)
if (sideA != null) { ratio = sideA / sinA; }
if (sideB != null) { ratio = sideB / sinB; }
if (sideC != null) { ratio = sideC / sinC; }
if (sideA == null) sideA = ratio * sinA;
if (sideB == null) sideB = ratio * sinB;
if (sideC == null) sideC = ratio * sinC;
return [angleA, angleB, angleC, sideA, sideB, sideC];
}
function degToRad(x) {
return x / 180 * Math.PI;
}
calcTriangle(79, 11, 90, 1080, null, null);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment