Created
April 29, 2014 13:23
-
-
Save johntron/11400227 to your computer and use it in GitHub Desktop.
Tabata 29 April 2014 - Trig triangle solver
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.reddit.com/r/dailyprogrammer/comments/2451r5/4282014_challenge_160_easy_trigonometric_triangle/ | |
Solutions: | |
C = 90 | |
f(a,b) = (a**2 + b**2)**-2 = c | |
f(a,c) = (c**2 - a**2)**-2 = b | |
f(b,c) = (c**2 - b**2)**-2 = a | |
*/ | |
var pow = Math.pow; | |
var solvers = [ | |
function (args) { | |
args.C = 90; | |
return args; | |
}, | |
function () { | |
if (!args.c && args.a && args.b) { | |
args.c = pow(pow(args.a, 2) + pow(args.b, 2), 0.5); | |
} | |
return args; | |
}, | |
function () { | |
if (!args.a && args.b && args.c) { | |
args.a = pow(pow(args.c, 2) - pow(args.b, 2), 0.5); | |
} | |
return args; | |
}, | |
function () { | |
if (!args.b && args.a && args.c) { | |
args.b = pow(pow(args.c, 2) - pow(args.a, 2), 0.5); | |
} | |
return args; | |
}, | |
]; | |
function solve(args) { | |
solvers.forEach(function(solver) { | |
args = solver(args); | |
}); | |
return args; | |
} | |
function get_args(str) { | |
str = str.split("\n"); | |
var args = {}, | |
lines = +str[0], | |
line; | |
while (lines > 0) { | |
line = str[str.length - lines]; | |
line = line.split('='); | |
args[line[0]] = +line[1]; | |
lines--; | |
} | |
return args; | |
} | |
problem = "2\na=3\nc=5"; | |
var args = get_args(problem); | |
console.info(args); | |
args = solve(args); | |
console.info(args); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment