hockey.api
Class Util

java.lang.Object
  |
  +--hockey.api.Util

public class Util
extends java.lang.Object

The class Util contains methods for performing basic numeric operations such as the elementary squaring, euclidean distance, angle conversion, trigonometric functions in degrees and clamping.

Many of the Util functions simply delegate to the equivalent functions in Math for their implementations.

See Also:
Math

Constructor Summary
Util()
           
 
Method Summary
static double clamp(double a, double x, double b)
          Clamps a double value to a given range.
static int clamp(int a, int x, int b)
          Clamps an int value to a given range.
static double clampAbs(double x, double a)
          Clamps a double value between zero and a given maximum value.
static int clampAbs(int x, int a)
          Clamps an int value to a given maximum absolute value.
static double clampPos(double x, double b)
          Clamps a double value between zero and a given maximum value.
static int clampPos(int x, int b)
          Clamps an int value between zero and a given maximum value.
static int collisionHeading(IObject target, IObject origin, int speed)
          Calculate the heading for collision from origin to target travelling at speed speed relative to the ice.
static double cosd(double deg)
          Returns the trigonometric cosine of an angle given in degrees.
static double dangle(double v1, double v2)
          Returns the minimal angular distance between two angles in degrees.
static double datan2(double y, double x)
          Returns the angle in degrees to a point (x,y).
static double datan2(IObject to, IObject origin)
          Returns the absolute angle in degrees to an IObject with another IObject as origin.
static double deg(double rad)
          Converts an angle measured in radians to the equivalent angle measured in degrees.
static double dist(double x, double y)
          Returns the distance from the origin to a coordinate.
static double dist(IObject o1, IObject o2)
          Returns the distance between two IObjects.
static double dist2(double x, double y)
          Returns the squared distance from the origin to a double coordinate.
static int dist2(int x, int y)
          Returns the squared distance from the origin to an int coordinate.
static int dist2(IObject o1, IObject o2)
          Returns the squared distance between two IObjects.
static double rad(double deg)
          Converts an angle measured in degrees to the equivalent angle measured in radians.
static double sind(double deg)
          Returns the trigonometric sine of an angle given in degrees.
static double solve(double a, double b, double c, boolean pos)
          Solve ax^2 + 2bx + c = 0 for x.
static double sqr(double x)
          Returns the square of a double value.
static int sqr(int x)
          Returns the square of an int value.
static double tand(double deg)
          Returns the trigonometric tangent of an angle given in degrees.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Util

public Util()
Method Detail

sqr

public static int sqr(int x)
Returns the square of an int value.

Parameters:
x - an int value.
Returns:
the square of x.

sqr

public static double sqr(double x)
Returns the square of a double value.

Parameters:
x - a double value.
Returns:
the square of x.

dist

public static double dist(double x,
                          double y)
Returns the distance from the origin to a coordinate.

Parameters:
x - the x-coordinate.
y - the y-coordinate.
Returns:
the distance from the origin to (x, y).

dist

public static double dist(IObject o1,
                          IObject o2)
Returns the distance between two IObjects.

Parameters:
o1 - the first IObject.
o2 - the second IObject.
Returns:
the distance between the two IObjects.

dist2

public static double dist2(double x,
                           double y)
Returns the squared distance from the origin to a double coordinate.

Parameters:
x - the x-coordinate.
y - the y-coordinate.
Returns:
the squared distance from the origin to (x, y).

dist2

public static int dist2(int x,
                        int y)
Returns the squared distance from the origin to an int coordinate.

Parameters:
x - the x-coordinate.
y - the y-coordinate.
Returns:
the squared distance from the origin to (x, y).

dist2

public static int dist2(IObject o1,
                        IObject o2)
Returns the squared distance between two IObjects.

Parameters:
o1 - the first IObject.
o2 - the second IObject.
Returns:
the distance between the two IObjects.

dangle

public static double dangle(double v1,
                            double v2)
Returns the minimal angular distance between two angles in degrees. Return values are in the range -180 to 180. This method is the same as calling Math.IEEEremainder(v1 - v2, 360).

Parameters:
v1 - the first angle, in degrees.
v2 - the second angle, in degrees.
Returns:
the minimal angular distance between the two angles, in degrees.

rad

public static double rad(double deg)
Converts an angle measured in degrees to the equivalent angle measured in radians. This method is the same as calling Math.toRadians(deg).

Parameters:
deg - an angle, in degrees.
Returns:
the measurement of the angle deg in radians.

deg

