1 package org.djunits.value.vdouble.scalar;
2
3 import org.djunits.unit.AngleUnit;
4 import org.djunits.unit.DimensionlessUnit;
5 import org.djunits.unit.DirectionUnit;
6
7 /**
8 * Easy access methods for the Relative Angle DoubleScalar. Instead of:
9 *
10 * <pre>
11 * DoubleScalar<AngleUnit> value = new DoubleScalar<AngleUnit>(100.0, AngleUnit.SI);
12 * </pre>
13 *
14 * we can now write:
15 *
16 * <pre>
17 * Angle value = new Angle(100.0, AngleUnit.SI);
18 * </pre>
19 *
20 * The compiler will automatically recognize which units belong to which quantity, and whether the quantity type and the unit
21 * used are compatible.
22 * <p>
23 * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. <br>
24 * All rights reserved. <br>
25 * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
26 * <p>
27 * $LastChangedDate: 2015-12-22 04:32:39 +0100 (Tue, 22 Dec 2015) $, @version $Revision: 180 $, by $Author: averbraeck $,
28 * initial version Sep 1, 2015 <br>
29 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
30 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
31 */
32 public class Angle extends AbstractDoubleScalarRel<AngleUnit, Angle>
33 {
34 /** */
35 private static final long serialVersionUID = 20150901L;
36
37 /** constant with value zero. */
38 public static final Angle ZERO = new Angle(0.0, AngleUnit.SI);
39
40 /** constant with value NaN. */
41 @SuppressWarnings("checkstyle:constantname")
42 public static final Angle NaN = new Angle(Double.NaN, AngleUnit.SI);
43
44 /** constant with value POSITIVE_INFINITY. */
45 public static final Angle POSITIVE_INFINITY = new Angle(Double.POSITIVE_INFINITY, AngleUnit.SI);
46
47 /** constant with value NEGATIVE_INFINITY. */
48 public static final Angle NEGATIVE_INFINITY = new Angle(Double.NEGATIVE_INFINITY, AngleUnit.SI);
49
50 /** constant with value MAX_VALUE. */
51 public static final Angle POS_MAXVALUE = new Angle(Double.MAX_VALUE, AngleUnit.SI);
52
53 /** constant with value -MAX_VALUE. */
54 public static final Angle NEG_MAXVALUE = new Angle(-Double.MAX_VALUE, AngleUnit.SI);
55
56 /**
57 * Construct Angle scalar.
58 * @param value double value
59 * @param unit unit for the double value
60 */
61 public Angle(final double value, final AngleUnit unit)
62 {
63 super(value, unit);
64 }
65
66 /**
67 * Construct Angle scalar.
68 * @param value Scalar from which to construct this instance
69 */
70 public Angle(final Angle value)
71 {
72 super(value);
73 }
74
75 /** {@inheritDoc} */
76 @Override
77 public final Angle instantiateRel(final double value, final AngleUnit unit)
78 {
79 return new Angle(value, unit);
80 }
81
82 /**
83 * Construct a new Absolute Immutable DoubleScalar of the right type. Each extending class must implement this method.
84 * @param value the double value
85 * @param unit the unit
86 * @return A a new absolute instance of the DoubleScalar of the right type
87 */
88 public final Direction instantiateAbs(final double value, final DirectionUnit unit)
89 {
90 return new Direction(value, unit);
91 }
92
93 /**
94 * Construct Angle scalar.
95 * @param value double value in SI units
96 * @return the new scalar with the SI value
97 */
98 public static final Angle createSI(final double value)
99 {
100 return new Angle(value, AngleUnit.SI);
101 }
102
103 /**
104 * Interpolate between two values.
105 * @param zero the low value
106 * @param one the high value
107 * @param ratio the ratio between 0 and 1, inclusive
108 * @return a Scalar at the ratio between
109 */
110 public static Angle interpolate(final Angle zero, final Angle one, final double ratio)
111 {
112 return new Angle(zero.getInUnit() * (1 - ratio) + one.getInUnit(zero.getUnit()) * ratio, zero.getUnit());
113 }
114
115 /**
116 * Relative scalar plus Absolute scalar = Absolute scalar.
117 * @param v the value to add
118 * @return sum of this value and v as a new object
119 */
120 public final Direction plus(final Direction v)
121 {
122 DirectionUnit targetUnit = v.getUnit();
123 return instantiateAbs(v.getInUnit() + getInUnit(targetUnit.getRelativeUnit()), targetUnit);
124 }
125
126 /**
127 * Return the maximum value of two relative scalars.
128 * @param r1 the first scalar
129 * @param r2 the second scalar
130 * @return the maximum value of two relative scalars
131 */
132 public static Angle max(final Angle r1, final Angle r2)
133 {
134 return (r1.gt(r2)) ? r1 : r2;
135 }
136
137 /**
138 * Return the maximum value of more than two relative scalars.
139 * @param r1 the first scalar
140 * @param r2 the second scalar
141 * @param rn the other scalars
142 * @return the maximum value of more than two relative scalars
143 */
144 public static Angle max(final Angle r1, final Angle r2, final Angle... rn)
145 {
146 Angle maxr = (r1.gt(r2)) ? r1 : r2;
147 for (Angle r : rn)
148 {
149 if (r.gt(maxr))
150 {
151 maxr = r;
152 }
153 }
154 return maxr;
155 }
156
157 /**
158 * Return the minimum value of two relative scalars.
159 * @param r1 the first scalar
160 * @param r2 the second scalar
161 * @return the minimum value of two relative scalars
162 */
163 public static Angle min(final Angle r1, final Angle r2)
164 {
165 return (r1.lt(r2)) ? r1 : r2;
166 }
167
168 /**
169 * Return the minimum value of more than two relative scalars.
170 * @param r1 the first scalar
171 * @param r2 the second scalar
172 * @param rn the other scalars
173 * @return the minimum value of more than two relative scalars
174 */
175 public static Angle min(final Angle r1, final Angle r2, final Angle... rn)
176 {
177 Angle minr = (r1.lt(r2)) ? r1 : r2;
178 for (Angle r : rn)
179 {
180 if (r.lt(minr))
181 {
182 minr = r;
183 }
184 }
185 return minr;
186 }
187
188 /**
189 * Calculate the division of Angle and Angle, which results in a Dimensionless scalar.
190 * @param v Angle scalar
191 * @return Dimensionless scalar as a division of Angle and Angle
192 */
193 public final Dimensionless divideBy(final Angle v)
194 {
195 return new Dimensionless(this.si / v.si, DimensionlessUnit.SI);
196 }
197
198 }