org.hermit.utils
Class Angle

java.lang.Object
  extended by org.hermit.utils.Angle
Direct Known Subclasses:
Azimuth

public class Angle
extends java.lang.Object

Utilities for handling and formatting angles, including latitudes and longitudes.

Author:
Ian Cameron Smith

Field Summary
static double HALFPI
          Half pi; a quarter circle in radians; same as 90 degrees.
static double TWOPI
          Two times pi; a circle in radians; same as 360 degrees.
 
Constructor Summary
Angle(double radians)
          Create an Angle from an angle given in radians.
 
Method Summary
 Angle add(double radians)
          Calculate the azimuth which is the given angular offset from this one.
static java.lang.String formatBearing(double val)
          Format an angle as a bearing.
 java.lang.String formatDeg()
          Format this azimuth for user display in degrees.
 java.lang.String formatDegMin()
          Format this azimuth for user display in degrees and minutes.
static java.lang.String formatDegMin(double angle)
          Format an angle for user display in degrees and minutes.
static java.lang.String formatDegMin(double angle, char pos, char neg)
          Format a latitude or longitude angle as a string in the format "W171° 15.165'".
static void formatDegMin(double angle, char pos, char neg, java.lang.StringBuilder sb)
          Format a latitude or longitude angle as a string in the format "W171°15.165'".
 java.lang.String formatDegMinSec()
          Format this azimuth for user display in degrees and minutes.
static java.lang.String formatDegMinSec(double angle)
          Format an angle for user display in degrees and minutes.
static java.lang.String formatDegMinSec(double angle, char posSign, char negSign)
          Format an angle for user display in degrees and minutes.
static java.lang.String formatFloat(double val, int frac)
          Format a floating-point value.
static java.lang.String formatLatLon(double lat, double lon)
          Format a latitude and longitude for user display in degrees and minutes.
static java.lang.String formatRightAsc(double angle)
          Format an angle for user display as a right ascension.
static Angle fromDegrees(double degrees)
          Create a Angle from an angle given in degrees.
static Angle fromDegrees(int d, int m, double s)
          Create a Angle from an angle given in degrees, minutes and seconds.
static Angle fromRightAscension(int rh, int rm, double rs)
          Create a Angle from a right ascension given in hours, minutes and seconds.
 double getDegrees()
          Get the azimuth in degrees.
 double getRadians()
          Get the angle in radians.
