1 /* 2 * Copyright (C) 2016 The Android Open Source Project 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 17 package com.android.dialer.common; 18 19 /** Utility class for common math operations */ 20 public class MathUtil { 21 22 /** 23 * Interpolates between two integer values based on percentage. 24 * 25 * @param begin Begin value 26 * @param end End value 27 * @param percent Percentage value, between 0 and 1 28 * @return Interpolated result 29 */ 30 public static int lerp(int begin, int end, float percent) { 31 return (int) (begin * (1 - percent) + end * percent); 32 } 33 34 /** 35 * Interpolates between two float values based on percentage. 36 * 37 * @param begin Begin value 38 * @param end End value 39 * @param percent Percentage value, between 0 and 1 40 * @return Interpolated result 41 */ 42 public static float lerp(float begin, float end, float percent) { 43 return begin * (1 - percent) + end * percent; 44 } 45 46 /** 47 * Clamps a value between two bounds inclusively. 48 * 49 * @param value Value to be clamped 50 * @param min Lower bound 51 * @param max Upper bound 52 * @return Clamped value 53 */ 54 public static float clamp(float value, float min, float max) { 55 return Math.max(min, Math.min(value, max)); 56 } 57 } 58