Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2009 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 android.util;
     18 
     19 import java.util.Random;
     20 
     21 /**
     22  * A class that contains utility methods related to numbers.
     23  *
     24  * @hide Pending API council approval
     25  */
     26 public final class MathUtils {
     27     private static final Random sRandom = new Random();
     28     private static final float DEG_TO_RAD = 3.1415926f / 180.0f;
     29     private static final float RAD_TO_DEG = 180.0f / 3.1415926f;
     30 
     31     private MathUtils() {
     32     }
     33 
     34     public static float abs(float v) {
     35         return v > 0 ? v : -v;
     36     }
     37 
     38     public static int constrain(int amount, int low, int high) {
     39         return amount < low ? low : (amount > high ? high : amount);
     40     }
     41 
     42     public static long constrain(long amount, long low, long high) {
     43         return amount < low ? low : (amount > high ? high : amount);
     44     }
     45 
     46     public static float constrain(float amount, float low, float high) {
     47         return amount < low ? low : (amount > high ? high : amount);
     48     }
     49 
     50     public static float log(float a) {
     51         return (float) Math.log(a);
     52     }
     53 
     54     public static float exp(float a) {
     55         return (float) Math.exp(a);
     56     }
     57 
     58     public static float pow(float a, float b) {
     59         return (float) Math.pow(a, b);
     60     }
     61 
     62     public static float max(float a, float b) {
     63         return a > b ? a : b;
     64     }
     65 
     66     public static float max(int a, int b) {
     67         return a > b ? a : b;
     68     }
     69 
     70     public static float max(float a, float b, float c) {
     71         return a > b ? (a > c ? a : c) : (b > c ? b : c);
     72     }
     73 
     74     public static float max(int a, int b, int c) {
     75         return a > b ? (a > c ? a : c) : (b > c ? b : c);
     76     }
     77 
     78     public static float min(float a, float b) {
     79         return a < b ? a : b;
     80     }
     81 
     82     public static float min(int a, int b) {
     83         return a < b ? a : b;
     84     }
     85 
     86     public static float min(float a, float b, float c) {
     87         return a < b ? (a < c ? a : c) : (b < c ? b : c);
     88     }
     89 
     90     public static float min(int a, int b, int c) {
     91         return a < b ? (a < c ? a : c) : (b < c ? b : c);
     92     }
     93 
     94     public static float dist(float x1, float y1, float x2, float y2) {
     95         final float x = (x2 - x1);
     96         final float y = (y2 - y1);
     97         return (float) Math.hypot(x, y);
     98     }
     99 
    100     public static float dist(float x1, float y1, float z1, float x2, float y2, float z2) {
    101         final float x = (x2 - x1);
    102         final float y = (y2 - y1);
    103         final float z = (z2 - z1);
    104         return (float) Math.sqrt(x * x + y * y + z * z);
    105     }
    106 
    107     public static float mag(float a, float b) {
    108         return (float) Math.hypot(a, b);
    109     }
    110 
    111     public static float mag(float a, float b, float c) {
    112         return (float) Math.sqrt(a * a + b * b + c * c);
    113     }
    114 
    115     public static float sq(float v) {
    116         return v * v;
    117     }
    118 
    119     public static float dot(float v1x, float v1y, float v2x, float v2y) {
    120         return v1x * v2x + v1y * v2y;
    121     }
    122 
    123     public static float cross(float v1x, float v1y, float v2x, float v2y) {
    124         return v1x * v2y - v1y * v2x;
    125     }
    126 
    127     public static float radians(float degrees) {
    128         return degrees * DEG_TO_RAD;
    129     }
    130 
    131     public static float degrees(float radians) {
    132         return radians * RAD_TO_DEG;
    133     }
    134 
    135     public static float acos(float value) {
    136         return (float) Math.acos(value);
    137     }
    138 
    139     public static float asin(float value) {
    140         return (float) Math.asin(value);
    141     }
    142 
    143     public static float atan(float value) {
    144         return (float) Math.atan(value);
    145     }
    146 
    147     public static float atan2(float a, float b) {
    148         return (float) Math.atan2(a, b);
    149     }
    150 
    151     public static float tan(float angle) {
    152         return (float) Math.tan(angle);
    153     }
    154 
    155     public static float lerp(float start, float stop, float amount) {
    156         return start + (stop - start) * amount;
    157     }
    158 
    159     /**
    160      * Returns an interpolated angle in degrees between a set of start and end
    161      * angles.
    162      * <p>
    163      * Unlike {@link #lerp(float, float, float)}, the direction and distance of
    164      * travel is determined by the shortest angle between the start and end
    165      * angles. For example, if the starting angle is 0 and the ending angle is
    166      * 350, then the interpolated angle will be in the range [0,-10] rather
    167      * than [0,350].
    168      *
    169      * @param start the starting angle in degrees
    170      * @param end the ending angle in degrees
    171      * @param amount the position between start and end in the range [0,1]
    172      *               where 0 is the starting angle and 1 is the ending angle
    173      * @return the interpolated angle in degrees
    174      */
    175     public static float lerpDeg(float start, float end, float amount) {
    176         final float minAngle = (((end - start) + 180) % 360) - 180;
    177         return minAngle * amount + start;
    178     }
    179 
    180     public static float norm(float start, float stop, float value) {
    181         return (value - start) / (stop - start);
    182     }
    183 
    184     public static float map(float minStart, float minStop, float maxStart, float maxStop, float value) {
    185         return maxStart + (maxStart - maxStop) * ((value - minStart) / (minStop - minStart));
    186     }
    187 
    188     public static int random(int howbig) {
    189         return (int) (sRandom.nextFloat() * howbig);
    190     }
    191 
    192     public static int random(int howsmall, int howbig) {
    193         if (howsmall >= howbig) return howsmall;
    194         return (int) (sRandom.nextFloat() * (howbig - howsmall) + howsmall);
    195     }
    196 
    197     public static float random(float howbig) {
    198         return sRandom.nextFloat() * howbig;
    199     }
    200 
    201     public static float random(float howsmall, float howbig) {
    202         if (howsmall >= howbig) return howsmall;
    203         return sRandom.nextFloat() * (howbig - howsmall) + howsmall;
    204     }
    205 
    206     public static void randomSeed(long seed) {
    207         sRandom.setSeed(seed);
    208     }
    209 
    210     /**
    211      * Returns the sum of the two parameters, or throws an exception if the resulting sum would
    212      * cause an overflow or underflow.
    213      * @throws IllegalArgumentException when overflow or underflow would occur.
    214      */
    215     public static int addOrThrow(int a, int b) throws IllegalArgumentException {
    216         if (b == 0) {
    217             return a;
    218         }
    219 
    220         if (b > 0 && a <= (Integer.MAX_VALUE - b)) {
    221             return a + b;
    222         }
    223 
    224         if (b < 0 && a >= (Integer.MIN_VALUE - b)) {
    225             return a + b;
    226         }
    227         throw new IllegalArgumentException("Addition overflow: " + a + " + " + b);
    228     }
    229 }
    230