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

    float scale = Math.max((float) size / bitmap.getWidth(), (float) size / bitmap.getHeight());
    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);//  w w w. ja v  a2  s  .c o m
    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:MiscUtils.java

/**
* Compares two strings.<p>/*www .  j  a  va  2s .com*/
* <p/>
* Unlike <function>String.compareTo()</function>,
* this method correctly recognizes and handles embedded numbers.
* For example, it places "My file 2" before "My file 10".<p>
*
* @param str1       The first string
* @param str2       The second string
* @param ignoreCase If true, case will be ignored
* @return negative If str1 &lt; str2, 0 if both are the same,
*         positive if str1 &gt; str2
* @since jEdit 4.3pre5
*/
public static int compareStrings(String str1, String str2, boolean ignoreCase) {
    char[] char1 = str1.toCharArray();
    char[] char2 = str2.toCharArray();

    int len = Math.min(char1.length, char2.length);

    for (int i = 0, j = 0; i < len && j < len; i++, j++) {
        char ch1 = char1[i];
        char ch2 = char2[j];
        if (Character.isDigit(ch1) && Character.isDigit(ch2) && ch1 != '0' && ch2 != '0') {
            int _i = i + 1;
            int _j = j + 1;

            for (; _i < char1.length; _i++) {
                if (!Character.isDigit(char1[_i])) {
                    //_i--;
                    break;
                }
            }

            for (; _j < char2.length; _j++) {
                if (!Character.isDigit(char2[_j])) {
                    //_j--;
                    break;
                }
            }

            int len1 = _i - i;
            int len2 = _j - j;
            if (len1 > len2)
                return 1;
            else if (len1 < len2)
                return -1;
            else {
                for (int k = 0; k < len1; k++) {
                    ch1 = char1[i + k];
                    ch2 = char2[j + k];
                    if (ch1 != ch2)
                        return ch1 - ch2;
                }
            }

            i = _i - 1;
            j = _j - 1;
        } else {
            if (ignoreCase) {
                ch1 = Character.toLowerCase(ch1);
                ch2 = Character.toLowerCase(ch2);
            }

            if (ch1 != ch2)
                return ch1 - ch2;
        }
    }

    return char1.length - char2.length;
}

From source file:Main.java

public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
    int h = options.outHeight;
    int w = options.outWidth;
    int inSampleSize = 0;
    if (h > reqHeight || w > reqWidth) {
        float ratioW = (float) w / reqWidth;
        float ratioH = (float) h / reqHeight;
        inSampleSize = (int) Math.min(ratioH, ratioW);
    }//from   w  w w  .  java  2s .  c o m
    inSampleSize = Math.max(1, inSampleSize);
    return inSampleSize;
}

From source file:Main.java

/**
 * Make INTERSECTION operation between 2 {@link Collection}
 * //from  www.j a  v a 2  s.  c  o  m
 * @param a
 * @param b
 * @return
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Collection intersection(final Collection a, final Collection b) {
    ArrayList list = new ArrayList();
    Map mapa = getCardinalityMap(a);
    Map mapb = getCardinalityMap(b);
    Set elts = new LinkedHashSet(a);
    elts.addAll(b);
    Iterator it = elts.iterator();
    while (it.hasNext()) {
        Object obj = it.next();
        for (int i = 0, m = Math.min(getFreq(obj, mapa), getFreq(obj, mapb)); i < m; i++) {
            list.add(obj);
        }
    }
    return list;
}

From source file:Main.java

/**
 * Convert RGB color to CMYK color./*from w  w w .j  av a2s  . c o m*/
 *
 * @param rgbColor target color
 * @return CMYK array
 */
