1 package org.djunits.unit.scale; 2 3 /** 4 * A Scale for linear transformations with an offset that has to be applied first when converting to the standard (SI) unit, 5 * before the scaling takes place, e.g. for Temperature. As an example, transform from Degrees Fahrenheit to Kelvin (SI). The 6 * conversion is K = (F + 459.67) × 5⁄9, and F = K × 9⁄5 − 459.67. 7 * <p> 8 * Copyright (c) 2013-2018 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 OffsetLinearScale extends LinearScale implements Scale 17 { 18 /** The offset that has to be taken into account for conversions, multiplied by the conversionFactorToStandardUnit. */ 19 private final double offsetToStandardUnit; 20 21 /** 22 * Construct a Scale for linear transformations with an offset, e.g. for Temperature. 23 * @param conversionFactorToStandardUnit the conversion factor by which this number has to be multiplied to convert it to 24 * the standard (e.g., SI) unit. 25 * @param offsetToStandardUnit the offset that has to be taken into account for conversions; when converting to a standard 26 * unit, the offset is applied first. 27 */ 28 public OffsetLinearScale(final double conversionFactorToStandardUnit, final double offsetToStandardUnit) 29 { 30 super(conversionFactorToStandardUnit); 31 this.offsetToStandardUnit = offsetToStandardUnit; 32 } 33 34 /** {@inheritDoc} */ 35 @Override 36 public final double toStandardUnit(final double value) 37 { 38 return (value + this.offsetToStandardUnit) * getConversionFactorToStandardUnit(); 39 } 40 41 /** {@inheritDoc} */ 42 @Override 43 public final double fromStandardUnit(final double value) 44 { 45 return (value / getConversionFactorToStandardUnit()) - this.offsetToStandardUnit; 46 } 47 48 /** 49 * @return offsetToStandardUnit 50 */ 51 public final double getOffsetToStandardUnit() 52 { 53 return this.offsetToStandardUnit; 54 } 55 56 /** {@inheritDoc} */ 57 @Override 58 public final boolean isBaseSIScale() 59 { 60 return getConversionFactorToStandardUnit() == 1.0 && this.offsetToStandardUnit == 0.0; 61 } 62 63 }