1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
package tim.prune.data;
/**
* Class to provide distance constants and functions
*/
public abstract class Distance
{
// Geographical constants
/** Earth radius in metres */
private static final double EARTH_RADIUS_M = 6372795.0;
/**
* Convert the given angle in radians into a distance
* @param inAngDist angular distance in radians
* @param inUnit distance units
* @return distance in specified distance units
*/
public static double convertRadiansToDistance(double inAngDist, Unit inUnit)
{
// Multiply by appropriate factor
return inAngDist * EARTH_RADIUS_M * inUnit.getMultFactorFromStd();
}
/**
* Convert the given distance into an angle in radians
* @param inDist distance to convert in the current distance units
* @param inUnit distance unit
* @return angular distance in radians
*/
public static double convertDistanceToRadians(double inDist, Unit inUnit)
{
// Divide by appropriate factor
return inDist / EARTH_RADIUS_M / inUnit.getMultFactorFromStd();
}
/**
* Convert distance value from one unit to another
* @param inDist distance value
* @param inValueUnit unit in which the given value is represented
* @param inTargetUnit desired unit
* @return value in target units
*/
public static double convertBetweenUnits(double inDist, Unit inValueUnit, Unit inTargetUnit)
{
return inDist / inValueUnit.getMultFactorFromStd() * inTargetUnit.getMultFactorFromStd();
}
/**
* Calculate the number of radians between two points (for distance calculation)
* @param inLatitude1 latitude of first point (in degrees)
* @param inLongitude1 longitude of first point
* @param inLatitude2 latitude of second point
* @param inLongitude2 longitude of second point
* @return angular distance between points in radians
*/
public static double calculateRadiansBetween(double inLatitude1, double inLongitude1,
double inLatitude2, double inLongitude2)
{
final double TO_RADIANS = Math.PI / 180.0;
final double lat1rad = inLatitude1 * TO_RADIANS;
final double lat2rad = inLatitude2 * TO_RADIANS;
final double lon1rad = inLongitude1 * TO_RADIANS;
final double lon2rad = inLongitude2 * TO_RADIANS;
// Formula given by Wikipedia:Great-circle_distance as follows:
// angle = 2 arcsin( sqrt( (sin ((lat2-lat1)/2))^^2 + cos(lat1)cos(lat2)(sin((lon2-lon1)/2))^^2))
double firstSine = Math.sin((lat2rad - lat1rad) / 2.0);
double secondSine = Math.sin((lon2rad - lon1rad) / 2.0);
double term2 = Math.cos(lat1rad) * Math.cos(lat2rad) * secondSine * secondSine;
double answer = 2 * Math.asin(Math.sqrt(firstSine * firstSine + term2));
// phew
return answer;
}
}
|