concept angle in category python

appears as: ngles, gle, n angle, The gle, gles, ngle
Math for Programmers: 3D graphics, machine learning, and simulations with Python

This is an excerpt from Manning's book Math for Programmers: 3D graphics, machine learning, and simulations with Python.

Figure 2.24: Using a protractor to measure the angle at which a vector points

This many radians is about the same as 123.7°, which we can confirm to be correct using a protractor. But this is only by happenstance; there are other angles that could have given us the same cosine. For instance, (-2, -3) also has distance from the origin, so it lies at an angle with the same cosine as θ: -2. To find the value of θ that we actually want, we’ll have to make sure the sine and cosine agree with our expectation. The angle returned by Python, which is approximately 2.159, satisfies this:

>> cos(2.1587989303424644) 
  -0.5547001962252293 
  >>> -2/sqrt(13) 
  -0.5547001962252291 
  >>> sin(2.1587989303424644) 
  0.8320502943378435 
  >>> 3/sqrt(13) 
  0.8320502943378437

None of the arcsine, arccosine, or arctangent functions are sufficient to find the angle to a point in the plane. It is possible to find the correct angle by a tricky geometric argument you probably learned in high school trigonometry class. I’ll leave that as an exercise and cut to the chase—Python can do the work for you! The math.atan2 function takes the Cartesian coordinates of a point in the plane (in reverse order!) and gives you back the angle at which it lies. For example,

Exercise 2.35: Locate the angle 10π/6. Do you expect the values of cos(10π/6) and sin(10π/6) to be positive or negative? Use Python to calculate their values and confirm.

Solution: A whole circle is 2π radians, so the angle π/6 is one twelfth of a circle. You can picture cutting a pizza in 12 slices, and counting counterclockwise from the positive x-axis, the angle 10π/6 is two slices short of a full rotation. This means that it points down and to the right. The cosine should be positive, and the sine should be negative because the distance in this direction corresponds with a positive horizontal displacement and a negative vertical displacement: