Example usage for java.lang Math max

List of usage examples for java.lang Math max

Introduction

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

Prototype

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

Source Link

Document

Returns the greater of two double values.

Usage

From source file:Main.java

private static void saveSmallerImage(String pathOfInputImage, int dstWidth, int dstHeight) {
    try {//from w w  w .j ava2  s  .  c  o m
        int inWidth = 0;
        int inHeight = 0;

        InputStream in = new FileInputStream(pathOfInputImage);

        // decode image size (decode metadata only, not the whole image)
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(in, null, options);
        in.close();
        in = null;

        // save width and height
        inWidth = options.outWidth;
        inHeight = options.outHeight;

        // decode full image pre-resized
        in = new FileInputStream(pathOfInputImage);
        options = new BitmapFactory.Options();
        // calc rought re-size (this is no exact resize)
        options.inSampleSize = Math.max(inWidth / dstWidth, inHeight / dstHeight);
        // decode full image
        Bitmap roughBitmap = BitmapFactory.decodeStream(in, null, options);

        // calc exact destination size
        Matrix m = new Matrix();
        RectF inRect = new RectF(0, 0, roughBitmap.getWidth(), roughBitmap.getHeight());
        RectF outRect = new RectF(0, 0, dstWidth, dstHeight);
        m.setRectToRect(inRect, outRect, Matrix.ScaleToFit.CENTER);
        float[] values = new float[9];
        m.getValues(values);

        // resize bitmap
        Bitmap resizedBitmap = Bitmap.createScaledBitmap(roughBitmap,
                (int) (roughBitmap.getWidth() * values[0]), (int) (roughBitmap.getHeight() * values[4]), true);

        // save image
        try {
            FileOutputStream out = new FileOutputStream(pathOfInputImage);
            resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);
        } catch (Exception e) {
            Log.e("Image", e.getMessage(), e);
        }
    } catch (IOException e) {
        Log.e("Image", e.getMessage(), e);
    } catch (Exception e) {
        Log.e("Image", e.getMessage());
    }
}

From source file:Main.java

/**
 * Fills the array with random floats.  Values will be between min (inclusive) and
 * max (inclusive)./*from   w ww  . j  av a2s  .  c  om*/
 */
public static void genRandomFloats(long seed, float min, float max, float array[], boolean includeExtremes) {
    Random r = new Random(seed);
    int minExponent = Math.min(Math.getExponent(min), 0);
    int maxExponent = Math.max(Math.getExponent(max), 0);
    if (minExponent < -6 || maxExponent > 6) {
        // Use an exponential distribution
        int exponentDiff = maxExponent - minExponent;
        for (int i = 0; i < array.length; i++) {
            float mantissa = r.nextFloat();
            int exponent = minExponent + r.nextInt(maxExponent - minExponent);
            int sign = (min >= 0) ? 1 : 1 - r.nextInt(2) * 2; // -1 or 1
            float rand = sign * mantissa * (float) Math.pow(2.0, exponent);
            if (rand < min || rand > max) {
                continue;
            }
            array[i] = rand;
        }
    } else {
        // Use a linear distribution
        for (int i = 0; i < array.length; i++) {
            float rand = r.nextFloat();
            array[i] = min + rand * (max - min);
        }
    }
    // Seed a few special numbers we want to be sure to test.
    for (int i = 0; i < sInterestingDoubles.length; i++) {
        float f = (float) sInterestingDoubles[i];
        if (min <= f && f <= max) {
            array[r.nextInt(array.length)] = f;
        }
    }
    array[r.nextInt(array.length)] = min;
    array[r.nextInt(array.length)] = max;
    if (includeExtremes) {
        array[r.nextInt(array.length)] = Float.NaN;
        array[r.nextInt(array.length)] = Float.POSITIVE_INFINITY;
        array[r.nextInt(array.length)] = Float.NEGATIVE_INFINITY;
        array[r.nextInt(array.length)] = Float.MIN_VALUE;
        array[r.nextInt(array.length)] = Float.MIN_NORMAL;
        array[r.nextInt(array.length)] = Float.MAX_VALUE;
        array[r.nextInt(array.length)] = -Float.MIN_VALUE;
        array[r.nextInt(array.length)] = -Float.MIN_NORMAL;
        array[r.nextInt(array.length)] = -Float.MAX_VALUE;
    }
}

