Created
April 28, 2015 16:32
-
-
Save lcdsantos/a9b273e012b9c541262b to your computer and use it in GitHub Desktop.
Get missing parameters from triangle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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