static double modPi(double v)
          Return the given value mod PI, with negative values made positive -- in other words, the value put into the range [0 ..
static double modTwoPi(double v)
          Return the given value mod 2*PI, with negative values made positive -- in other words, the value put into the range [0 ..
 java.lang.String toString()
          Format this azimuth as a String.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

HALFPI

public static final double HALFPI
Half pi; a quarter circle in radians; same as 90 degrees.

See Also:
Constant Field Values

TWOPI

public static final double TWOPI
Two times pi; a circle in radians; same as 360 degrees.

See Also:
Constant Field Values
Constructor Detail

Angle

public Angle(double radians)
Create an Angle from an angle given in radians.

Parameters:
radians - Source angle in radians.
Method Detail

fromDegrees

public static Angle fromDegrees(double degrees)
Create a Angle from an angle given in degrees.

Parameters:
degrees - Source angle in degrees.
Returns:
The corresponding Angle.

fromDegrees

public static Angle fromDegrees(int d,
                                int m,
                                double s)
Create a Angle from an angle given in degrees, minutes and seconds.

If any of the parameters is negative, the result is negative.

Parameters:
d - Whole degrees.
m - Minutes.
s - Seconds.
Returns:
The corresponding Angle.

fromRightAscension

public static Angle fromRightAscension(int rh,
                                       int rm,
                                       double rs)
Create a Angle from a right ascension given in hours, minutes and seconds.

If any of the parameters is negative, the result is negative (though they really shouldn't be).

Parameters:
rh - Hours of right ascension.
rm - Minutes of right ascension.
rs - Seconds of right ascension.
Returns:
The corresponding Angle.

getRadians

public final double getRadians()
Get the angle in radians.

Returns:
The angle in radians.

getDegrees

public final double getDegrees()
Get the azimuth in degrees.

Returns:
The azimuth in degrees, clockwise from north. This will be in the range 0 <= degrees < 360.0.

add

public Angle add(double radians)
Calculate the azimuth which is the given angular offset from this one.

Parameters:
radians - Offset to add to this Azimuth, in radians; positive is clockwise from north, may be negative.
Returns:
Azimuth which is equal to this Azimuth plus the given offset. Overflow is taken care of.

modPi

public static final double modPi(double v)
Return the given value mod PI, with negative values made positive -- in other words, the value put into the range [0 .. PI).

Parameters:
v - Input value.
Returns:
v % PI, plus PI if negative.

modTwoPi

public static final double modTwoPi(double v)
Return the given value mod 2*PI, with negative values made positive -- in other words, the value put into the range [0 .. TWOPI).

Parameters:
v - Input value.
Returns:
v % TWOPI, plus TWOPI if negative.

formatDeg

public java.lang.String formatDeg()
Format this azimuth for user display in degrees.

Returns:
The formatted azimuth.

formatDegMin

public java.lang.String formatDegMin()
Format this azimuth for user display in degrees and minutes.

Returns:
The formatted azimuth.

formatDegMinSec

public java.lang.String formatDegMinSec()
Format this azimuth for user display in degrees and minutes.

Returns:
The formatted azimuth.

toString

public java.lang.String toString()
Format this azimuth as a String.

Overrides:
toString in class java.lang.Object
Returns:
This azimuth as a string, in degrees.

formatFloat

public static java.lang.String formatFloat(double val,
                                           int frac)
Format a floating-point value.

Parameters:
val - The value to format.
frac - Maximum number of digits after the point.
Returns:
The formatted value.

formatBearing

public static java.lang.String formatBearing(double val)
Format an angle as a bearing.

Parameters:
val - The value to format.
Returns:
The formatted value.

formatDegMin

public static java.lang.String formatDegMin(double angle)
Format an angle for user display in degrees and minutes. Negative angles are formatted with a "-" sign.

Parameters:
angle - The angle to format.
Returns:
The formatted angle.

formatDegMin

public static java.lang.String formatDegMin(double angle,
                                            char pos,
                                            char neg)
Format a latitude or longitude angle as a string in the format "W171° 15.165'".

Parameters:
angle - Angle to format.
pos - Sign character to use if positive.
neg - Sign character to use if negative.
Returns:
The formatted angle.

formatDegMin

public static void formatDegMin(double angle,
                                char pos,
                                char neg,
                                java.lang.StringBuilder sb)
Format a latitude or longitude angle as a string in the format "W171°15.165'". Place the result in a supplied StringBuilder. The StringBuilder will be set to the required length, 12. For best efficiency, leave it at that length.

Parameters:
angle - Angle to format.
pos - Sign character to use if positive.
neg - Sign character to use if negative.
sb - StringBuilder to write the result into.

formatDegMinSec

public static java.lang.String formatDegMinSec(double angle)
Format an angle for user display in degrees and minutes.

Parameters:
angle - The angle to format.
Returns:
The formatted angle.

formatDegMinSec

public static java.lang.String formatDegMinSec(double angle,
                                               char posSign,
                                               char negSign)
Format an angle for user display in degrees and minutes.

Parameters:
angle - The angle to format.
posSign - Sign to use for positive values; none if null.
negSign - Sign to use for negative values; none if null.
Returns:
The formatted angle.

formatLatLon

public static java.lang.String formatLatLon(double lat,
                                            double lon)
Format a latitude and longitude for user display in degrees and minutes.

Parameters:
lat - The latitude.
lon - The longitude.
Returns:
The formatted angle.

formatRightAsc

public static java.lang.String formatRightAsc(double angle)
Format an angle for user display as a right ascension.

Parameters:
angle - The angle to format.
Returns:
The formatted angle. TODO: units!