From source file:Main.java

/**
 * Make a color scaled using a defined factor.
 *
 * @param color/*from  www  .ja  va2 s  . c o m*/
 *          the color to scale.
 * @param factor
 *          the factor to use.
 * @return the scaled color.
 */
public static Color getScaledColor(Color color, double factor) {
    if (color == null) {
        return null;
    }
    if (factor <= 1) {
        return new Color(Math.max((int) (color.getRed() * factor), 0),
                Math.max((int) (color.getGreen() * factor), 0), Math.max((int) (color.getBlue() * factor), 0),
                color.getAlpha());
    }
    int r = color.getRed();
    int g = color.getGreen();
    int b = color.getBlue();

    int i = (int) (1.0 / (factor - 1));
    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 / (2 - factor)), 255), Math.min((int) (g / (2 - factor)), 255),
            Math.min((int) (b / (2 - factor)), 255));

}

From source file:Main.java

public static Bitmap fastblur(Context context, Bitmap sentBitmap, int radius) {
    Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);
    if (radius < 1) {
        return (null);
    }/*  w  w  w . j a v a2 s  .c  o  m*/
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    int[] pix = new int[w * h];
    bitmap.getPixels(pix, 0, w, 0, 0, w, h);
    int wm = w - 1;
    int hm = h - 1;
    int wh = w * h;
    int div = radius + radius + 1;

    int r[] = new int[wh];
    int g[] = new int[wh];
    int b[] = new int[wh];
    int rsum, gsum, bsum, x, y, i, p, yp, yi, yw;

    int vmin[] = new int[Math.max(w, h)];
    int divsum = (div + 1) >> 1;
    divsum *= divsum;
    int temp = 256 * divsum;
    int dv[] = new int[temp];
    for (i = 0; i < temp; i++) {
        dv[i] = (i / divsum);
    }

    yw = yi = 0;

    int[][] stack = new int[div][3];

    int stackpointer;

    int stackstart;

    int[] sir;

    int rbs;

    int r1 = radius + 1;

    int routsum, goutsum, boutsum;

    int rinsum, ginsum, binsum;

    for (y = 0; y < h; y++) {

        rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;

        for (i = -radius; i <= radius; i++) {

            p = pix[yi + Math.min(wm, Math.max(i, 0))];

            sir = stack[i + radius];

            sir[0] = (p & 0xff0000) >> 16;

            sir[1] = (p & 0x00ff00) >> 8;

            sir[2] = (p & 0x0000ff);

            rbs = r1 - Math.abs(i);

            rsum += sir[0] * rbs;

            gsum += sir[1] * rbs;

            bsum += sir[2] * rbs;

            if (i > 0) {

                rinsum += sir[0];

                ginsum += sir[1];

                binsum += sir[2];

            } else {

                routsum += sir[0];

                goutsum += sir[1];

                boutsum += sir[2];

            }

        }

        stackpointer = radius;

        for (x = 0; x < w; x++) {

            r[yi] = dv[rsum];

            g[yi] = dv[gsum];

            b[yi] = dv[bsum];

            rsum -= routsum;

            gsum -= goutsum;

            bsum -= boutsum;

            stackstart = stackpointer - radius + div;

            sir = stack[stackstart % div];

            routsum -= sir[0];

            goutsum -= sir[1];

            boutsum -= sir[2];

            if (y == 0) {

                vmin[x] = Math.min(x + radius + 1, wm);

            }

            p = pix[yw + vmin[x]];

            sir[0] = (p & 0xff0000) >> 16;

            sir[1] = (p & 0x00ff00) >> 8;

            sir[2] = (p & 0x0000ff);

            rinsum += sir[0];

            ginsum += sir[1];

            binsum += sir[2];

            rsum += rinsum;

            gsum += ginsum;

            bsum += binsum;

            stackpointer = (stackpointer + 1) % div;

            sir = stack[(stackpointer) % div];

            routsum += sir[0];

            goutsum += sir[1];

            boutsum += sir[2];

            rinsum -= sir[0];

            ginsum -= sir[1];

            binsum -= sir[2];

            yi++;

        }

        yw += w;

    }

    for (x = 0; x < w; x++) {

        rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;

        yp = -radius * w;

        for (i = -radius; i <= radius; i++) {

            yi = Math.max(0, yp) + x;

            sir = stack[i + radius];

            sir[0] = r[yi];

            sir[1] = g[yi];

            sir[2] = b[yi];

            rbs = r1 - Math.abs(i);

            rsum += r[yi] * rbs;

            gsum += g[yi] * rbs;

            bsum += b[yi] * rbs;

            if (i > 0) {

                rinsum += sir[0];

                ginsum += sir[1];

                binsum += sir[2];

            } else {

                routsum += sir[0];

                goutsum += sir[1];

                boutsum += sir[2];

            }

            if (i < hm) {

                yp += w;

            }

        }

        yi = x;

        stackpointer = radius;

        for (y = 0; y < h; y++) {

            pix[yi] = (0xff000000 & pix[yi]) | (dv[rsum] << 16)

                    | (dv[gsum] << 8) | dv[bsum];

            rsum -= routsum;

            gsum -= goutsum;

            bsum -= boutsum;

            stackstart = stackpointer - radius + div;

            sir = stack[stackstart % div];

            routsum -= sir[0];

            goutsum -= sir[1];

            boutsum -= sir[2];

            if (x == 0) {

                vmin[y] = Math.min(y + r1, hm) * w;

            }

            p = x + vmin[y];

            sir[0] = r[p];

            sir[1] = g[p];

            sir[2] = b[p];

            rinsum += sir[0];

            ginsum += sir[1];

            binsum += sir[2];

            rsum += rinsum;

            gsum += ginsum;

            bsum += binsum;

            stackpointer = (stackpointer + 1) % div;

            sir = stack[stackpointer];

            routsum += sir[0];

            goutsum += sir[1];

            boutsum += sir[2];

            rinsum -= sir[0];

            ginsum -= sir[1];

            binsum -= sir[2];

            yi += w;

        }

    }

    bitmap.setPixels(pix, 0, w, 0, 0, w, h);

    return (bitmap);

}

