Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I found a formula on the web but i have trouble translating it into a js function.
I tried to find the result without any lucky, see my code so far:

JavaScript
function getRadius(a, b, degrees) {
  var t = degrees * (Math.PI / 180); //radians
  var r = (a * b) / Math.sqrt((b * Math.cos(t))^2 + (a * Math.sin(t))^2);
  alert(r);
}


getRadius(560, 336, 45); // should give a result approx middle of 560 and 336

Can someone help me in building this method or just get the formula correct in JS?
Posted
Comments
Yuriy Loginov 16-Jul-13 17:55pm    
try using Math.pow(2) instead of x^2. check the javascript math API: http://www.w3schools.com/jsref/jsref_obj_math.asp
magnihan 16-Jul-13 18:03pm    
I have not tested this yet, but now i got a 407 result thats indeed in the range i was looking for.

Edited line of code:
var r = (a * b) / Math.sqrt( Math.pow(b * Math.cos(t), 2) + Math.pow(a * Math.sin(t), 2) );
Yuriy Loginov 16-Jul-13 20:06pm    
Another thing you may want to do is to convert your a and b parameters to floating point values. One way to do it is: a = a * 1.0
Sergey Alexandrovich Kryukov 16-Jul-13 20:13pm    
What's wrong with just reading the JavaScript reference?
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900