<< Chapter < Page | Chapter >> Page > |
Again, the getAngle function tests the signs of the values for y and x. If both values are negative, the value returned from the Math.atan method is corrected to place the angle in the third quadrant.
No corrections required...
Finally, if no corrections are required for the quadrant, the getAngle function returns the value returned by the Math.atan method. Note however, that in all cases, the Math.atan method returns the angle in radians. That value is converted to degrees by the getAngle function and the returned value is in degrees.
Positive and negative angles
As you can see from the results of the test shown in Figure 6 , angles in the first, second, and third quadrants are returned as positive angles in degrees.However, angles in the fourth quadrant are returned as negative angles in degrees.
Earlier in this module, I described a problem involving the sum of two displacement vectors. One vectorhad a magnitude of 30 meters with an angle of 0. The other vector had a magnitude of 40 meters with an angle of 135 degrees.
On the basis of a graphic solution, I estimated the length of the resultant vector to be about 28 metersand the angle of the resultant vector to be a little less than 90 degrees. I promised to provide a mathematical solution for that problem later, and thattime has come.
Please copy the code shown in Listing 2 into an html file and open that file in your browser.
Listing 2 . The sum of two vectors.
<!-- File JavaScript02.html --><html><body><script language="JavaScript1.3">//The purpose of this function is to receive the adjacent
// and opposite side values for a right triangle and to// return the angle in degrees in the correct quadrant.
function getAngle(x,y){if((x == 0)&&(y == 0)){
//Angle is indeterminate. Just return zero.return 0;
}else if((x == 0)&&(y>0)){
//Avoid divide by zero denominator.return 90;
}else if((x == 0)&&(y<0)){
//Avoid divide by zero denominator.return -90;
}else if((x<0)&&(y>= 0)){
//Correct to second quadrantreturn Math.atan(y/x)*180/Math.PI + 180;
}else if((x<0)&&(y<= 0)){
//Correct to third quadrantreturn Math.atan(y/x)*180/Math.PI + 180;
}else{//First and fourth quadrants. No correction required.
return Math.atan(y/x)*180/Math.PI;}//end else
}//end function getAngle//Specify the magnitudes and angles of two vectors.
//Convert the angles from degrees to radians.var vecABmag = 30;//at an angle of 0
var vecABang = 0*Math.PI/180;//degrees*radians/degrees=radiansvar vecBCmag = 40;//at an angle of 135
var vecBCang = 135*Math.PI/180;//degrees*radians/degrees=radians//Compute the horizontal and vertical components of each vector.
var vecABh = vecABmag*Math.cos(vecABang);var vecABv = vecABmag*Math.sin(vecABang);
var vecBCh = vecBCmag*Math.cos(vecBCang);var vecBCv = vecBCmag*Math.sin(vecBCang);
//Compute the sums of the horizontal and vertical components from// the two vectors to get the horizontal and vertical component
// of the resultant vector.var vecResultH = vecABh + vecBCh;
var vecResultV = vecABv + vecBCv;//Use the Pythagorean theorem to compute the magnitude of the
// resultant vector.var vecResultMag = Math.sqrt(Math.pow(vecResultH,2) +
Math.pow(vecResultV,2));//Compute the angle of the resultant.
vecResultAng = getAngle(vecResultH,vecResultV);document.write("Hello from JavaScript" + "<br/>")
document.write("vecABv = " + vecABv + "<br/>");
document.write("vecABh = " + vecABh + "<br/>");
document.write("vecBCv = " + vecBCv + "<br/>");
document.write("vecBCh = " + vecBCh + "<br/>");
document.write("vecResultMag = " + vecResultMag + "<br/>");
document.write("vecResultAng = " + vecResultAng + "<br/>");
document.write("The End")</script></body></html>
Notification Switch
Would you like to follow the 'Game 2302 - mathematical applications for game development' conversation and receive update notifications?