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      /** Multiply by this number to convert to the standard (e.g., SI) unit. */
19      private final double conversionFactorToGrade;
20  
21      /**
22       * Construct a Scale for transforming a slope as a grade, where 45 degrees is 1, and 90 degrees is infinite, to radians.
23       * @param conversionFactorToGrade double; the conversion factor by which this number has to be multiplied to convert it to
24       *            the standard (e.g., SI) unit.
25       */
26      public GradeScale(final double conversionFactorToGrade)
27      {
28          super();
29          this.conversionFactorToGrade = conversionFactorToGrade;
30      }
31  
32      /** {@inheritDoc} */
33      @Override
34      public final double toStandardUnit(final double value)
35      {
36          return Math.atan(value * this.conversionFactorToGrade);
37      }
38  
39      /** {@inheritDoc} */
40      @Override
41      public final double fromStandardUnit(final double value)
42      {
43          return Math.tan(value) / this.conversionFactorToGrade;
44      }
45  
46      /**
47       * @return conversionFactorToGrade
48       */
49      public final double getConversionFactorToGrade()
50      {
51          return this.conversionFactorToGrade;
52      }
53  
54      /** {@inheritDoc} */
55      @Override
56      public final boolean isBaseSIScale()
57      {
58          return this.conversionFactorToGrade == 1.0;
59      }
60  
61      /** {@inheritDoc} */
62      @Override
63      public int hashCode()
64      {
65          final int prime = 31;
66          int result = 1;
67          long temp;
68          temp = Double.doubleToLongBits(this.conversionFactorToGrade);
69          result = prime * result + (int) (temp ^ (temp >>> 32));
70          return result;
71      }
72  
73      /** {@inheritDoc} */
74      @SuppressWarnings("checkstyle:needbraces")
75      @Override
76      public boolean equals(final Object obj)
77      {
78          if (this == obj)
79              return true;
80          if (obj == null)
81              return false;
82          if (getClass() != obj.getClass())
83              return false;
84          GradeScale other = (GradeScale) obj;
85          if (Double.doubleToLongBits(this.conversionFactorToGrade) != Double.doubleToLongBits(other.conversionFactorToGrade))
86              return false;
87          return true;
88      }
89  
90  }