Trigonometric MethodsThe Math and StrictMath classes define a number of methods for computing trigonometric and inverse trigonometric functions. A general note about all of the trigonometric methods is that the angles used as arguments or return values are in radians. The Math and StrictMath classes provide the toRadians() and toDegrees() methods to convert from degrees to radians and vice versa.
acos() returns the angle whose cosine is the argument value. The return values will range from 0.0 to p . If the argument is NaN or if its absolute value is greater than 1, the return value will be NaN . asin() returns the angle whose sine is the argument value. The return values will range from “ p /2 to p /2. If the argument is NaN or if its absolute value is greater than 1, the return value will be NaN . atan() returns the angle whose tangent is the argument value. The return values will range from “ p /2 to p /2. If the argument is NaN , the return value will be NaN . cos() returns the cosine of the specified angle. If the argument is NaN or infinity, the return value will be NaN . sin() returns the sine of the specified angle. If the argument is NaN or infinity, the return value will be NaN . tan() returns the tangent of the specified angle. Example: Using the Trigonometric MethodsThe TrigDemo class uses the sin() and cos() methods to uniformly distribute points around a circle. This might be useful, for instance, if you were building a finite-difference grid around a circular shape. This example also makes use of the PI constant defined in the java.Math package. public class TrigDemo { public static void main(String args[]) { int numPoints = 9; double x[] = new double[numPoints]; double y[] = new double[numPoints]; double angle; // Use sin() and cos() methods to uniformly // distribute points around a circle of // radius 1.0 for(int i=0; i<numPoints; ++i) { angle = i*2.0*Math.PI/(numPoints-1); x[i] = Math.cos(angle); y[i] = Math.sin(angle); System.out.println("x[" + i + "] = " + x[i] + " y[" + i + "] = " + y[i]); } } } Output ” x[0] = 1.0 y[0] = 0.0 x[1] = 0.7071067 y[1] = 0.7071067 x[2] = 6.123233e-17 y[2] = 1.0 x[3] = -0.7071067 y[3] = 0.7071067 x[4] = -1.0 y[4] = 1.224625e-16 x[5] = -0.7071067 y[5] = -0.7071067 x[6] = -1.82145e-16 y[6] = -1.0 x[7] = 0.7071067 y[7] = -0.7071067 x[8] = 1.0 y[8] = -2.442487e-16 Figure 16.1 shows the circle grid we created. Of course, if you were really creating a grid over a circular geometry you would most likely use more than nine points. Figure 16.1. Circle grid
|