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-2023 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
9    * BSD-style license. See <a href="https://djunits.org/docs/license.html">DJUNITS License</a>.
10   * </p>
11   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
12   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
13   */
14  public class GradeScale implements Scale
15  {
16      /** */
17      private static final long serialVersionUID = 20151011L;
18  
19      /** Multiply by this number to convert to the standard (e.g., SI) unit. */
20      private final double conversionFactorToGrade;
21  
22      /**
23       * Construct a Scale for transforming a slope as a grade, where 45 degrees is 1, and 90 degrees is infinite, to radians.
24       * @param conversionFactorToGrade double; the conversion factor by which this number has to be multiplied to convert it to
25       *            the standard (e.g., SI) unit.
26       */
27      public GradeScale(final double conversionFactorToGrade)
28      {
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 boolean isBaseSIScale()
57      {
58          return false;
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      /** {@inheritDoc} */
91      @Override
92      public String toString()
93      {
94          return "GradeScale [conversionFactorToGrade=" + this.conversionFactorToGrade + "]";
95      }
96  
97  }