netwar.utils
Class Trig
java.lang.Object
|
+--netwar.utils.Trig
- public class Trig
- extends java.lang.Object
This is a purely static class used to perform trigonometric calculations rapidly.
To accomplish this, Trig uses a static array of precalculated results,
and merely performs an array look-up when the actual value is needed.
As a result, Trig.initialize() must be called once before any of the Trig methods are called.
Features like error trapping and automatic initialization at the first use were left out,
because it would slow the performance of the methods, which are intended to be called up to
several thousand times per second.
- Author:
- Group N2 - Project Netwar, Daniel Grund and Kyle Kakligian
|
Field Summary |
static int |
fullCircle
This is the number of angular units in a full circle. |
static int |
right
This is the number of angular units in a right angle. |
|
Constructor Summary |
Trig()
|
|
Method Summary |
static float |
cos(int a)
Returns the cosine of the angle. |
static void |
initialize()
Sets up the Trig lookup table. |
static int |
normalize(int a)
Returns an angle equivalent to a, but guaranteed to be within the range [0, fullCirlce). |
static int |
pi(float factor)
Returns the measure of an angle in angular units (degrees) which has a measure of factor times pi radians. |
static float |
sin(int a)
Returns the sine of the angle. |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
fullCircle
public static final int fullCircle
- This is the number of angular units in a full circle.
Since the value is currently 360, they will be referred to as degrees.
This value can be changed to use another form of angular unit.
Note that the resolution of the sin/cos methods in Trig is equal to one angular unit.
right
public static final int right
- This is the number of angular units in a right angle.
If this is not exactly one fourth of fullCircle, it will cause miscalculations.
This is pre-calculated, to prevent an unnecessary divide from being added to each cos call.
Trig
public Trig()
sin
public static float sin(int a)
- Returns the sine of the angle.
- Parameters:
a - The angle, in degrees.- Returns:
- The sine of a.
- See Also:
normalize(int)
cos
public static float cos(int a)
- Returns the cosine of the angle.
- Parameters:
a - The angle, in degrees.- Returns:
- The cosine of a.
- See Also:
normalize(int)
pi
public static int pi(float factor)
- Returns the measure of an angle in angular units (degrees) which has a measure of factor times pi radians.
Example: a factor of 1 corresponds to an angle of pi radians, which is 180 degrees, so pi(1) returns 180.
- Parameters:
factor - A number to multiply by PI to get the radian measure of an angle.- Returns:
- The degree measure of that angle.
normalize
public static int normalize(int a)
- Returns an angle equivalent to a, but guaranteed to be within the range [0, fullCirlce).
If an angle is less than 0 or greater than or equal to fullCircle,
it will cause an array out of bounds exception when you call Trig.sin or Trig.cos.
Use this method to ensure that your angle can be used with Trig.sin and Trig.cos.
- Parameters:
a - The angle to correct.- Returns:
- The corrected angle.
initialize
public static void initialize()
- Sets up the Trig lookup table.
This must be called once before Trig.sin or Trig.cos can be used without error.