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 Bitmap resizeAndCropCenter(Bitmap bitmap, int size, boolean recycle) {
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    if (w == size && h == size)
        return bitmap;

    // scale the image so that the shorter side equals to the target;
    // the longer side will be center-cropped.
    float scale = (float) size / Math.min(w, h);

    Bitmap target = Bitmap.createBitmap(size, size, getConfig(bitmap));
    int width = Math.round(scale * bitmap.getWidth());
    int height = Math.round(scale * bitmap.getHeight());
    Canvas canvas = new Canvas(target);
    canvas.translate((size - width) / 2f, (size - height) / 2f);
    canvas.scale(scale, scale);/*from w ww  .ja v a 2s  . c  om*/
    Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
    canvas.drawBitmap(bitmap, 0, 0, paint);
    if (recycle)
        bitmap.recycle();
    return target;
}

From source file:Main.java

private static int computeInitialSampleSize(int w, int h, int minSideLength, int maxNumOfPixels) {
    if (maxNumOfPixels == UNCONSTRAINED && minSideLength == UNCONSTRAINED)
        return 1;

    int lowerBound = (maxNumOfPixels == UNCONSTRAINED) ? 1
            : (int) Math.ceil(Math.sqrt((float) (w * h) / maxNumOfPixels));

    if (minSideLength == UNCONSTRAINED) {
        return lowerBound;
    } else {/*  w w w.j  a va 2 s . c  o  m*/
        int sampleSize = Math.min(w / minSideLength, h / minSideLength);
        return Math.max(sampleSize, lowerBound);
    }
}

From source file:Main.java

private static int computeInitialSampleSize(int w, int h, int minSideLength, int maxNumOfPixels) {
    if (maxNumOfPixels == UNCONSTRAINED && minSideLength == UNCONSTRAINED)
        return 1;

    int lowerBound = (maxNumOfPixels == UNCONSTRAINED) ? 1
            : (int) Math.ceil(Math.sqrt((double) (w * h) / maxNumOfPixels));

    if (minSideLength == UNCONSTRAINED) {
        return lowerBound;
    } else {/*from  ww  w  .j a v a 2 s  .c  o  m*/
        int sampleSize = Math.min(w / minSideLength, h / minSideLength);
        return Math.max(sampleSize, lowerBound);
    }
}

From source file:Main.java

public static int clamp(final int num, final int bound1, final int bound2) {
    final int max = Math.max(bound1, bound2), min = Math.min(bound1, bound2);
    return Math.max(Math.min(num, max), min);
}

From source file:Main.java

/**
 * Retrieve a byte from a byte array.//w  w w.  ja  v  a2 s . com
 * 
 * @param a
 *        the byte array to look in
 * @param fromIndex
 *        the position from which to start looking
 * @param toIndex
 *        the position up to which to look
 * @param key
 *        the byte to find
 * @return the position of the byte in the array, or -1 if the byte was not found in the array
 */
public static int find(byte[] a, int fromIndex, int toIndex, byte key) {
    int result = -1;

    if (fromIndex < 0) {
        fromIndex = 0;
    }
    toIndex = Math.min(toIndex, a.length);

    for (int i = fromIndex; fromIndex < toIndex && result == -1 && i < toIndex; i++) {
        if (a[i] == key) {
            result = i;
        }
    }

    return result;
}

From source file:Main.java

public static boolean eLineOnLine(double x1, double y1, double x2, double y2, double x3, double y3, double x4,
        double y4) {
    double k1 = (y2 - y1) / (x2 - x1);
    double k2 = (y4 - y3) / (x4 - x3);
    if (k1 == k2) {
        return false;
    } else {//w ww .  j av a2  s.c o  m
        double x = ((x1 * y2 - y1 * x2) * (x3 - x4) - (x3 * y4 - y3 * x4) * (x1 - x2))
                / ((y2 - y1) * (x3 - x4) - (y4 - y3) * (x1 - x2));
        double y = (x1 * y2 - y1 * x2 - x * (y2 - y1)) / (x1 - x2);
        if (x >= Math.min(x1, x2) && x <= Math.max(x1, x2) && y >= Math.min(y1, y2) && y <= Math.max(y1, y2)) {
            return true;
        } else {
            return false;
        }
    }
}

From source file:Main.java

public static boolean eLineOnELine(double x1, double y1, double x2, double y2, double x3, double y3, double x4,
        double y4) {
    double k1 = (y2 - y1) / (x2 - x1);
    double k2 = (y4 - y3) / (x4 - x3);
    if (k1 == k2) {
        return false;
    } else {//from   w  w w.j  a  va  2 s  .c om
        double x = ((x1 * y2 - y1 * x2) * (x3 - x4) - (x3 * y4 - y3 * x4) * (x1 - x2))
                / ((y2 - y1) * (x3 - x4) - (y4 - y3) * (x1 - x2));
        double y = (x1 * y2 - y1 * x2 - x * (y2 - y1)) / (x1 - x2);
        if (x >= Math.min(x1, x2) && x <= Math.max(x1, x2) && y >= Math.min(y1, y2) && y <= Math.max(y1, y2)
                && x >= Math.min(x3, x4) && x <= Math.max(x3, x4) && y >= Math.min(y3, y4)
                && y <= Math.max(y3, y4)) {
            return true;
        } else {
            return false;
        }
    }
}

From source file:Main.java

/**
 * Displays a message dialog with given information.
 *//*from  w  ww .j  av  a2  s. c  o  m*/
public static void showInformationDialog(Component component, String message) {
    final JPanel panel = new JPanel(new BorderLayout(5, 5));

    final JLabel messageLabel = new JLabel(message);
    messageLabel.setFont(new Font("Dialog", Font.BOLD, messageLabel.getFont().getSize()));

    panel.add(messageLabel, BorderLayout.CENTER);

    // Adjust stack trace dimensions
    final Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
    screenDimension.setSize(screenDimension.getWidth() * 0.7, screenDimension.getHeight() * 0.7);
    final Dimension maxStackTraceDimension = new Dimension(500, 500);
    maxStackTraceDimension.setSize(Math.min(maxStackTraceDimension.getWidth(), screenDimension.getWidth()),
            Math.min(maxStackTraceDimension.getHeight(), screenDimension.getHeight()));

    JOptionPane.showMessageDialog(component, panel, "Information", JOptionPane.INFORMATION_MESSAGE);
}

From source file:Main.java

@TargetApi(21)
public static AdvertiseData getFatBeaconAdvertisementData(byte[] fatBeaconAdvertisement) {

    // Manually build the advertising info
    int length = Math.min(fatBeaconAdvertisement.length, 17);
    byte[] beaconData = new byte[length + 3];
    System.arraycopy(fatBeaconAdvertisement, 0, beaconData, 3, length);
    beaconData[0] = URL_FRAME_TYPE;
    beaconData[1] = (byte) 0xBA;
    beaconData[2] = FAT_BEACON;//w w w  .j a v a 2s . c  om
    return new AdvertiseData.Builder().setIncludeTxPowerLevel(false) // reserve advertising space for URI
            .addServiceData(EDDYSTONE_BEACON_UUID, beaconData)
            // Adding 0xFEAA to the "Service Complete List UUID 16" (0x3) for iOS compatibility
            .addServiceUuid(EDDYSTONE_BEACON_UUID).build();
}

From source file:Main.java

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