public static double deg(double rad)
Converts an angle measured in radians to the equivalent angle measured in degrees. This method is the same as calling Math.toDegrees(rad).

Parameters:
rad - an angle, in radians.
Returns:
the measurement of the angle rad in degrees.

cosd

public static double cosd(double deg)
Returns the trigonometric cosine of an angle given in degrees. This method is the same as calling Math.cos(rad(deg)).

Parameters:
deg - the angle, in degrees.
Returns:
the cosine of the argument.

sind

public static double sind(double deg)
Returns the trigonometric sine of an angle given in degrees. This method is the same as calling Math.sin(rad(deg)).

Parameters:
deg - the angle, in degrees.
Returns:
the sine of the argument.

tand

public static double tand(double deg)
Returns the trigonometric tangent of an angle given in degrees. This method is the same as calling Math.tan(rad(deg)).

Parameters:
deg - the angle, in degrees.
Returns:
the tangent of the argument.

datan2

public static double datan2(double y,
                            double x)
Returns the angle in degrees to a point (x,y). This method is the same as calling deg(Math.atan2(y, x)). Note that the point is given with the y-coordinate first, as for Math.atan2.

Parameters:
y - the y-coordinate of the point.
x - the x-coordinate of the point.
Returns:
the angle from the origin to the point, in degrees.

datan2

public static double datan2(IObject to,
                            IObject origin)
Returns the absolute angle in degrees to an IObject with another IObject as origin. The angle is absolute in the x-y-coordinate system of the IObjects, the heading of the origin IObject is not considered.

Parameters:
to - the IObject to get the absolute angle to.
origin - the IObject to use as origin.
Returns:
the absolute angle to to with origin as origin.

clamp

public static int clamp(int a,
                        int x,
                        int b)
Clamps an int value to a given range. The returned value is a if x<a, x if it is in the range, or b if x>b. The metod is the same as calling Math.min(Math.max(a, x), b).

Parameters:
a - the minimum value of the range.
x - the int value.
b - the maximum value of the range.
Returns:
the value x clamped to the range a-b.

clampPos

public static int clampPos(int x,
                           int b)
Clamps an int value between zero and a given maximum value. The returned value is 0 if x<0, x if it is in the range, or b if x>b. The metod is the same as calling clamp(0, x, b).

Parameters:
x - the int value.
b - the maximum value of the range.
Returns:
the value x clamped to the range 0-b.

clampAbs

public static int clampAbs(int x,
                           int a)
Clamps an int value to a given maximum absolute value. The returned value is -a if x<-a, x if it is in the range, or a if x>a. The metod is the same as calling clamp(-a, x, a).

Parameters:
x - the int value.
a - the maximum absolute value.
Returns:
the value x clamped to the range -a-a.

clamp

public static double clamp(double a,
                           double x,
                           double b)
Clamps a double value to a given range. The returned value is a if x<a, x if it is in the range, or b if x>b. The metod is the same as calling Math.min(Math.max(a, x), b).

Parameters:
a - the minimum value of the range.
x - the int value.
b - the maximum value of the range.
Returns:
the value x clamped to the range a-b.

clampPos

public static double clampPos(double x,
                              double b)
Clamps a double value between zero and a given maximum value. The returned value is 0 if x<0, x if it is in the range, or b if x>b. The metod is the same as calling clamp(0, x, b).

Parameters:
x - the int value.
b - the maximum value of the range.
Returns:
the value x clamped to the range 0-b.

clampAbs

public static double clampAbs(double x,
                              double a)
Clamps a double value between zero and a given maximum value. The returned value is 0 if x<0, x if it is in the range, or a if x>a. The metod is the same as calling clamp(-a, x, a).

Parameters:
x - the int value.
a - the maximum absolute value.
Returns:
the value x clamped to the range -a-a.

solve

public static double solve(double a,
                           double b,
                           double c,
                           boolean pos)
Solve ax^2 + 2bx + c = 0 for x.

Parameters:
a - the quadratic factor.
b - half the linear factor.
c - the constant factor.
pos - determines whether to return the + solution.
Returns:
(-b +/- sqrt(b*b - a*c)) / a or NaN if there is no solution.

or -c / 2 / b if a + solution is requested and the equation is (nearly) linear.


collisionHeading

public static int collisionHeading(IObject target,
                                   IObject origin,
                                   int speed)
Calculate the heading for collision from origin to target travelling at speed speed relative to the ice.

Parameters:
target - the target.
origin - the origin.
speed - the speed.
Returns:
the collision heading or the target heading if no collision possible.