<< Chapter < Page | Chapter >> Page > |
Algebraic signs versus quadrant number
It is sometimes useful to consider how the algebraic sign of the sine, cosine, and tangent values varies among the four quadrants. Figure 15 contains a table that shows the sign of the sine, cosine, and tangent values for each ofthe four quadrants
Figure 15 . Algebraic signs versus quadrants. |
---|
1 2 3 4
sine + + - -cosine + - - +
tangent + - + - |
Working with arctangents is more difficult than arcsine or arccosine
Working with arctangent is somewhat more difficult than working with arcsine or arccosine, if for no other reason than the possibility of dividing by zerowhen working with the arctangent.
Listing 9 shows a JavaScript function named getAngle that deals with this issue.
Listing 9 . Listing 9: A function to deal with quadrants.
<!---------------- File JavaScript09.html ---------------------><html><body><script language="JavaScript1.3">document.write("Start Script<br/>");
//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(adjacent,opposite){
if((adjacent == 0)&&(opposite == 0)){
//Angle is indeterminate. Just return zero.return 0;
}else if((adjacent == 0)&&(opposite>0)){
//Avoid divide by zero denominator.return 90;
}else if((adjacent == 0)&&(opposite<0)){
//Avoid divide by zero denominator.return -90;
}else if((adjacent<0)&&(opposite>= 0)){
//Correct to second quadrantreturn Math.atan(opposite/adjacent)*180/Math.PI + 180;
}else if((adjacent<0)&&(opposite<= 0)){
//Correct to third quadrantreturn Math.atan(opposite/adjacent)*180/Math.PI + 180;
}else{//First and fourth quadrants. No correction required.
return Math.atan(opposite/adjacent)*180/Math.PI;}//end else
}//end function getAngle//Modify these values and run for different cases.
var adj = 3;var opp = 4;
document.write("adj = " + adj.toFixed(2) +" opp = " + opp.toFixed(2) + " units<br/>");
document.write("angle = " + getAngle(adj,opp).toFixed(2)+ " units<br/>");
var adj = -3;var opp = 4;
document.write("adj = " + adj.toFixed(2) +" opp = " + opp.toFixed(2) + " units<br/>");
document.write("angle = " + getAngle(adj,opp).toFixed(2)+ " units<br/>");
var adj = -3;var opp = -4;
document.write("adj = " + adj.toFixed(2) +" opp = " + opp.toFixed(2) + " units<br/>");
document.write("angle = " + getAngle(adj,opp).toFixed(2)+ " units<br/>");var adj = 3;
var opp = -4;document.write("adj = " + adj.toFixed(2) +
" opp = " + opp.toFixed(2) + " units<br/>");
document.write("angle = " + getAngle(adj,opp).toFixed(2)+ " units<br/>");</script></body></html>
The code in Listing 9 begins by defining a function named getAngle that accepts the signed values of the adjacent side and the opposite side of the righttriangle and returns the angle that the hypotenuse makes with the positive horizontal axis.
Then the code in Listing 9 tests the result for four different triangles situated in each of the four quadrants.
Notification Switch
Would you like to follow the 'Game 2302 - mathematical applications for game development' conversation and receive update notifications?