?? trig.java
字號:
/*
Netwar
Copyright (C) 2002 Daniel Grund, Kyle Kakligian, Jason Komutrattananon, & Brian Hibler.
This file is part of Netwar.
Netwar is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Netwar is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Netwar; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package netwar.utils;
/** 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
* @author Daniel Grund
* @author Kyle Kakligian
*/
public class Trig {
/** 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.
*/
public static final int fullCircle = 360;
/** 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.
*/
public static final int right = 90;
private static float sine[] = null;
/** Sets up the Trig lookup table.
*/
static {
int i;
if(sine == null) {
sine = new float[fullCircle];
for(i = 0; i < fullCircle; i++)
sine[i] = (float)Math.sin((float)i / fullCircle * 2 * Math.PI);
}
}
/** Returns the sine of the angle.
* @param a The angle, in degrees.
* @return The sine of a.
* @see normalize(int)
*/
public static float sin(int a) {return sine[a];}
/** Returns the cosine of the angle.
* @param a The angle, in degrees.
* @return The cosine of a.
* @see normalize(int)
*/
public static float cos(int a) {
if(a <= right)
return sine[right - a];
return sine[fullCircle + right - a];
}
/** 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.
* @param factor A number to multiply by PI to get the radian measure of an angle.
* @return The degree measure of that angle.
*/
static public int pi(float factor) {
return (int)((long)(fullCircle * factor / 2) % (fullCircle));
}
/** 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.
* @param a The angle to correct.
* @return The corrected angle.
*/
static public int normalize(int a) {
a %= fullCircle;
if(a < 0) a+= fullCircle;
return a;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -