1 /* 2 * Copyright (C) 2011 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 package com.android.chimpchat.adb; 17 18 19 20 /** 21 * Linear Interpolation class. 22 */ 23 public class LinearInterpolator { 24 private final int steps; 25 26 /** 27 * Use our own Point class so we don't pull in java.awt.* just for this simple class. 28 */ 29 public static class Point { 30 private final int x; 31 private final int y; 32 Point(int x, int y)33 public Point(int x, int y) { 34 this.x = x; 35 this.y = y; 36 } 37 38 @Override toString()39 public String toString() { 40 return new StringBuilder(). 41 append("("). 42 append(x). 43 append(","). 44 append(y). 45 append(")").toString(); 46 } 47 48 49 @Override equals(Object obj)50 public boolean equals(Object obj) { 51 if (obj instanceof Point) { 52 Point that = (Point) obj; 53 return this.x == that.x && this.y == that.y; 54 } 55 return false; 56 } 57 58 @Override hashCode()59 public int hashCode() { 60 return 0x43125315 + x + y; 61 } 62 getX()63 public int getX() { 64 return x; 65 } 66 getY()67 public int getY() { 68 return y; 69 } 70 } 71 72 /** 73 * Callback interface to recieve interpolated points. 74 */ 75 public interface Callback { 76 /** 77 * Called once to inform of the start point. 78 */ start(Point point)79 void start(Point point); 80 /** 81 * Called once to inform of the end point. 82 */ end(Point point)83 void end(Point point); 84 /** 85 * Called at every step in-between start and end. 86 */ step(Point point)87 void step(Point point); 88 } 89 90 /** 91 * Create a new linear Interpolator. 92 * 93 * @param steps How many steps should be in a single run. This counts the intervals 94 * in-between points, so the actual number of points generated will be steps + 1. 95 */ LinearInterpolator(int steps)96 public LinearInterpolator(int steps) { 97 this.steps = steps; 98 } 99 100 // Copied from android.util.MathUtils since we couldn't link it in on the host. lerp(float start, float stop, float amount)101 private static float lerp(float start, float stop, float amount) { 102 return start + (stop - start) * amount; 103 } 104 105 /** 106 * Calculate the interpolated points. 107 * 108 * @param start The starting point 109 * @param end The ending point 110 * @param callback the callback to call with each calculated points. 111 */ interpolate(Point start, Point end, Callback callback)112 public void interpolate(Point start, Point end, Callback callback) { 113 int xDistance = Math.abs(end.getX() - start.getX()); 114 int yDistance = Math.abs(end.getY() - start.getY()); 115 float amount = (float) (1.0 / steps); 116 117 118 callback.start(start); 119 for (int i = 1; i < steps; i++) { 120 float newX = lerp(start.getX(), end.getX(), amount * i); 121 float newY = lerp(start.getY(), end.getY(), amount * i); 122 123 callback.step(new Point(Math.round(newX), Math.round(newY))); 124 } 125 // Generate final point 126 callback.end(end); 127 } 128 } 129