<< Chapter < Page | Chapter >> Page > |
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.
Figure 18 shows the output produced by this script.
Figure 18 . Output from the code in Listing 9. |
---|
Start Script
adj = 3.00 opp = 4.00 unitsangle = 53.13 units
adj = -3.00 opp = 4.00 unitsangle = 126.87 units
adj = -3.00 opp = -4.00 unitsangle = 233.13 units
adj = 3.00 opp = -4.00 unitsangle = -53.13 units |
This is an issue that will become important when we reach the module that deals with vectors in all four quadrants.
Structure of the script
The script shown in Listing 9 begins by defining a function named getAngle . The purpose of this function is to return an angle in degrees in the correct quadrant based on the lengths of the adjacent and opposite sidesof an enclosing right triangle. As mentioned above, the returned angle is the angle that the hypotenuse makes with the positive horizontal axis.
An indeterminate result
The getAngle function calls the Math.atan method to compute the angle whose tangent is the ratio of the opposite side to the adjacent side of a righttriangle.
If the lengths of both the opposite and adjacent sides are zero, the ratio opposite/adjacent is indeterminate and the value of the angle cannot be computed. In fact there is noangle corresponding to the ratio 0/0. However, the function must either return the value of an angle, or must return some sort of flag indicating thatcomputation of the angle is not possible.
In this case, the function simply returns the value zero for the angle.
Avoiding division by zero
If the length of adjacent side is zero and the length of opposite side is not zero, the ratio opposite/adjacent is infinite. Therefore, the value of the angle cannot becomputed. However, in this case, the angle is known to be 90 degrees (for opposite greater than zero) or 270 degrees (-90 degrees, for opposite less thanzero). The getAngle function traps both of those cases and returns the correct angle in each case.
Correcting for the quadrant
The Math.atan method receives one parameter and it is either a positive or negative value. If the value is positive, the method returns an angle between 0and 90 degrees. If the value is negative, the method returns an angle between 0 and -90 degrees. Thus, the angles returned by the Math.atan method always lie inthe first or fourth quadrants.
(Actually, as I mentioned earlier, +90 degrees and -90 degrees are not possible because the tangent of +90 degrees or -90 degrees is an infinitelylarge positive or negative value. However, the method can handle angles that are very close to +90 or -90 degrees.)
A negative opposite/adjacent ratio
If the opposite/adjacent ratio is negative, this doesn't necessarily mean that the angle lies in the fourth quadrant. That negative ratio could resultfrom a positive value for opposite and a negative value for adjacent. In that case, the angle would lie in the second quadrant between 90 degrees and 180degrees.
Notification Switch
Would you like to follow the 'Contemporary math applications' conversation and receive update notifications?