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