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     public static float norm(float start, float stop, float value) {
    160         return (value - start) / (stop - start);
    161     }
    162 
    163     public static float map(float minStart, float minStop, float maxStart, float maxStop, float value) {
    164         return maxStart + (maxStart - maxStop) * ((value - minStart) / (minStop - minStart));
    165     }
    166 
    167     public static int random(int howbig) {
    168         return (int) (sRandom.nextFloat() * howbig);
    169     }
    170 
    171     public static int random(int howsmall, int howbig) {
    172         if (howsmall >= howbig) return howsmall;
    173         return (int) (sRandom.nextFloat() * (howbig - howsmall) + howsmall);
    174     }
    175 
    176     public static float random(float howbig) {
    177         return sRandom.nextFloat() * howbig;
    178     }
    179 
    180     public static float random(float howsmall, float howbig) {
    181         if (howsmall >= howbig) return howsmall;
    182         return sRandom.nextFloat() * (howbig - howsmall) + howsmall;
    183     }
    184 
    185     public static void randomSeed(long seed) {
    186         sRandom.setSeed(seed);
    187     }
    188 }
    189