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 Bitmap fastBlur(Context context, Bitmap sentBitmap, int radius) {

    Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);

    if (radius < 1) {
        return (null);
    }//from  w w  w.jav a2 s  . c  o m

    int w = bitmap.getWidth();
    int h = bitmap.getHeight();

    int[] pix = new int[w * h];
    //        Log.e("pix", w + " " + h + " " + pix.length);
    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++) {
            // Preserve alpha channel: ( 0xff000000 & pix[yi] )
            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;
        }
    }

    //        Log.e("pix", w + " " + h + " " + pix.length);
    bitmap.setPixels(pix, 0, w, 0, 0, w, h);
    return (bitmap);
}

From source file:Main.java

public static float squareDistance(float[] p1, float[] p2) throws ArrayIndexOutOfBoundsException {
    if (p1.length != p2.length) {
        throw new ArrayIndexOutOfBoundsException(Math.max(p1.length, p2.length));
    }/*from  www. ja  v  a 2  s.c  o m*/
    float sum = 0;
    for (int i = 0; i < p1.length; i++) {
        float d = p1[i] - p2[i];
        sum += d * d;
    }
    return sum;
}

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  w  w . jav a 2s  .com
        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 String getRelativeUri(String parent, String child) {
    if (!isAbsolute(child)) {
        return child;
    }/*w ww .j a v a 2  s .  com*/
    if (!isAbsolute(parent)) {
        throw new IllegalArgumentException("Parent uri must be absolute when child uri is absolute.");
    }
    parent = parent.substring(1);
    child = child.substring(1);
    if (child.startsWith(parent)) {
        return child.substring(parent.length());
    } else {
        String[] parentPaths = parent.split("/");
        String[] currentPaths = child.split("/");
        int max = Math.max(parentPaths.length, currentPaths.length);
        int sameCount = 0;
        for (int i = 0; i < max; i++) {
            if (parentPaths[i].equals(currentPaths[i])) {
                sameCount++;
            } else {
                break;
            }
        }
        int upLevel = parentPaths.length - sameCount;
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < upLevel - 1; i++) {
            buffer.append("../");
        }
        for (int i = sameCount; i < currentPaths.length; i++) {
            buffer.append(currentPaths[i]);
            if (i != currentPaths.length - 1)
                buffer.append('/');
        }
        return buffer.toString();
    }
}

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 av  a  2  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)
                && 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

public static String extractDomain(String url) {
    // url shorteners seems to have a "domain.de" shorter or equal to 11
    // the longest was tinyurl.com the shortest is t.co
    if (url.startsWith(HTTP))
        url = url.substring(HTTP.length());
    if (url.startsWith(HTTPS))
        url = url.substring(HTTPS.length());

    int index = url.indexOf("/");
    if (index < 0)
        index = Math.max(url.length(), url.indexOf(" "));

    String domain = url.substring(0, index);
    if (domain.startsWith("www."))
        domain = domain.substring(4);/*from w  w  w.  j a va2  s  .co m*/

    // skip if the domain of domain.de is of zero length or if the "de" is less then 2 chars
    index = domain.indexOf(".");
    if (index < 0 || domain.length() < 4)
        return "";

    return domain;
}

From source file:ImageClip.java

/**
 * Clips the input image to the specified shape
 * /*from  w w w  . jav a  2 s  .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:darks.learning.common.utils.MatrixHelper.java

public static DoubleMatrix max(double min, DoubleMatrix matrix) {
    for (int i = 0; i < matrix.length; i++)
        matrix.put(i, Math.max(min, matrix.get(i)));
    return matrix;
}

From source file:Main.java

private static Bitmap decodeBitmapWithSize(String pathName, int width, int height, boolean useBigger) {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;//from   w w  w  .  j a v  a2  s  .co m
    options.inInputShareable = true;
    options.inPurgeable = true;
    BitmapFactory.decodeFile(pathName, options);

    int decodeWidth = width, decodeHeight = height;
    final int degrees = getImageDegrees(pathName);
    if (degrees == 90 || degrees == 270) {
        decodeWidth = height;
        decodeHeight = width;
    }

    if (useBigger) {
        options.inSampleSize = (int) Math.min(((float) options.outWidth / decodeWidth),
                ((float) options.outHeight / decodeHeight));
    } else {
        options.inSampleSize = (int) Math.max(((float) options.outWidth / decodeWidth),
                ((float) options.outHeight / decodeHeight));
    }

    options.inJustDecodeBounds = false;
    Bitmap sourceBm = BitmapFactory.decodeFile(pathName, options);
    return imageWithFixedRotation(sourceBm, degrees);
}

From source file:Main.java

/**
 * Creates an animation to fade the dialog opacity from 1 to 0, and then
 * dispose./* w w  w .j a  va 2 s  . c  o m*/
 *
 * @param dialog the dialog to fade out
 * @param delay the delay in ms before starting and between each change
 * @param incrementSize the increment size
 */
public static void fadeOut(final JDialog dialog, int delay, final float incrementSize) {
    final Timer timer = new Timer(delay, null);
    timer.setRepeats(true);
    timer.addActionListener(new ActionListener() {
        private float opacity = 1;

        @Override
        public void actionPerformed(ActionEvent e) {
            opacity -= incrementSize;
            dialog.setOpacity(Math.max(opacity, 0)); // requires java 1.7
            if (opacity < 0) {
                timer.stop();
                dialog.dispose();
            }
        }
    });

    dialog.setOpacity(1); // requires java 1.7
    timer.start();
}