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 float constrain(float amount, float low, float high) {
     43         return amount < low ? low : (amount > high ? high : amount);
     44     }
     45 
     46     public static float log(float a) {
     47         return (float) Math.log(a);
     48     }
     49 
     50     public static float exp(float a) {
     51         return (float) Math.exp(a);
     52     }
     53 
     54     public static float pow(float a, float b) {
     55         return (float) Math.pow(a, b);
     56     }
     57 
     58     public static float max(float a, float b) {
     59         return a > b ? a : b;
     60     }
     61 
     62     public static float max(int a, int b) {
     63         return a > b ? a : b;
     64     }
     65 
     66     public static float max(float a, float b, float c) {
     67         return a > b ? (a > c ? a : c) : (b > c ? b : c);
     68     }
     69 
     70     public static float max(int a, int b, int c) {
     71         return a > b ? (a > c ? a : c) : (b > c ? b : c);
     72     }
     73 
     74     public static float min(float a, float b) {
     75         return a < b ? a : b;
     76     }
     77 
     78     public static float min(int a, int b) {
     79         return a < b ? a : b;
     80     }
     81 
     82     public static float min(float a, float b, float c) {
     83         return a < b ? (a < c ? a : c) : (b < c ? b : c);
     84     }
     85 
     86     public static float min(int a, int b, int c) {
     87         return a < b ? (a < c ? a : c) : (b < c ? b : c);
     88     }
     89 
     90     public static float dist(float x1, float y1, float x2, float y2) {
     91         final float x = (x2 - x1);
     92         final float y = (y2 - y1);
     93         return (float) Math.sqrt(x * x + y * y);
     94     }
     95 
     96     public static float dist(float x1, float y1, float z1, float x2, float y2, float z2) {
     97         final float x = (x2 - x1);
     98         final float y = (y2 - y1);
     99         final float z = (z2 - z1);
    100         return (float) Math.sqrt(x * x + y * y + z * z);
    101     }
    102 
    103     public static float mag(float a, float b) {
    104         return (float) Math.sqrt(a * a + b * b);
    105     }
    106 
    107     public static float mag(float a, float b, float c) {
    108         return (float) Math.sqrt(a * a + b * b + c * c);
    109     }
    110 
    111     public static float sq(float v) {
    112         return v * v;
    113     }
    114 
    115     public static float radians(float degrees) {
    116         return degrees * DEG_TO_RAD;
    117     }
    118 
    119     public static float degrees(float radians) {
    120         return radians * RAD_TO_DEG;
    121     }
    122 
    123     public static float acos(float value) {
    124         return (float) Math.acos(value);
    125     }
    126 
    127     public static float asin(float value) {
    128         return (float) Math.asin(value);
    129     }
    130 
    131     public static float atan(float value) {
    132         return (float) Math.atan(value);
    133     }
    134 
    135     public static float atan2(float a, float b) {
    136         return (float) Math.atan2(a, b);
    137     }
    138 
    139     public static float tan(float angle) {
    140         return (float) Math.tan(angle);
    141     }
    142 
    143     public static float lerp(float start, float stop, float amount) {
    144         return start + (stop - start) * amount;
    145     }
    146 
    147     public static float norm(float start, float stop, float value) {
    148         return (value - start) / (stop - start);
    149     }
    150 
    151     public static float map(float minStart, float minStop, float maxStart, float maxStop, float value) {
    152         return maxStart + (maxStart - maxStop) * ((value - minStart) / (minStop - minStart));
    153     }
    154 
    155     public static int random(int howbig) {
    156         return (int) (sRandom.nextFloat() * howbig);
    157     }
    158 
    159     public static int random(int howsmall, int howbig) {
    160         if (howsmall >= howbig) return howsmall;
    161         return (int) (sRandom.nextFloat() * (howbig - howsmall) + howsmall);
    162     }
    163 
    164     public static float random(float howbig) {
    165         return sRandom.nextFloat() * howbig;
    166     }
    167 
    168     public static float random(float howsmall, float howbig) {
    169         if (howsmall >= howbig) return howsmall;
    170         return sRandom.nextFloat() * (howbig - howsmall) + howsmall;
    171     }
    172 
    173     public static void randomSeed(long seed) {
    174         sRandom.setSeed(seed);
    175     }
    176 }
    177