1 /*
2  * Copyright (C) 2014 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.camera.ui.motion;
18 
19 /**
20  * Various static helper functions for interpolating between values.
21  */
22 public class InterpolateUtils {
23 
InterpolateUtils()24     private InterpolateUtils() {
25     }
26 
27     /**
28      * Linear interpolation from v0 to v1 as t goes from 0...1
29      *
30      * @param v0 the value at t=0
31      * @param v1 the value at t=1
32      * @param t value in the range of 0 to 1.
33      * @return the value between v0 and v1 as a ratio between 0 and 1 defined by t.
34      */
lerp(float v0, float v1, float t)35     public static float lerp(float v0, float v1, float t) {
36         return v0 + t * (v1 - v0);
37     }
38 
39     /**
40      * Project a value that is within the in(Min/Max) number space into the to(Min/Max) number
41      * space.
42      *
43      * @param v value to scale into the 'to' number space.
44      * @param vMin min value of the values number space.
45      * @param vMax max value of the values number space.
46      * @param pMin min value of the projection number space.
47      * @param pMax max value of the projection number space.
48      * @return the ratio of the value in the source number space as a value in the to(Min/Max)
49      * number space.
50      */
scale(float v, float vMin, float vMax, float pMin, float pMax)51     public static float scale(float v, float vMin, float vMax, float pMin, float pMax) {
52         return (pMax - pMin) * (v - vMin) / (vMax - vMin) + pMin;
53     }
54 
55     /**
56      * Value between 0 and 1 as a ratio between tBegin over tDuration
57      * with no upper bound.
58      */
unitRatio(long t, long tBegin, float tDuration)59     public static float unitRatio(long t, long tBegin, float tDuration) {
60         if (t <= tBegin) {
61             return 0.0f;
62         }
63 
64         return (t - tBegin) / tDuration;
65     }
66 }
67