1/* 2 * Copyright (C) 2010 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.google.caliper.util; 18 19import com.google.common.annotations.GwtCompatible; 20 21@GwtCompatible 22public class LinearTranslation { 23 // y = mx + b 24 private final double m; 25 private final double b; 26 27 // TODO(kevinb): why so high? why even check this at all? 28 private static final double EQUALITY_TOLERANCE = 1.0E-6; 29 30 /** 31 * Constructs a linear translation for which {@code translate(in1) == out1} 32 * and {@code translate(in2) == out2}. 33 * 34 * @throws IllegalArgumentException if {@code in1 == in2} 35 */ 36 public LinearTranslation(double in1, double out1, double in2, double out2) { 37 if (Math.abs(in1 - in2) < EQUALITY_TOLERANCE) { 38 throw new IllegalArgumentException("in1 and in2 are approximately equal"); 39 } 40 double divisor = in1 - in2; 41 this.m = (out1 - out2) / divisor; 42 this.b = (in1 * out2 - in2 * out1) / divisor; 43 } 44 45 public double translate(double in) { 46 return m * in + b; 47 } 48} 49