001package com.hfg.math.units; 002 003 004import com.hfg.exception.ProgrammingException; 005 006//------------------------------------------------------------------------------ 007/** 008 Base class for unit-independent angle measurement. 009 <div> 010 @author J. Alex Taylor, hairyfatguy.com 011 </div> 012 */ 013//------------------------------------------------------------------------------ 014// com.hfg XML/HTML Coding Library 015// 016// This library is free software; you can redistribute it and/or 017// modify it under the terms of the GNU Lesser General Public 018// License as published by the Free Software Foundation; either 019// version 2.1 of the License, or (at your option) any later version. 020// 021// This library is distributed in the hope that it will be useful, 022// but WITHOUT ANY WARRANTY; without even the implied warranty of 023// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 024// Lesser General Public License for more details. 025// 026// You should have received a copy of the GNU Lesser General Public 027// License along with this library; if not, write to the Free Software 028// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 029// 030// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 031// jataylor@hairyfatguy.com 032//------------------------------------------------------------------------------ 033 034public abstract class AngleImpl implements Angle 035{ 036 //########################################################################## 037 // PRIVATE FIELDS 038 //########################################################################## 039 040 private float mValue; 041 private AngleUnits mAngleUnits; 042 043 //########################################################################## 044 // CONSTRUCTORS 045 //########################################################################## 046 047 //-------------------------------------------------------------------------- 048 protected AngleImpl(int inValue, AngleUnits inUnits) 049 { 050 mValue = inValue; 051 mAngleUnits = inUnits; 052 } 053 054 //-------------------------------------------------------------------------- 055 protected AngleImpl(float inValue, AngleUnits inUnits) 056 { 057 mValue = inValue; 058 mAngleUnits = inUnits; 059 } 060 061 //########################################################################## 062 // PUBLIC METHODS 063 //########################################################################## 064 065 //-------------------------------------------------------------------------- 066 public float value() 067 { 068 return mValue; 069 } 070 071 //-------------------------------------------------------------------------- 072 public AngleUnits getUnits() 073 { 074 return mAngleUnits; 075 } 076 077 //-------------------------------------------------------------------------- 078 @Override 079 public String toString() 080 { 081 return value() + mAngleUnits.abbrev(); 082 } 083 084 //--------------------------------------------------------------------------- 085 public int toInt(AngleUnits inUnits) 086 { 087 return (int) to(inUnits); 088 } 089 090 //--------------------------------------------------------------------------- 091 public float to(AngleUnits inUnits) 092 { 093 float convertedValue; 094 095 if (inUnits.equals(getUnits())) 096 { 097 // No conversion necessary 098 convertedValue = value(); 099 } 100 else 101 { 102 // For simplicity, start from degrees 103 float degrees = toDegrees(); 104 105 if (inUnits.equals(AngleUnits.degrees)) 106 { 107 convertedValue = degrees; 108 } 109 else if (inUnits.equals(AngleUnits.radians)) 110 { 111 convertedValue = (float) (degrees * Math.PI / 180d); 112 } 113 else 114 { 115 throw new ProgrammingException(inUnits + " is not a currently supported unit for conversion!"); 116 } 117 } 118 119 return convertedValue; 120 } 121 122 //-------------------------------------------------------------------------- 123 public void scale(float inScalingFactor) 124 { 125 mValue *= inScalingFactor; 126 } 127 128 //--------------------------------------------------------------------------- 129 private float toDegrees() 130 { 131 float degrees; 132 133 if (mAngleUnits.equals(AngleUnits.degrees)) 134 { 135 degrees = mValue; 136 } 137 else if (mAngleUnits.equals(AngleUnits.radians)) 138 { 139 degrees = (float) (mValue * 180d / Math.PI); 140 } 141 else 142 { 143 throw new ProgrammingException(mAngleUnits + " is not a currently supported unit for conversion!"); 144 } 145 146 return degrees; 147 } 148}