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

/**
 * Converts the given pair of arrays into a map that maps each keys[i] to
 * the corresponding values[i]./* ww  w  .  ja  v a2s  .  c o  m*/
 * @return the map (empty map if keys == null or values == null)
 */
public static <K extends Comparable<? super K>, V> Map<K, V> asMapSorted(List<K> keys, List<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.size(), values.size()); i < len; i++) {
        map.put(keys.get(i), values.get(i));
    }
    return map;
}

From source file:Main.java

/***
 * Downsize the bitmap to at most the indicated ratio of the max specified size
 * @param bm Bitmap to resize//from w w w  .j ava  2s. c  o  m
 * @param maxX pixels of max width
 * @param maxY pixels of max height
 * @param maxRatio The max ratio wrt the display size
 * @return the new bitmap, OR input bitmap if no change needed
 */
public static Bitmap getResizedBitmap(Bitmap bm, int maxX, int maxY, double maxRatio) {

    // we have an starting bitmap object to work from
    if (null == bm) {
        return bm;
    }

    // Get current size and h:w ratio
    int height = bm.getHeight();
    int width = bm.getWidth();

    // ensure bitmap size is valid
    if (0 == height || 0 == width) {
        return bm;
    }

    // What is the height to width ratio - will always be > 0 at this point
    double ratio = height / width;

    // Figure out new max size
    int newHeight = (int) (Math.min(maxX, maxY) * maxRatio);
    int newWidth = (int) (newHeight / ratio);

    // If we don't need to downsize, then return with the original
    if (newHeight >= height && newWidth >= width) {
        return bm;
    }

    // Calculate the scaling factors in both the x and y direction
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;

    // create a matrix for the manipulation
    Matrix matrix = new Matrix();

    // resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);

    // recreate the new Bitmap, allowing for failure
    try {
        Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
        return resizedBitmap;
    } catch (Exception e) {
        return bm;
    }
}

From source file:Main.java

/**
 * Centers a Window on the screen<p>
 * Sets the window on the top left corner if the window's
 * dimensions are bigger than the screen's.
 *
 * @param window a Window object//from w  w w .  j  a  va 2  s .c  o m
 */
public static void centerOnScreen(Window window) {
    Point center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
    Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();

    double w = Math.min(window.getWidth(), screen.width);
    double h = Math.min(window.getHeight(), screen.height);
    int x = (int) (center.x - (w / 2));
    int y = (int) (center.y - (h / 2));

    Point corner = new Point((x >= 0 ? x : 0), (y >= 0 ? y : 0));

    window.setLocation(corner);
}

From source file:ImageClip.java

/**
 * Clips the input image to the specified shape
 * /*from  w  w  w .j  a  va 2s  .c o m*/
 * @param image
 *           the input image
 * @param clipVerts
 *           list of x, y pairs defining the clip shape, normalised
 *           to image dimensions (think texture coordinates)
 * @return The smallest image containing those pixels that fall
 *         inside the clip shape
 */
public static BufferedImage clip(BufferedImage image, float... clipVerts) {
    assert clipVerts.length >= 6;
    assert clipVerts.length % 2 == 0;

    int[] xp = new int[clipVerts.length / 2];
    int[] yp = new int[xp.length];

    int minX = image.getWidth(), minY = image.getHeight(), maxX = 0, maxY = 0;

    for (int j = 0; j < xp.length; j++) {
        xp[j] = Math.round(clipVerts[2 * j] * image.getWidth());
        yp[j] = Math.round(clipVerts[2 * j + 1] * image.getHeight());

        minX = Math.min(minX, xp[j]);
        minY = Math.min(minY, yp[j]);
        maxX = Math.max(maxX, xp[j]);
        maxY = Math.max(maxY, yp[j]);
    }

    for (int i = 0; i < xp.length; i++) {
        xp[i] -= minX;
        yp[i] -= minY;
    }

    Polygon clip = new Polygon(xp, yp, xp.length);
    BufferedImage out = new BufferedImage(maxX - minX, maxY - minY, image.getType());
    Graphics g = out.getGraphics();
    g.setClip(clip);

    g.drawImage(image, -minX, -minY, null);
    g.dispose();

    return out;
}

