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 :)
Related
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.
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.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>');
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);
Is there a way to translate into javascript a piece of code that will allow me to show map pins around a point taking in consideration a radius ?
var data=[
{long:3,lat:2},
{long:5,lat:2},
{long:2,lat:3}
];
aCoord={long:1,lat:2};
for(var i=0;i<data.length;i++){
if (data[i] is 30 kms far from aCoord)
myMap.addPin(data[i]);
}
myMap.autozoom();
Thank you,
Regards
I came up with this example so you have an idea on how to calculate the points. You'll need to figure out how to do any necessary conversions for lat/lon.
/**
* Returns coordinates for N points around a circle with a given radius from
* the center.
*
* center: array [x, y]
* radius: int
* num_points: int
*/
function get_points_on_circle(center, radius, num_points) {
if (!num_points) num_points = 10;
var interval = Math.PI * 2 / num_points;
points = [];
i = -1;
while (++i < num_points) {
var theta = interval * i,
point = [Math.cos(theta) * radius + center[0], Math.sin(theta) * radius + center[1]];
points.push(point);
}
return points;
}
// Sample usage
var center = [250, 250],
radius = 100,
num_points = 10;
var points = get_points_on_circle(center, radius, num_points);
Test it out (uses Raphael for plotting)
If you are interested in learning a little about the logic:
A radian is a unit of measure for angles. There are a total of 2*PI radians in a circle. Using that fact, you can calculate the angle interval of any number of points on a circle by performing 2*PI/num_points.
When you know the angle interval, you can calculate the angle (theta) of a point on a circle. Once you have theta (the angle), you have polar coordinates (radius,angle). For that to be of any use to us in this problem, you need to convert the polar coordinates into Cartesian coordinates (x,y). You can do that by using the following formulas:
x = cos(theta) * radius
y = sin(theta) * radius
That's pretty much it in a nutshell.