To start I'm not that well versed in javascript and I'm trying to convert this complex excel formula to javascript without any luck.
=DEGREES(ASIN(SIN(RADIANS(59.036))*SIN(RADIANS(150))))/2
Here's what I have so far
var x = DEGREES(Math.asin(Math.sin(RADIANS(59.036))*Math.sin(RADIANS(150))))/2
Obviously, DEGREES and RADIANS are wrong and I can't figure what the javascript equivalent would be.
(BTW the correct answer is 5.831)
Well, you could write your own DEGREES and RADIANS functions, which only involve a little math:
function degrees(x) { return x * 180 / Math.PI; }
function radians(x) { return x * Math.PI / 180; }
var x = degrees(Math.asin(Math.sin(radians(59.036))*Math.sin(radians(150))))/2;
Define both functions yourself. They are both trivial to implement.
function DEGREES(radians){
return (radians * 180 / Math.PI);
}
function RADIANS(degrees){
return (degrees / 180 * Math.PI);
}
Related
I am seeking for some already done solution to simulate gravity interaction of n-body system, like our solar system for example, in any programming language (but preferably Javascript or C). I found this question, and it looks like exactly what I need, but, as I see, there's no interoperation between planets theyselves, only between sun and planets. I'm not so bad in JS, but really weak (more lazy) in geometry and gravity, so maybe somebody can give me an advice of how to enhance this code (or make the new one) to be able to simulate not the sun-to-planets, but full n-body gravity interaction? Or maybe you know such a software already written?
Cause as I understand, all I need is to change the code to calculate and display this:
//...
distanceTo: function(p2) {
var dx = p2.position.x - this.position.x,
dy = p2.position.y - this.position.y;
return Math.sqrt(dx ** 2 + dy ** 2);
},
attraction: function(p2) {
var dx = p2.position.x - this.position.x;
var dy = p2.position.y - this.position.y;
var d = Math.sqrt(dx ** 2 + dy ** 2);
this.f = G * (this.mass * p2.mass) / (d ** 2);
var theta = Math.atan2(dy, dx);
var fx = Math.cos(theta) * this.f;
var fy = Math.sin(theta) * this.f;
this.velocity.x += fx / this.mass;
this.velocity.y += fy / this.mass;
this.position.x += this.velocity.x;
this.position.y += this.velocity.y;
}
//...
for every single body in the system, but I'm not sure is it the right way to just iterate this code over bodies, due to such an iteration will dynamically change bodies' positions, which have been used for previous calculations within the iteration. Hope I spell it clear.
Is this possible at all? Is not this related to Three-body problem exponential computation complexity increasing? If not, can you please just direct me to right formulas and spell out simply for me, what to write and how?
Thanks!
If I did not know that side AB was 489.84 or that side BC was 12.66, how could I calculate these two lengths with JavaScript given I had all the other information?
Use the Math.sin and Math.cos functions. Note: these functions accept radians, you would thus need to convert degrees by using rad = deg * Math.PI/180:
Math.cos(88.52 * Math.PI/180) * 490; // 12.655720238100102
Math.sin(88.52 * Math.PI/180) * 490; // 489.83653676022874
Sin(angle) = opposite / hypotenuse
So
opposite = Sin(angle) * hypotenuse
Therefore...
<script>
var angle = 88.52;
var angleInRadians = angle * Math.PI / 180;
var hypotenuse = 490;
var opposite = Math.sin(angleInRadians) * hypotenuse;
console.log('Opposite: ' + opposite);
console.log('Opposite (to 2 decimal places): ' + opposite.toFixed(2));
</script>
You can get the equivalent for the bottom value by using Math.cos instead of Math.sin, of course.
Math.acos(Math.cos(30)) will not return 30, but Math.acos(Math.cos(0.7)) will return 0.7... How can I do it correctly?
It is because the input/parameter to the cos function should be in radians not in degrees.
From MDN docs:
Parameters
x : A number given in unit of radians.
So, before making call to the function, convert the input to radians.
Make use of formula Radians = Degrees * ( Pi / 180)
Convert 30 degrees to radians
var radians = 30 * Math.PI / 180;
document.write(radians);
var result = Math.cos(radians);
var andBackToRadians = Math.acos(result);
document.write('<p>'+result+'</p>');
document.write('<p>' + andBackToRadians + '</p>');
I am trying to convert the result of cosA which is 0.25 to Degrees. The answer should be 75.5... but I am getting 0.9... Can someone help me please ?
Here is my code
var b = 6;
var a = 8;
var c = 7;
var cosA = ((b*b)+(c*c)-(a*a))/(2*b*c);
console.log(cosA);
cosA = Math.cos(cosA);
console.log(cosA);
wht you need is
Math.acos(0.25) * 180/Math.PI
the * 180 / Math.PI converts radians to degrees
Math.acos(0.25) * 180/Math.PI
This will return you inverse cosine transform and we multiple it by the 180/pi to convert radian to degrees :)
Is there an easy way to connect an LFO directly to a panner node to automate x,y, or z? Like osc.connect(pannerNode.position.x)?
Or would it be better to just use a channelSplitter and handle left/right separately?Alternately I could input an LFO into a ScriptProcessorNode and then set the panner's x,y,z with the following, but wasn't sure if there was a better way:
function pan(range) {
var xDeg = parseInt(range.value);
var zDeg = xDeg + 90;
if (zDeg > 90) {
zDeg = 180 - zDeg;
}
var x = Math.sin(xDeg * (Math.PI / 180));
var z = Math.sin(zDeg * (Math.PI / 180));
p.setPosition(x, 0, z);
}
How to create very basic left/right equal power panning with createPanner();
No, I'm afraid there is not. I believe there's an open issue on this - that xyz should be audioparams.