public static float[] cmykFromRgb(int rgbColor) {
    int red = (0xff0000 & rgbColor) >> 16;
    int green = (0xff00 & rgbColor) >> 8;
    int blue = (0xff & rgbColor);
    float black = Math.min(1.0f - red / 255.0f, Math.min(1.0f - green / 255.0f, 1.0f - blue / 255.0f));
    float cyan = 1.0f;
    float magenta = 1.0f;
    float yellow = 1.0f;
    if (black != 1.0f) {
        // black 1.0 causes zero divide
        cyan = (1.0f - (red / 255.0f) - black) / (1.0f - black);
        magenta = (1.0f - (green / 255.0f) - black) / (1.0f - black);
        yellow = (1.0f - (blue / 255.0f) - black) / (1.0f - black);
    }
    return new float[] { cyan, magenta, yellow, black };
}

From source file:Main.java

public static void showAsPopup(Activity activity) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
        return;/*from   w w w .ja  v  a  2  s .com*/
    }
    activity.requestWindowFeature(Window.FEATURE_ACTION_BAR);

    //To show activity as dialog and dim the background, you need to declare android:theme="@style/PopupTheme" on for the chosen activity on the manifest
    Window window = activity.getWindow();
    window.setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND, WindowManager.LayoutParams.FLAG_DIM_BEHIND);

    Display display = activity.getWindowManager().getDefaultDisplay();
    WindowManager.LayoutParams params = window.getAttributes();
    params.height = (int) (display.getHeight() * 0.95);
    params.width = Math.min((int) (display.getWidth() * 0.9), (int) (params.height * 0.85));
    params.gravity = Gravity.BOTTOM;
    params.alpha = 1.0f;
    params.dimAmount = 0.5f;
    window.setAttributes(params);
}

From source file:Main.java

public static double[] copyOf(double[] original, int newLength) {
    double[] copy = new double[newLength];
    System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));
    return copy;//  w  w w  .  j av  a2s.c  o  m
}

From source file:Main.java

/**
 * Get the string from the passed in textComponent representing the text displayed by the
 * component with all the soft new lines (added in from text wrapping) added in. 
 * /*from   w w  w . jav a2 s. co m*/
 * @param c
 * @return
 *    Returns null if comp does not have a size
 */
public static String getWrappedText(JTextComponent c) {
    int len = c.getDocument().getLength();
    int offset = 0;

    // Increase 10% for extra newlines
    StringBuffer buf = new StringBuffer((int) (len * 1.10));

    try {
        while (offset < len) {
            int end = Utilities.getRowEnd(c, offset);
            if (end < 0) {
                break;
            }

            // Include the last character on the line
            end = Math.min(end + 1, len);

            String s = c.getDocument().getText(offset, end - offset);
            buf.append(s);

            // Add a newline if s does not have one
            if (!s.endsWith("\n")) {
                buf.append('\n');
            }
            offset = end;
        }
    } catch (BadLocationException e) {
    }
    return buf.toString();
}

From source file:Main.java

private static int computeInitialSampleSize(BitmapFactory.Options options, int minSideLength,
        int maxNumOfPixels) {
    double w = options.outWidth;
    double h = options.outHeight;
    int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
    int upperBound = (minSideLength == -1) ? 128
            : (int) Math.min(Math.floor(w / minSideLength), Math.floor(h / minSideLength));
    if (upperBound < lowerBound) {
        // return the larger one when there is no overlapping zone.
        return lowerBound;
    }/*www  .  j  ava  2 s  .  c om*/
    if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
        return 1;
    } else if (minSideLength == -1) {
        return lowerBound;
    } else {
        return upperBound;
    }
}

From source file:Main.java

private static int computeInitialSampleSize(BitmapFactory.Options options, int minSideLength,
        int maxNumOfPixels) {
    double w = options.outWidth;
    double h = options.outHeight;

    int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
    int upperBound = (minSideLength == -1) ? 1280
            : (int) Math.min(Math.floor(w / minSideLength), Math.floor(h / minSideLength));

    if (upperBound < lowerBound) {
        // return the larger one when there is no overlapping zone.
        return lowerBound;
    }//from w w w  .  ja  v a  2s .c  o m

    if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
        return 1;
    } else if (minSideLength == -1) {
        return lowerBound;
    } else {
        return upperBound;
    }
}