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>
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 valueInUnit the value, expressed in the unit
65 * @param unit the unit in which the value is expressed
66 */
67 public Angle(final double valueInUnit, final Angle.Unit unit)
68 {
69 super(valueInUnit, unit);
70 }
71
72 /**
73 * Instantiate a Angle quantity with a unit, expressed as a String.
74 * @param valueInUnit 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 valueInUnit, final String abbreviation)
78 {
79 this(valueInUnit, 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 instantiateSi(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 valueInUnit the value, expressed in the unit as given by unitString
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 valueInUnit, final String unitString)
137 {
138 return Quantity.of(valueInUnit, unitString, ZERO);
139 }
140
141 @Override
142 public Angle.Unit getDisplayUnit()
143 {
144 return (Angle.Unit) super.getDisplayUnit();
145 }
146
147 /**
148 * Add an (absolute) direction to this angle, and return a direction. The unit of the return value will be the unit of this
149 * angle, and the reference of the return value will be the reference belonging to the given direction.
150 * <code>R.add(A)</code> = unit of R and reference value of A.
151 * @param direction the absolute direction to add
152 * @return the absolute direction plus this angle
153 */
154 public final Direction add(final Direction direction)
155 {
156 return direction.add(this).setDisplayUnit(getDisplayUnit());
157 }
158
159 /**
160 * Calculate the division of Angle and Angle, which results in a Dimensionless scalar.
161 * @param v scalar
162 * @return scalar as a division of Angle and Angle
163 */
164 public final Dimensionless divide(final Angle v)
165 {
166 return new Dimensionless(this.si() / v.si(), Unitless.BASE);
167 }
168
169 /**
170 * Calculate the multiplication of Angle and Frequency, which results in a AngularVelocity scalar.
171 * @param v scalar
172 * @return scalar as a multiplication of Angle and Frequency
173 */
174 public final AngularVelocity multiply(final Frequency v)
175 {
176 return new AngularVelocity(this.si() * v.si(), AngularVelocity.Unit.SI);
177 }
178
179 /**
180 * Calculate the division of Angle and Duration, which results in a AngularVelocity scalar.
181 * @param v scalar
182 * @return scalar as a division of Angle and Duration
183 */
184 public final AngularVelocity divide(final Duration v)
185 {
186 return new AngularVelocity(this.si() / v.si(), AngularVelocity.Unit.SI);
187 }
188
189 /**
190 * Calculate the division of Angle and AngularVelocity, which results in a Duration scalar.
191 * @param v scalar
192 * @return scalar as a division of Angle and AngularVelocity
193 */
194 public final Duration divide(final AngularVelocity v)
195 {
196 return new Duration(this.si() / v.si(), Duration.Unit.SI);
197 }
198
199 /**
200 * Normalize an angle between 0 and 2 * PI.
201 * @param angle original angle.
202 * @return angle between 0 and 2 * PI.
203 */
204 public static double normalize(final double angle)
205 {
206 double normalized = angle % (2 * Math.PI);
207 if (normalized < 0.0)
208 {
209 normalized += 2 * Math.PI;
210 }
211 return normalized;
212 }
213
214 /**
215 * Normalize an angle between 0 and 2 * PI.
216 * @param angle original angle.
217 * @return a new Angle object with angle between 0 and 2 * PI.
218 */
219 public static Angle normalize(final Angle angle)
220 {
221 return new Angle(normalize(angle.si()), Angle.Unit.rad);
222 }
223
224 /******************************************************************************************************/
225 /********************************************** UNIT CLASS ********************************************/
226 /******************************************************************************************************/
227
228 /**
229 * Angle.Unit encodes the units of angle (radians, degrees).
230 * <p>
231 * Copyright (c) 2025-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved.
232 * See for project information <a href="https://djunits.org" target="_blank">https://djunits.org</a>. The DJUNITS project is
233 * distributed under a <a href="https://djunits.org/docs/license.html" target="_blank">three-clause BSD-style license</a>.
234 * @author Alexander Verbraeck
235 */
236 @SuppressWarnings("checkstyle:constantname")
237 public static class Unit extends AbstractUnit<Angle.Unit, Angle>
238 {
239 /** The dimensions of Angle: rad. */
240 public static final SIUnit SI_UNIT = SIUnit.of("rad");
241
242 /** radian. */
243 public static final Angle.Unit rad = new Angle.Unit("rad", "rad", "radian", IdentityScale.SCALE, UnitSystem.SI_DERIVED);
244
245 /** The SI or BASE unit. */
246 public static final Angle.Unit SI = rad;
247
248 /** percent (non-linear, 100% is 45 degrees; 90 degrees is infinite). */
249 public static final Angle.Unit percent = new Angle.Unit("%", "%", "percent", new GradeScale(0.01), UnitSystem.OTHER);
250
251 /** degree. */
252 public static final Angle.Unit deg = rad.deriveUnit("deg", "\u00b0", "degree", Math.PI / 180.0, UnitSystem.SI_ACCEPTED);
253
254 /** arcminute. */
255 public static final Angle.Unit arcmin = deg.deriveUnit("arcmin", "'", "arcminute", 1.0 / 60.0, UnitSystem.OTHER);
256
257 /** arcsecond. */
258 public static final Angle.Unit arcsec = deg.deriveUnit("arcsec", "\"", "arcsecond", 1.0 / 3600.0, UnitSystem.OTHER);
259
260 /** grad. */
261 public static final Angle.Unit grad = rad.deriveUnit("grad", "gradian", 2.0 * Math.PI / 400.0, UnitSystem.OTHER);
262
263 /** centesimal arcminute. */
264 public static final Angle.Unit cdm =
265 grad.deriveUnit("cdm", "c'", "centesimal arcminute", 1.0 / 100.0, UnitSystem.OTHER);
266
267 /** centesimal arcsecond. */
268 public static final Angle.Unit cds =
269 grad.deriveUnit("cds", "c\"", "centesimal arcsecond", 1.0 / 10000.0, UnitSystem.OTHER);
270
271 /**
272 * Create a new Angle unit.
273 * @param id the id or main abbreviation of the unit
274 * @param name the full name of the unit
275 * @param scaleFactorToBaseUnit the scale factor of the unit to convert it TO the base (SI) unit
276 * @param unitSystem the unit system such as SI or IMPERIAL
277 */
278 public Unit(final String id, final String name, final double scaleFactorToBaseUnit, final UnitSystem unitSystem)
279 {
280 super(id, name, new LinearScale(scaleFactorToBaseUnit), unitSystem);
281 }
282
283 /**
284 * Return a derived unit for this unit, with textual abbreviation(s) and a display abbreviation.
285 * @param textualAbbreviation the textual abbreviation of the unit, which doubles as the id
286 * @param displayAbbreviation the display abbreviation of the unit
287 * @param name the full name of the unit
288 * @param scale the scale to use to convert between this unit and the standard (e.g., SI, BASE) unit
289 * @param unitSystem unit system, e.g. SI or Imperial
290 */
291 public Unit(final String textualAbbreviation, final String displayAbbreviation, final String name, final Scale scale,
292 final UnitSystem unitSystem)
293 {
294 super(textualAbbreviation, displayAbbreviation, name, scale, unitSystem);
295 }
296
297 @Override
298 public SIUnit siUnit()
299 {
300 return SI_UNIT;
301 }
302
303 @Override
304 public Unit getBaseUnit()
305 {
306 return SI;
307 }
308
309 /** {@inheritDoc} */
310 @Override
311 public Angle ofSi(final double si)
312 {
313 return Angle.ofSi(si);
314 }
315
316 @Override
317 public Unit deriveUnit(final String textualAbbreviation, final String displayAbbreviation, final String name,
318 final double scaleFactor, final UnitSystem unitSystem)
319 {
320 if (getScale() instanceof LinearScale ls)
321 {
322 return new Angle.Unit(textualAbbreviation, displayAbbreviation, name,
323 new LinearScale(ls.getScaleFactorToBaseUnit() * scaleFactor), unitSystem);
324 }
325 throw new UnitRuntimeException("Only possible to derive a unit from a unit with a linear scale");
326 }
327
328 }
329 }