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-2024 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      @Override
33      public final double toStandardUnit(final double value)
34      {
35          return Math.atan(value * this.conversionFactorToGrade);
36      }
37  
38      @Override
39      public final double fromStandardUnit(final double value)
40      {
41          return Math.tan(value) / this.conversionFactorToGrade;
42      }
43  
44      /**
45       * @return conversionFactorToGrade
46       */
47      public final double getConversionFactorToGrade()
48      {
49          return this.conversionFactorToGrade;
50      }
51  
52      @Override
53      public boolean isBaseSIScale()
54      {
55          return false;
56      }
57  
58      @Override
59      public int hashCode()
60      {
61          final int prime = 31;
62          int result = 1;
63          long temp;
64          temp = Double.doubleToLongBits(this.conversionFactorToGrade);
65          result = prime * result + (int) (temp ^ (temp >>> 32));
66          return result;
67      }
68  
69      @SuppressWarnings("checkstyle:needbraces")
70      @Override
71      public boolean equals(final Object obj)
72      {
73          if (this == obj)
74              return true;
75          if (obj == null)
76              return false;
77          if (getClass() != obj.getClass())
78              return false;
79          GradeScale other = (GradeScale) obj;
80          if (Double.doubleToLongBits(this.conversionFactorToGrade) != Double.doubleToLongBits(other.conversionFactorToGrade))
81              return false;
82          return true;
83      }
84  
85      @Override
86      public String toString()
87      {
88          return "GradeScale [conversionFactorToGrade=" + this.conversionFactorToGrade + "]";
89      }
90  
91  }