View Javadoc
1   package org.djunits.unit.scale;
2   
3   /**
4    * A Scale for transforming a slope as a grade, where 45 degrees is 1, and 90 degrees is infinite, to radians. The formula is:
5    * angle = atan(grade). The factor that is given, is the factor by which the value is multiplied to get a grade. When a scale is
6    * constructed for e.g. ratio as a percentage (100% instead of 1), the conversionFactor to apply is 0.01.
7    * <p>
8    * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
9    * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
10   * </p>
11   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
12   * initial version Oct 11, 2015 <br>
13   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
14   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
15   */
16  public class GradeScale implements Scale
17  {
18      /** */
19      private static final long serialVersionUID = 1L;
20      
21      /** Multiply by this number to convert to the standard (e.g., SI) unit. */
22      private final double conversionFactorToGrade;
23  
24      /**
25       * Construct a Scale for transforming a slope as a grade, where 45 degrees is 1, and 90 degrees is infinite, to radians.
26       * @param conversionFactorToGrade double; the conversion factor by which this number has to be multiplied to convert it to
27       *            the standard (e.g., SI) unit.
28       */
29      public GradeScale(final double conversionFactorToGrade)
30      {
31          super();
32          this.conversionFactorToGrade = conversionFactorToGrade;
33      }
34  
35      /** {@inheritDoc} */
36      @Override
37      public final double toStandardUnit(final double value)
38      {
39          return Math.atan(value * this.conversionFactorToGrade);
40      }
41  
42      /** {@inheritDoc} */
43      @Override
44      public final double fromStandardUnit(final double value)
45      {
46          return Math.tan(value) / this.conversionFactorToGrade;
47      }
48  
49      /**
50       * @return conversionFactorToGrade
51       */
52      public final double getConversionFactorToGrade()
53      {
54          return this.conversionFactorToGrade;
55      }
56  
57      /** {@inheritDoc} */
58      @Override
59      public final boolean isBaseSIScale()
60      {
61          return this.conversionFactorToGrade == 1.0;
62      }
63  
64      /** {@inheritDoc} */
65      @Override
66      public int hashCode()
67      {
68          final int prime = 31;
69          int result = 1;
70          long temp;
71          temp = Double.doubleToLongBits(this.conversionFactorToGrade);
72          result = prime * result + (int) (temp ^ (temp >>> 32));
73          return result;
74      }
75  
76      /** {@inheritDoc} */
77      @SuppressWarnings("checkstyle:needbraces")
78      @Override
79      public boolean equals(final Object obj)
80      {
81          if (this == obj)
82              return true;
83          if (obj == null)
84              return false;
85          if (getClass() != obj.getClass())
86              return false;
87          GradeScale other = (GradeScale) obj;
88          if (Double.doubleToLongBits(this.conversionFactorToGrade) != Double.doubleToLongBits(other.conversionFactorToGrade))
89              return false;
90          return true;
91      }
92  
93  }