All Downloads are FREE. Search and download functionalities are using the official Maven repository.

uk.org.retep.util.unit.AngleUnit Maven / Gradle / Ivy

The newest version!
/*
 * 

Copyright (c) 1998-2010, Peter T Mount
* All rights reserved.

* *

Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met:

* *
    *
  • Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer.
  • * *
  • Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution.
  • * *
  • Neither the name of the retep.org.uk nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission.
  • * *
* *

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/ package uk.org.retep.util.unit; /** * A unit of measuring and angle, with the ability to convert into other standard * units of measuring angles. * @since 7.1 * @author peter */ public enum AngleUnit { /** *

* The angular mil is commonly used by military organisations. Its relationship to * the radian gives rise to the handy property that object of size s that subtends * an angle ? angular mils is at a distance d = 1000s/?. Alternatively, if the * distance is known, we can determine the size of an object by s = ?d/1000. * The practical form of this that is easy to remember is: * 1 mil at 1 km is about 1 metre. * Another example: 100 mils at 2 km is about 200 metres. *

*

* However, military mils are fixed angles not based on the above formula. *

*

* In the general case, where neither the distance nor the object size is known, the formulae may be of little use. In practice, sizes of observed objects are known with reasonable accuracy since they are often people, buildings and vehicles. Using the formulae, distances of the objects can be readily calculated without a calculator. In military terms, distances are of course essential for artillery bombardments and estimations of journey times. *

*

* Many telescopic sights used on rifles have reticles that are marked in angular mils, and these are generally called mil dot scopes. The mil dots serve two purposes, range estimation and trajectory correction. By determining how many angular mils an object of known size subtends, the distance to that object can be estimated with a fair degree of accuracy. Once the distance is known, the drop of the bullet at that range (see external ballistics), converted back into angular mils, can be used to adjust the aiming point. Generally mil dot scopes have both horizontal and vertical crosshairs marked; the horizontal and vertical marks are used for range estimation and the vertical marks for bullet drop compensation. Skilled shooters, however, can also use the horizontal dots to compensate for bullet drift due to wind. Mil dot scopes are most suited for long shots under uncertain conditions, such as those encountered by military snipers and varmint hunters. In both of those cases, the range to the target is not fixed and shots are taken at extreme ranges, so accurate compensation for bullet drop is required. *

* @since 7.1 * @see Wikipedia Article */ ANGULAR_MIL() { public double convert( double a, AngleUnit u ) { return u.toAngularMil( a ); } public double toRadian( double a ) { return Math.PI * a / 3200.0; } public double toDegree( double a ) { return ( 9.0 * a * Math.PI ) / ( 160.0 * Math.PI ); } public double toMinuteOfArc( double a ) { return ( 27.0 * a * Math.PI ) / ( 8.0 * Math.PI ); } public double toArcSecond( double a ) { return ( 405.0 * a * Math.PI ) / ( 2.0 * Math.PI ); } public double toGradian( double a ) { return a; } public double toAngularMil( double a ) { return a / 16.0; } public String getName() { return "angular mil"; } public String unitString() { return "\u00B5"; } public char unitChar() { return '\u00B5'; } }, /** * The subdivision of the minute of arc is the arcsecond. * There are 60 arcseconds in an arcminute. * @since 7.1 * @see Wikipedia Article */ ARC_SECOND() { public double convert( double a, AngleUnit u ) { return u.toArcSecond( a ); } public double toRadian( double a ) { return a * Math.PI / 648000.0; } public double toDegree( double a ) { return a / 3600.0; } public double toMinuteOfArc( double a ) { return a / 60.0; } public double toArcSecond( double a ) { return a; } public double toGradian( double a ) { return a / 3240.0; } public double toAngularMil( double a ) { return a / 51840.0; } public String getName() { return "arc second"; } /** * The string representation of this unit as a unicode string. * @return string representation of this unit as a unicode string */ public String unitString() { return "\""; } /** * The unicode character representing this unit. If there is no character defined * in the unicode standard, then UNICODE_UNSUPPORTED is returned. * @return character representing this unit */ public char unitChar() { return '\u2033'; } }, /** * A degree (in full, a degree of arc, arc degree, or arcdegree), usually * symbolized \u00B0, is a measurement of plane angle, representing 1/360 * of a full rotation. When that angle is with respect to a reference meridian, * it indicates a location along a great circle of a sphere * (such as Earth, Mars, or the celestial sphere) * @since 7.1 * @see Wikipedia Article */ DEGREE() { public double convert( double a, AngleUnit u ) { return u.toDegree( a ); } public double toRadian( double a ) { return a * Math.PI / 180.0; } public double toDegree( double a ) { return a; } public double toMinuteOfArc( double a ) { return a * 60.0; } public double toArcSecond( double a ) { return a * 3600.0; } public double toGradian( double a ) { return 10.0 * a / 9.0; } public double toAngularMil( double a ) { return 160.0 * a / 9.0; } public String getName() { return "degree (of arc)"; } /** * The string representation of this unit as a unicode string. * @return string representation of this unit as a unicode string */ public String unitString() { return "\u00B0"; } /** * The unicode character representing this unit. If there is no character defined * in the unicode standard, then UNICODE_UNSUPPORTED is returned. * @return character representing this unit */ public char unitChar() { return '\u00B0'; } public String unicodeString() { return "\u00B0"; } }, /** * The grad is a unit of plane angle, equivalent to 1/400 of a full circle, * dividing a right angle in 100. It is also known as gon, grade, or gradian * (not to be confused with grade of a slope, nor with gradient). * One grad equals 9/10 of a degree or PI/200 of a radian. * @since 7.1 * @see Wikipedia Article */ GRADIAN() { public double convert( double a, AngleUnit u ) { return u.toGradian( a ); } public double toRadian( double a ) { return Math.PI * a / 200.0; } public double toDegree( double a ) { return 9.0 * a / 10.0; } public double toMinuteOfArc( double a ) { return 54.0 * a; } public double toArcSecond( double a ) { return 3240.0 * a; } public double toGradian( double a ) { return a; } public double toAngularMil( double a ) { return a / 16.0; } public String getName() { return "gradian"; } public String unitString() { return "gr"; } public char unitChar() { return UNICODE_UNSUPPORTED; } }, /** * A minute of arc, arcminute, or MOA is a unit of angular measurement, equal to * one sixtieth (1/60) of one degree. Since one degree is defined as one three * hundred sixtieth (1/360) of a circle, 1 MOA is 1/21600 of the amount of arc in * a closed circle, or (?/10800) radians. * * It is used in those fields which require a unit for the expression of very small * amounts of arc, such as astronomy. * @since 7.1 * @see Wikipedia Article */ MINUTE_OF_ARC() { public double convert( double a, AngleUnit u ) { return u.toMinuteOfArc( a ); } public double toRadian( double a ) { return Math.PI / 10800.0; } public double toDegree( double a ) { return a / 60.0; } public double toMinuteOfArc( double a ) { return a; } public double toArcSecond( double a ) { return a * 60.0; } public double toGradian( double a ) { return a / 54.0; } public double toAngularMil( double a ) { return 8.0 * a / 27.0; } public String getName() { return "minutes of arc"; } /** * The string representation of this unit as a unicode string. * @return string representation of this unit as a unicode string */ public String unitString() { return "'"; } /** * The unicode character representing this unit. If there is no character defined * in the unicode standard, then UNICODE_UNSUPPORTED is returned. * @return character representing this unit */ public char unitChar() { return '\u2032'; } public String unicodeString() { return "\u2032"; } }, /** *

* The radian is a unit of plane angle. It is represented by the symbol "rad" * or, more rarely, by the superscript c (for "circular measure"). * For example, an angle of 1.2 radians would be written "1.2 rad" or "1.2c ". *

*

* The radian was formerly an SI supplementary unit, but this category was * abolished from the SI system in 1995 and the radian is now considered an SI * derived unit. For measuring solid angles, see steradian. *

*

* Nowadays, radian is the de facto unit of plane angles for mathematicians, * and the symbol "rad" is usually omitted in mathematicial writings. * When using degrees, the symbol is used to distinguish it from radians. *

*/ RADIAN() { public double convert( double a, AngleUnit u ) { return u.toRadian( a ); } public double toRadian( double a ) { return a; } public double toDegree( double a ) { return 180.0 * a / Math.PI; } public double toMinuteOfArc( double a ) { return 10800.0 * a / Math.PI; } public double toArcSecond( double a ) { return 648000.0 * a / Math.PI; } public double toGradian( double a ) { return a * Math.PI / 200.0; } public double toAngularMil( double a ) { return a * Math.PI / 3200.0; } public String getName() { return "radian"; } public String unitString() { return "rad"; } public char unitChar() { return UNICODE_UNSUPPORTED; } }; /** * char returned by unitChar() when there is no unicode character defined for a * AngleUnit. */ public static final char UNICODE_UNSUPPORTED = '?'; /** * Convert the given angle in the given unit to this * unit. * *

For example, to convert 180 degrees into radians, use: * AngleUnit.RADIAN.convert( 180.0, AngleUnit.DEGREE ) * * @param sourceAngle the angle in the given sourceUnit * @param sourceUnit the unit of the sourceAngle argument * @return the converted angle in this unit. */ public double convert( double sourceAngle, AngleUnit sourceUnit ) { throw new AbstractMethodError(); } /** * Equivalent to ARC_SECOND.convert(duration, this). * @param a the angle * @return the converted angle */ public double toArcSecond( double a ) { throw new AbstractMethodError(); } /** * Equivalent to DEGREE.convert(duration, this). * @param a the angle * @return the converted angle */ public double toDegree( double a ) { throw new AbstractMethodError(); } /** * Equivalent to GRADIAN.convert(duration, this). * @param a the angle * @return the converted angle */ public double toGradian( double a ) { throw new AbstractMethodError(); } /** * Equivalent to ANGULAR_MIL.convert(duration, this). * @param a the angle * @return the converted angle */ public double toAngularMil( double a ) { throw new AbstractMethodError(); } /** * Equivalent to MINUTE_OF_ARC.convert(duration, this). * @param a the angle * @return the converted angle */ public double toMinuteOfArc( double a ) { throw new AbstractMethodError(); } /** * Equivalent to RADIAN.convert(duration, this). * @param a the angle * @return the converted angle */ public double toRadian( double a ) { throw new AbstractMethodError(); } /** * The proper name of this AngleUnit, including the correct punctuation. * @return The proper name of this AngleUnit */ public String getName() { throw new AbstractMethodError(); } /** * The string representation of this unit as a unicode string. * @return string representation of this unit as a unicode string */ public String unitString() { throw new AbstractMethodError(); } /** * The unicode character representing this unit. If there is no character defined * in the unicode standard, then UNICODE_UNSUPPORTED is returned. * @return character representing this unit */ public char unitChar() { return UNICODE_UNSUPPORTED; } /** * String containing the unicode character representing this unit. If there is no character defined * in the unicode standard, then UNICODE_UNSUPPORTED is returned. * @return character representing this unit */ public String unicodeString() { return String.valueOf( UNICODE_UNSUPPORTED ); } /** * Convert the given temperature into a String including it's unit * @param t temperature * @return The temperature with it's unit */ public final String toString( double t ) { return toString( t, this ); } /** * Convert the given temperature into a String including it's unit * @param t temperature * @param unit The AngleUnit to format * @return The temperature with it's unit */ public final static String toString( double t, AngleUnit unit ) { return String.format( "%.2f%s", t, unit.unitString() ); } /** * Convert the given temperature into a String including it's unit. * Unlike the toString() varients, this will use the unicode character * to represent the unit if the AngleUnit has a unicode character. * If the AngleUnit does not support a unicode character, then this * is the same as toString( t ). * @param t temperature * @return The temperature with it's unit */ public final String toUnicodeString( double t ) { return toUnicodeString( t, this ); } /** * Convert the given temperature into a String including it's unit * Unlike the toString() varients, this will use the unicode character * to represent the unit if the AngleUnit has a unicode character. * If the AngleUnit does not support a unicode character, then this * is the same as toString( t, unit ). * @param t temperature * @param unit The AngleUnit to format * @return The temperature with it's unit */ public final static String toUnicodeString( double t, AngleUnit unit ) { if( unit.unitChar() == UNICODE_UNSUPPORTED ) { return toString( t, unit ); } else { return String.format( "%.2f%c", t, unit.unitChar() ); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy