HTML and CSS Reference
In-Depth Information
console.log(Math.atan(0.577) * 180 / Math.PI);
You get back something close to 30. Now, that seems so basic and straightforward, why would you ever
need another function to do the same thing? Well, to answer that, look at the diagram shown in Figure 3-
13.
-1
-1
D
A
26.57
-26.57
26.57
-2
-26.57
2
C
B
1
1
Figure 3-13. Angles in four quadrants
Figure 3-13 shows four different triangles: A, B, C, and D. Triangles A and B have a positive x value, and
triangles C and D extend into negative x dimensions. Likewise, triangles A and D are in the negative y
space, whereas triangles B and C have positive y measurements. So, for the ratios of the four inner
angles, you get the following:
A:-1/2 or -0.5
B: 1/2 or 0.5
C: 1/ -2 or -0.5
D: -1/ -2 or 0.5
Let's say you divide the opposite leg by the adjacent leg and come up with a ratio of 0.5. You feed
that in with Math.atan(0.5) , convert it to degrees, and you get approximately 26.57. But which
triangle are you talking about now: B or D? There is no way of knowing, because they both have a
ratio of 0.5. This might seem like a minor point, but as you see in some real-life examples later in
this chapter, it becomes quite important.
Welcome Math.atan2(y, x) . This is the other arctangent function in JavaScript, and it is quite a
bit more useful than Math.atan(ratio) . And in fact, you will probably use this one exclusively.
This function takes two values: the measurement of the opposite side and the measurement of the
adjacent side. For most purposes, this measurement is the position of an x and y coordinate. A
Search WWH ::




Custom Search