View Javadoc
1   package org.djunits.quantity;
2   
3   import org.djunits.quantity.def.Quantity;
4   import org.djunits.unit.AbstractUnit;
5   import org.djunits.unit.UnitRuntimeException;
6   import org.djunits.unit.Unitless;
7   import org.djunits.unit.scale.GradeScale;
8   import org.djunits.unit.scale.IdentityScale;
9   import org.djunits.unit.scale.LinearScale;
10  import org.djunits.unit.scale.Scale;
11  import org.djunits.unit.si.SIUnit;
12  import org.djunits.unit.system.UnitSystem;
13  
14  /**
15   * Angle is the measure of rotation between two intersecting lines, expressed in radians (rad) or degrees.
16   * <p>
17   * Copyright (c) 2025-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
18   * for project information <a href="https://djunits.org" target="_blank">https://djunits.org</a>. The DJUNITS project is
19   * distributed under a <a href="https://djunits.org/docs/license.html" target="_blank">three-clause BSD-style license</a>.
20   * @author Alexander Verbraeck
21   */
22  public class Angle extends Quantity<Angle>
23  {
24      /** Constant with value zero radians. */
25      public static final Angle ZERO = ofSi(0.0);
26  
27      /** Constant with value pi radians. */
28      public static final Angle PI = ofSi(Math.PI);
29  
30      /** Constant with value pi/2 radians. */
31      public static final Angle HALF_PI = ofSi(Math.PI / 2.0);
32  
33      /** Constant with value 2 pi radians. */
34      public static final Angle TWO_PI = ofSi(Math.PI * 2.0);
35  
36      /** Constant with value tau radians. */
37      public static final Angle TAU = ofSi(Math.PI * 2.0);
38  
39      /** Constant with value one radian. */
40      public static final Angle ONE = ofSi(1.0);
41  
42      /** Constant with value NaN. */
43      @SuppressWarnings("checkstyle:constantname")
44      public static final Angle NaN = ofSi(Double.NaN);
45  
46      /** Constant with value POSITIVE_INFINITY. */
47      public static final Angle POSITIVE_INFINITY = ofSi(Double.POSITIVE_INFINITY);
48  
49      /** Constant with value NEGATIVE_INFINITY. */
50      public static final Angle NEGATIVE_INFINITY = ofSi(Double.NEGATIVE_INFINITY);
51  
52      /** Constant with value MAX_VALUE. */
53      public static final Angle POS_MAXVALUE = ofSi(Double.MAX_VALUE);
54  
55      /** Constant with value -MAX_VALUE. */
56      public static final Angle NEG_MAXVALUE = ofSi(-Double.MAX_VALUE);
57  
58      /** */
59      private static final long serialVersionUID = 600L;
60  
61      /**
62       * Instantiate a Angle quantity with a unit.
63       * @param valueInUnit the value, expressed in the unit
64       * @param unit the unit in which the value is expressed
65       */
66      public Angle(final double valueInUnit, final Angle.Unit unit)
67      {
68          super(valueInUnit, unit);
69      }
70  
71      /**
72       * Return a Angle instance based on an SI value.
73       * @param si the si value
74       * @return the Angle instance based on an SI value
75       */
76      public static Angle ofSi(final double si)
77      {
78          return new Angle(si, Angle.Unit.SI);
79      }
80  
81      @Override
82      public Angle instantiateSi(final double si)
83      {
84          return ofSi(si);
85      }
86  
87      @Override
88      public SIUnit siUnit()
89      {
90          return Angle.Unit.SI_UNIT;
91      }
92  
93      /**
94       * Returns a Angle representation of a textual representation of a value with a unit. The String representation that can be
95       * parsed is the double value in the unit, followed by a localized or English abbreviation of the unit. Spaces are allowed,
96       * but not required, between the value and the unit.
97       * @param text the textual representation to parse into a Angle
98       * @return the Scalar representation of the value in its unit
99       * @throws IllegalArgumentException when the text cannot be parsed
100      * @throws NullPointerException when the text argument is null
101      */
102     public static Angle valueOf(final String text)
103     {
104         return Quantity.valueOf(text, ZERO);
105     }
106 
107     /**
108      * Returns a Angle based on a value and the textual representation of the unit, which can be localized.
109      * @param valueInUnit the value, expressed in the unit as given by unitString
110      * @param unitString the textual representation of the unit
111      * @return the Scalar representation of the value in its unit
112      * @throws IllegalArgumentException when the unit cannot be parsed or is incorrect
113      * @throws NullPointerException when the unitString argument is null
114      */
115     public static Angle of(final double valueInUnit, final String unitString)
116     {
117         return Quantity.of(valueInUnit, unitString, ZERO);
118     }
119 
120     @Override
121     public Angle.Unit getDisplayUnit()
122     {
123         return (Angle.Unit) super.getDisplayUnit();
124     }
125 
126     /**
127      * Add an (absolute) direction to this angle, and return a direction. The unit of the return value will be the unit of this
128      * angle, and the reference of the return value will be the reference belonging to the given direction.
129      * <code>R.add(A)</code> = unit of R and reference value of A.
130      * @param direction the absolute direction to add
131      * @return the absolute direction plus this angle
132      */
133     public final Direction add(final Direction direction)
134     {
135         return direction.add(this).setDisplayUnit(getDisplayUnit());
136     }
137 
138     /**
139      * Calculate the division of Angle and Angle, which results in a Dimensionless scalar.
140      * @param v scalar
141      * @return scalar as a division of Angle and Angle
142      */
143     public final Dimensionless divide(final Angle v)
144     {
145         return new Dimensionless(this.si() / v.si(), Unitless.BASE);
146     }
147 
148     /**
149      * Calculate the multiplication of Angle and Frequency, which results in a AngularVelocity scalar.
150      * @param v scalar
151      * @return scalar as a multiplication of Angle and Frequency
152      */
153     public final AngularVelocity multiply(final Frequency v)
154     {
155         return new AngularVelocity(this.si() * v.si(), AngularVelocity.Unit.SI);
156     }
157 
158     /**
159      * Calculate the division of Angle and Duration, which results in a AngularVelocity scalar.
160      * @param v scalar
161      * @return scalar as a division of Angle and Duration
162      */
163     public final AngularVelocity divide(final Duration v)
164     {
165         return new AngularVelocity(this.si() / v.si(), AngularVelocity.Unit.SI);
166     }
167 
168     /**
169      * Calculate the division of Angle and AngularVelocity, which results in a Duration scalar.
170      * @param v scalar
171      * @return scalar as a division of Angle and AngularVelocity
172      */
173     public final Duration divide(final AngularVelocity v)
174     {
175         return new Duration(this.si() / v.si(), Duration.Unit.SI);
176     }
177 
178     /**
179      * Normalize an angle between 0 and 2 * PI.
180      * @param angle original angle.
181      * @return angle between 0 and 2 * PI.
182      */
183     public static double normalize(final double angle)
184     {
185         double normalized = angle % (2 * Math.PI);
186         if (normalized < 0.0)
187         {
188             normalized += 2 * Math.PI;
189         }
190         return normalized;
191     }
192 
193     /**
194      * Normalize an angle between 0 and 2 * PI.
195      * @param angle original angle.
196      * @return a new Angle object with angle between 0 and 2 * PI.
197      */
198     public static Angle normalize(final Angle angle)
199     {
200         return new Angle(normalize(angle.si()), Angle.Unit.rad);
201     }
202 
203     /******************************************************************************************************/
204     /********************************************** UNIT CLASS ********************************************/
205     /******************************************************************************************************/
206 
207     /**
208      * Angle.Unit encodes the units of angle (radians, degrees).
209      * <p>
210      * Copyright (c) 2025-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved.
211      * See for project information <a href="https://djunits.org" target="_blank">https://djunits.org</a>. The DJUNITS project is
212      * distributed under a <a href="https://djunits.org/docs/license.html" target="_blank">three-clause BSD-style license</a>.
213      * @author Alexander Verbraeck
214      */
215     @SuppressWarnings("checkstyle:constantname")
216     public static class Unit extends AbstractUnit<Angle.Unit, Angle>
217     {
218         /** The dimensions of Angle: rad. */
219         public static final SIUnit SI_UNIT = SIUnit.of("rad");
220 
221         /** radian. */
222         public static final Angle.Unit rad = new Angle.Unit("rad", "rad", "radian", IdentityScale.SCALE, UnitSystem.SI_DERIVED);
223 
224         /** The SI or BASE unit. */
225         public static final Angle.Unit SI = rad;
226 
227         /** percent (non-linear, 100% is 45 degrees; 90 degrees is infinite). */
228         public static final Angle.Unit percent = new Angle.Unit("%", "%", "percent", new GradeScale(0.01), UnitSystem.OTHER);
229 
230         /** degree. */
231         public static final Angle.Unit deg = rad.deriveUnit("deg", "\u00b0", "degree", Math.PI / 180.0, UnitSystem.SI_ACCEPTED);
232 
233         /** arcminute. */
234         public static final Angle.Unit arcmin = deg.deriveUnit("arcmin", "'", "arcminute", 1.0 / 60.0, UnitSystem.OTHER);
235 
236         /** arcsecond. */
237         public static final Angle.Unit arcsec = deg.deriveUnit("arcsec", "\"", "arcsecond", 1.0 / 3600.0, UnitSystem.OTHER);
238 
239         /** grad. */
240         public static final Angle.Unit grad = rad.deriveUnit("grad", "gradian", 2.0 * Math.PI / 400.0, UnitSystem.OTHER);
241 
242         /** centesimal arcminute. */
243         public static final Angle.Unit cdm =
244                 grad.deriveUnit("cdm", "c'", "centesimal arcminute", 1.0 / 100.0, UnitSystem.OTHER);
245 
246         /** centesimal arcsecond. */
247         public static final Angle.Unit cds =
248                 grad.deriveUnit("cds", "c\"", "centesimal arcsecond", 1.0 / 10000.0, UnitSystem.OTHER);
249 
250         /**
251          * Create a new Angle unit.
252          * @param id the id or main abbreviation of the unit
253          * @param name the full name of the unit
254          * @param scaleFactorToBaseUnit the scale factor of the unit to convert it TO the base (SI) unit
255          * @param unitSystem the unit system such as SI or IMPERIAL
256          */
257         public Unit(final String id, final String name, final double scaleFactorToBaseUnit, final UnitSystem unitSystem)
258         {
259             super(id, name, new LinearScale(scaleFactorToBaseUnit), unitSystem);
260         }
261 
262         /**
263          * Return a derived unit for this unit, with textual abbreviation(s) and a display abbreviation.
264          * @param textualAbbreviation the textual abbreviation of the unit, which doubles as the id
265          * @param displayAbbreviation the display abbreviation of the unit
266          * @param name the full name of the unit
267          * @param scale the scale to use to convert between this unit and the standard (e.g., SI, BASE) unit
268          * @param unitSystem unit system, e.g. SI or Imperial
269          */
270         public Unit(final String textualAbbreviation, final String displayAbbreviation, final String name, final Scale scale,
271                 final UnitSystem unitSystem)
272         {
273             super(textualAbbreviation, displayAbbreviation, name, scale, unitSystem);
274         }
275 
276         @Override
277         public SIUnit siUnit()
278         {
279             return SI_UNIT;
280         }
281 
282         @Override
283         public Unit getBaseUnit()
284         {
285             return SI;
286         }
287 
288         /** {@inheritDoc} */
289         @Override
290         public Angle ofSi(final double si)
291         {
292             return Angle.ofSi(si);
293         }
294 
295         @Override
296         public Unit deriveUnit(final String textualAbbreviation, final String displayAbbreviation, final String name,
297                 final double scaleFactor, final UnitSystem unitSystem)
298         {
299             if (getScale() instanceof LinearScale ls)
300             {
301                 return new Angle.Unit(textualAbbreviation, displayAbbreviation, name,
302                         new LinearScale(ls.getScaleFactorToBaseUnit() * scaleFactor), unitSystem);
303             }
304             throw new UnitRuntimeException("Only possible to derive a unit from a unit with a linear scale");
305         }
306 
307     }
308 }