From source file:Main.java

/**
 * Linearly interpolates a between two one-byte channels.  If the value is outside the range of
 * 0-255 (either because the an input is outside the range, or the ratio is outside the range of
 * 0.0-1.0), the results will be clamped to 0-255.
 *
 * @param a The start color, or the result if the {@code ratio} is 0.0.
 * @param b The end color, or the result if the {@code ratio} is 1.0.
 * @param ratio The ratio of {@code b}'s influence in the result, between 0.0 to 1.0.
 * @return The computed blend value.//  www.j  av a 2  s .  co m
 */
private static int clampedLerp(int a, int b, float ratio) {
    int rawResult = a + (int) ((b - a) * ratio);
    return Math.max(Math.min(rawResult, 255), 0);
}

From source file:Main.java

public static void setupXAxis(NumberAxis numberAxis,
        ObservableList<XYChart.Series<Number, Number>> seriesList) {
    long min = Long.MAX_VALUE;
    long max = 0;

    for (XYChart.Series<Number, ?> series : seriesList) {
        for (Data<Number, ?> data : series.getData()) {
            min = Math.min(data.getXValue().longValue(), min);
            max = Math.max(data.getXValue().longValue(), max);
        }// ww w. j  av a  2  s .  co  m
    }
    setupXAxis(numberAxis, min, max);
}

From source file:de.dhke.projects.cutil.collections.BagUtil.java

public static <T> Bag<T> unionBags(final Bag<T> b0, final Bag<T> b1, final Bag<T> targetBag) {
    targetBag.clear();//from ww  w.j  a  v a2 s. c om
    for (T item : b0) {
        final int count0 = b0.getCount(item);
        final int count1 = b1.getCount(item);
        targetBag.add(item, Math.max(count0, count1));
    }
    for (T item : b1) {
        if (!b0.contains(item)) {
            final int count1 = b1.getCount(item);
            targetBag.add(item, count1);
        }
    }
    return targetBag;
}

From source file:Main.java

