View Javadoc
1   package org.djunits.unit.scale;
2   
3   import org.djutils.exceptions.Throw;
4   
5   /**
6    * A Scale for transforming a slope as a grade, where 45 degrees is 1, and 90 degrees is infinite, to radians. The formula is:
7    * 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
8    * constructed for e.g. ratio as a percentage (100% instead of 1), the conversionFactor to apply is 0.01.
9    * <p>
10   * Copyright (c) 2013-2026 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
11   * BSD-style license. See <a href="https://djunits.org/docs/license.html">DJUNITS License</a>.
12   * @author Alexander Verbraeck
13   * @author Peter Knoppers
14   */
15  public class GradeScale implements Scale
16  {
17      /** */
18      private static final long serialVersionUID = 20151011L;
19  
20      /** Multiply by this number to convert to the standard (e.g., SI) unit. */
21      private final double conversionFactorToGrade;
22  
23      /**
24       * Construct a Scale for transforming a slope as a grade, where 45 degrees is 1, and 90 degrees is infinite, to radians.
25       * @param conversionFactorToGrade the conversion factor by which this number has to be multiplied to convert it to the
26       *            standard (e.g., SI) unit.
27       */
28      public GradeScale(final double conversionFactorToGrade)
29      {
30          Throw.when(conversionFactorToGrade == 0.0 || !Double.isFinite(conversionFactorToGrade), IllegalArgumentException.class,
31                  "conversion factor for grade scale cannnot be 0, NaN or infinity");
32          this.conversionFactorToGrade = conversionFactorToGrade;
33      }
34  
35      @Override
36      public final double toBaseValue(final double value)
37      {
38          return Math.atan(value * this.conversionFactorToGrade);
39      }
40  
41      @Override
42      public final double fromBaseValue(final double value)
43      {
44          return Math.tan(value) / this.conversionFactorToGrade;
45      }
46  
47      /**
48       * @return conversionFactorToGrade
49       */
50      public final double getConversionFactorToGrade()
51      {
52          return this.conversionFactorToGrade;
53      }
54  
55      @Override
56      public boolean isBaseScale()
57      {
58          return false;
59      }
60  
61      @Override
62      public int hashCode()
63      {
64          final int prime = 31;
65          int result = 1;
66          long temp;
67          temp = Double.doubleToLongBits(this.conversionFactorToGrade);
68          result = prime * result + (int) (temp ^ (temp >>> 32));
69          return result;
70      }
71  
72      @SuppressWarnings("checkstyle:needbraces")
73      @Override
74      public boolean equals(final Object obj)
75      {
76          if (this == obj)
77              return true;
78          if (obj == null)
79              return false;
80          if (getClass() != obj.getClass())
81              return false;
82          GradeScale other = (GradeScale) obj;
83          if (Double.doubleToLongBits(this.conversionFactorToGrade) != Double.doubleToLongBits(other.conversionFactorToGrade))
84              return false;
85          return true;
86      }
87  
88      @Override
89      public String toString()
90      {
91          return "GradeScale [conversionFactorToGrade=" + this.conversionFactorToGrade + "]";
92      }
93  
94  }