Example usage for java.lang Math min

List of usage examples for java.lang Math min

Introduction

In this page you can find the example usage for java.lang Math min.

Prototype

@HotSpotIntrinsicCandidate
public static double min(double a, double b) 

Source Link

Document

Returns the smaller of two double values.

Usage

From source file:Main.java

public static int roundOrientation(int orientation, int orientationHistory) {
    boolean changeOrientation = false;
    if (orientationHistory == OrientationEventListener.ORIENTATION_UNKNOWN) {
        changeOrientation = true;/*w ww.  ja v  a  2 s .c o  m*/
    } else {
        int dist = Math.abs(orientation - orientationHistory);
        dist = Math.min(dist, 360 - dist);
        changeOrientation = (dist >= 45 + ORIENTATION_HYSTERESIS);
    }
    if (changeOrientation) {
        return ((orientation + 45) / 90 * 90) % 360;
    }
    return orientationHistory;
}

From source file:Main.java

/**
 * Converts the given pair of arrays into a sorted map that maps each
 * keys[i] to the corresponding values[i].
 * @return the map (empty map if keys == null or values == null)
 *//*w  ww. ja v a  2  s  .  c om*/
public static <K extends Comparable<? super K>, V> Map<K, V> asMapSorted(K[] keys, V[] values) {
    Map<K, V> map = new TreeMap<K, V>();
    if (keys == null || values == null) {
        return map;
    }

    for (int i = 0, len = Math.min(keys.length, values.length); i < len; i++) {
        map.put(keys[i], values[i]);
    }
    return map;
}

From source file:Main.java

public static Bitmap resizeImage(String path, int targetH, int targetW) {

    Options bmOptions = new Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    int scaleFactor = Math.min(photoW / targetW, photoH / targetH);

    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;
    bmOptions.inPurgeable = true;/*from   w w  w.j  a  v a2s  . c  o  m*/

    return BitmapFactory.decodeFile(path, bmOptions);

}

From source file:Main.java

public static byte[] copyOf(byte[] original, int newLength) {
    byte[] copy = new byte[newLength];
    System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));
    return copy;//w w  w  . j  a v a 2 s  .c  o  m
}

From source file:Main.java

public static double round(double d) {
    if (Double.isNaN(d) || Double.isInfinite(d))
        return d;
    int digits = leadingDigits(d);
    int roundToDecimal = Math.min(MAXDIGIT - digits, MAXDECIMAL);
    double rounded = BigDecimal.valueOf(d).setScale(roundToDecimal, BigDecimal.ROUND_HALF_UP).doubleValue();
    return rounded;
}

From source file:Main.java

/**
 * Return true if the smallest width in DP of the device is equal or greater than the given
 * value.//from   ww  w .j  av a 2s.  c  o m
 */
public static boolean isScreenSw(int smallestWidthDp) {
    DisplayMetrics displayMetrics = Resources.getSystem().getDisplayMetrics();
    float widthDp = displayMetrics.widthPixels / displayMetrics.density;
    float heightDp = displayMetrics.heightPixels / displayMetrics.density;
    float screenSw = Math.min(widthDp, heightDp);
    return screenSw >= smallestWidthDp;
}

From source file:Main.java

/**
 * Compares two arrays of comparable elements lexicographically.
 * @param arr1 first array/*ww  w.jav  a 2  s .c  om*/
 * @param arr2 second array
 * @param <T> comparable type of array elements
 * @return negative integer if {@code arr1} comes before {@code arr2},
 *         positive integer if {@code arr1} comes after {@code arr2},
 *         {@code 0} if arrays equal
 */
public static <T extends Comparable<T>> int compare(T[] arr1, T[] arr2) {
    int length = Math.min(arr1.length, arr2.length);
    for (int i = 0; i < length; i++) {
        int compare = arr1[i].compareTo(arr2[i]);
        if (compare != 0)
            return compare;
    }
    return arr1.length - arr2.length;
}

From source file:Main.java

/**
 * Clamps a value between lower and upper bounds. Formally, given value v,
 * and an interval [lower, upper], lower <= clamp(v, lower, upper) <= upper holds true
 *
 * @param v     the value to clamp//from  w w  w  .j av a 2 s .c om
 * @param lower the lower bound of the interval
 * @param upper the upper bound of the interval
 * @return the clamped value of v in the [lower, upper] interval
 */
public static float clamp(float v, float lower, float upper) {

    return Math.max(lower, Math.min(upper, v));
}

From source file:Main.java

/**
 * Method returning color with modified alpha proportional to given values.
 *
 * @param color     color to modify// w  ww.j  a  va2s .c  o  m
 * @param fullValue total value
 * @param partValue part of fullValue. When partValue equals fullValue, the alpha is 255.
 * @return color with alpha relative to partValue/fullValue ratio.
 */
public static int getColorWithProportionalAlpha(int color, float fullValue, float partValue) {
    float progress = Math.min(Math.max(partValue, 0), fullValue) / fullValue;
    return Color.argb(Math.round(Color.alpha(color) * progress), Color.red(color), Color.green(color),
            Color.blue(color));
}

From source file:Main.java

private static Bitmap pictureDrawable2Bitmap(PictureDrawable pictureDrawable) {
    float intrinsicWidth = (float) pictureDrawable.getIntrinsicWidth();
    float intrinsicHeight = (float) pictureDrawable.getIntrinsicHeight();
    float f = 1.0f;
    if (intrinsicWidth < 600.0f || intrinsicHeight < 600.0f) {
        f = Math.min(600.0f / intrinsicWidth, 600.0f / intrinsicHeight);
        intrinsicWidth = (intrinsicWidth * f) + 0.5f;
        intrinsicHeight = (intrinsicHeight * f) + 0.5f;
    }/*from   w  ww .  j  a  va  2s  .  c om*/
    Bitmap createBitmap = Bitmap.createBitmap((int) intrinsicWidth, (int) intrinsicHeight, Config.ARGB_8888);
    Canvas canvas = new Canvas(createBitmap);
    Matrix matrix = new Matrix();
    matrix.preScale(f, f);
    canvas.concat(matrix);
    canvas.drawPicture(pictureDrawable.getPicture());
    return createBitmap;
}