/**
 * Fills the array with random doubles.  Values will be between min (inclusive) and
 * max (inclusive).//from   ww  w  .j  a v  a 2  s  .  c  o m
 */
public static void genRandomDoubles(long seed, double min, double max, double array[],
        boolean includeExtremes) {
    Random r = new Random(seed);
    int minExponent = Math.min(Math.getExponent(min), 0);
    int maxExponent = Math.max(Math.getExponent(max), 0);
    if (minExponent < -6 || maxExponent > 6) {
        // Use an exponential distribution
        int exponentDiff = maxExponent - minExponent;
        for (int i = 0; i < array.length; i++) {
            double mantissa = r.nextDouble();
            int exponent = minExponent + r.nextInt(maxExponent - minExponent);
            int sign = (min >= 0) ? 1 : 1 - r.nextInt(2) * 2; // -1 or 1
            double rand = sign * mantissa * Math.pow(2.0, exponent);
            if (rand < min || rand > max) {
                continue;
            }
            array[i] = rand;
        }
    } else {
        // Use a linear distribution
        for (int i = 0; i < array.length; i++) {
            double rand = r.nextDouble();
            array[i] = min + rand * (max - min);
        }
    }
    // Seed a few special numbers we want to be sure to test.
    for (int i = 0; i < sInterestingDoubles.length; i++) {
        double d = sInterestingDoubles[i];
        if (min <= d && d <= max) {
            array[r.nextInt(array.length)] = d;
        }
    }
    array[r.nextInt(array.length)] = min;
    array[r.nextInt(array.length)] = max;
    if (includeExtremes) {
        array[r.nextInt(array.length)] = Double.NaN;
        array[r.nextInt(array.length)] = Double.POSITIVE_INFINITY;
        array[r.nextInt(array.length)] = Double.NEGATIVE_INFINITY;
        array[r.nextInt(array.length)] = Double.MIN_VALUE;
        array[r.nextInt(array.length)] = Double.MIN_NORMAL;
        array[r.nextInt(array.length)] = Double.MAX_VALUE;
        array[r.nextInt(array.length)] = -Double.MIN_VALUE;
        array[r.nextInt(array.length)] = -Double.MIN_NORMAL;
        array[r.nextInt(array.length)] = -Double.MAX_VALUE;
    }
}

From source file:Main.java

public static int[] histogram(float[] dataPoints) {

    int[] buckets = { 0, 0, 0, 0, 0 }; // 5 buckets
    float minVal = 1000000.0F;
    float maxVal = -minVal;
    int numOfDataPoints = dataPoints.length;

    // Get the min and max values of the data points
    for (int i = 0; i < numOfDataPoints; i++) {

        minVal = Math.min(minVal, Math.abs(dataPoints[i]));
        maxVal = Math.max(maxVal, Math.abs(dataPoints[i]));
    }/*from  w  w w .ja v a 2  s.  c  o m*/

    float bucketSize = (maxVal - minVal) / 5;
    // float bucketSize = 0.002F;

    // Count the number of points for each bucket
    for (int i = 0; i < numOfDataPoints; i++) {

        float val = Math.abs(dataPoints[i]);
        if (val <= minVal + bucketSize)
            buckets[0] = buckets[0] + 1; // between minVal and minVal + bucketSize
        else if (val <= minVal + 2 * bucketSize)
            buckets[1] = buckets[1] + 1; // between minVal and minVal + 2* bucketSize
        else if (val <= minVal + 3 * bucketSize)
            buckets[2] = buckets[2] + 1; // between minVal and minVal + 3* bucketSize
        else if (val <= minVal + 4 * bucketSize)
            buckets[3] = buckets[3] + 1; // between minVal and minVal + 4* bucketSize
        else
            buckets[4] = buckets[4] + 1; // greater than minVal + 4* bucketSize
    }
    return buckets;
}

From source file:Main.java

public static int getLengthOfLongestWord(String string) {
    int longestWordLength = 0;
    Matcher m = Pattern.compile("\\s*(\\S+)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE).matcher(string);
    while (m.find()) {
        longestWordLength = Math.max(longestWordLength, m.group(1).length());
    }//  w ww.jav  a 2  s .c  o  m
    return longestWordLength;
}