From source file:Main.java

public static boolean isSmallTablet() {
    float minSide = Math.min(displaySize.x, displaySize.y) / density;
    return minSide <= 700;
}

From source file:Main.java

/**
 * Make the color brighter by the specified factor.
 * <p/>//from  w w w  .  j a va 2 s  .c  o  m
 * This code is adapted from java.awt.Color to take in a factor.
 * Therefore, it inherits its quirks. Most importantly,
 * a smaller factor makes the color more bright.
 *
 * @param c      a color
 * @param factor the smaller the factor, the brighter.
 * @return a brighter color
 */
private static Color brighter(Color c, double factor) {
    int r = c.getRed();
    int g = c.getGreen();
    int b = c.getBlue();

    /* From 2D group:
     * 1. black.brighter() should return grey
     * 2. applying brighter to blue will always return blue, brighter
     * 3. non pure color (non zero rgb) will eventually return white
     */
    int i = (int) (1.0 / (1.0 - factor));
    if (r == 0 && g == 0 && b == 0) {
        return new Color(i, i, i);
    }
    if (r > 0 && r < i)
        r = i;
    if (g > 0 && g < i)
        g = i;
    if (b > 0 && b < i)
        b = i;

    return new Color(Math.min((int) (r / factor), 255), Math.min((int) (g / factor), 255),
            Math.min((int) (b / factor), 255));
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static final <T, U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
    T[] copy = ((Object) newType == (Object) Object[].class) ? (T[]) new Object[newLength]
            : (T[]) Array.newInstance(newType.getComponentType(), newLength);
    System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));
    return copy;/*  www  .  ja  va 2  s  .  c o  m*/
}

From source file:Main.java

/**
 * Returns minimum insets combined from the specified ones.
 *
 * @param insets1 first insets//from w w  w .j a v a  2  s . c  om
 * @param insets2 second insets
 * @return minimum insets
 */
public static Insets min(final Insets insets1, final Insets insets2) {
    if (insets1 != null && insets2 != null) {
        return new Insets(Math.min(insets1.top, insets2.top), Math.min(insets1.left, insets2.left),
                Math.min(insets1.bottom, insets2.bottom), Math.min(insets1.right, insets2.right));
    } else if (insets1 != null) {
        return insets1;
    } else if (insets2 != null) {
        return insets2;
    } else {
        return null;
    }
}

From source file:Main.java

/**
 * Copies the specified array, truncating or padding with nulls (if necessary)
 * so the copy has the specified length. For all indices that are valid in
 * both the original array and the copy, the two arrays will contain identical
 * values. For any indices that are valid in the copy but not the original,
 * the copy will contain null. Such indices will exist if and only if the
 * specified length is greater than that of the original array. The
 * resulting array is of exactly the same class as the original array.
 *
 * @param original the array to be copied
 * @param newLength the length of the copy to be returned
 * @return a copy of the original array, truncated or padded with nulls to
 * obtain the specified length/*from www  . j  av  a 2  s . c om*/
 */
public static <T> T[] copyOf(T[] original, int newLength) {
    @SuppressWarnings({ "unchecked" })
    T[] copy = (T[]) Array.newInstance(original.getClass().getComponentType(), newLength);
    System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));
    return copy;
}

From source file:Main.java

public static char[] copyOfRange(final char[] original, final int from, final int to) {
    final int newLength = to - from;
    if (newLength < 0) {
        throw new IllegalArgumentException(from + " > " + to);
    }/*from   w ww .  j ava2  s.  co  m*/
    final char[] copy = new char[newLength];
    System.arraycopy(original, from, copy, 0, Math.min(original.length - from, newLength));
    return copy;
}