JavaScript - Reverse of Math.cos(30) - javascript

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>');

Related

Calculate sides of a right angle triangle with JavaScript?

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.

Getting the angle from a direction vector?

I have this simple function to set an angle for a vector. It effectively gets the vector's current magnitude (length), calulates the angle and converts the angle from radians to degrees. Then I apply the angle to X and Y, lastly multiplying the vector by it's original magnitude.
this.setAngle = function(degree){
var l = this.length(); //magnitude of vector
var angle = degree*Math.PI/180; //degress converted to radians
this.x=Math.cos(angle);
this.y=Math.sin(angle);
this.multiply(l); //original magnitude
return;
}
However I am unsure how to obtain (get) an angle from a Vector. Below is my attempt:
this.getAngle = function(){
var angle = Math.atan(this.y/this.x); //radians
var degrees = angle/(180*Math.PI); //degrees
return Math.floor(degrees); //round number, avoid decimal fragments
}
This attempt doesn't return any value except 0 or -1.
Any suggestions?
Edit:
Correct method:
this.getAngle = function(){
var angle = Math.atan2(this.y, this.x);
var degrees = 180 * angle / Math.PI;
return (360 + Math.round(degrees)) % 360;
}
this.getAngle = function(){
var angle = Math.atan2(this.y, this.x); //radians
// you need to devide by PI, and MULTIPLY by 180:
var degrees = 180*angle/Math.PI; //degrees
return (360+Math.round(degrees))%360; //round number, avoid decimal fragments
}

Math.cos - Cant get right answer

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 :)

Calculating the angle from one point to another

So on my canvas I have a large ellipse, and when the user clicks on the canvas a small ellipse should be created on the edge of the large ellipse in the direction of where the click was. The angles are off, and I'm not very confident in the calculations, plus I think the fact that this coordinate system has y increasing when it goes down is screwing it up. Can anyone help me get the desired result?
HTML
<html>
<head>
<script src='processing-1.4.1.min.js'></script>
<script src='jquery-1.9.1.min.js'></script>
</head>
<body>
<canvas id="gamecanvas" data-processing-sources="canvas.pde"></canvas>
</body>
<script>
var gamecanvas = document.getElementById("gamecanvas");
var projectiles = [];
$("#gamecanvas").click(function(e) {
var x = e.clientX - gamecanvas.offsetLeft;
var y = e.clientY - gamecanvas.offsetTop;
var pindex = projectiles.length;
projectiles[pindex] = [];
projectiles[pindex]['angle'] = Math.atan2(y - 200, x - 300) * 180 / Math.PI;
projectiles[pindex]['x'] = 300 + 10 * Math.cos(projectiles[pindex]['angle']);
projectiles[pindex]['y'] = 200 + 10 * Math.sin(projectiles[pindex]['angle']);
});
</script>
</html>
Processing.js Canvas Sketch (Reference)
void draw() {
size(600,400);
background(255,255,255);
fill(#FF0000);
ellipse(300,200,15,15);
for(i = 0;i < projectiles.length;i++) {
ellipse(projectiles[i]['x'],projectiles[i]['y'],2,2);
}
}
You mix radians and degrees here. The JavaScript Math functions that deals with angles needs radian values:
From MDN:
The atan2 method returns a numeric value between -pi and pi
representing the angle theta of an (x,y) point. This is the
counterclockwise angle, measured in radians, between the positive X
axis, and the point (x,y).
And for Math.cos and Math.sin:
A number given in unit of radians.
so you could try with this instead:
/// keep radians, don't convert to degrees
projectiles[pindex]['angle'] = Math.atan2(y - 200, x - 300); // * 180 / Math.PI;
projectiles[pindex]['x'] = 300 + 10 * Math.cos(projectiles[pindex]['angle']);
projectiles[pindex]['y'] = 200 + 10 * Math.sin(projectiles[pindex]['angle']);
Unless you want to keep degrees which in case you need to do this:
projectiles[pindex]['angle'] = Math.atan2(y - 200, x - 300) * 180 / Math.PI;
/// convert degrees back to radians
projectiles[pindex]['x'] =
300 + 10 * Math.cos(projectiles[pindex]['angle'] * Math.PI / 180);
projectiles[pindex]['y'] =
200 + 10 * Math.sin(projectiles[pindex]['angle'] * Math.PI / 180);

Convert complex excel formula to javascript

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);
}

Categories

Resources