Example usage for java.lang Math round

List of usage examples for java.lang Math round

Introduction

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

Prototype

public static long round(double a) 

Source Link

Document

Returns the closest long to the argument, with ties rounding to positive infinity.

Usage

From source file:Main.java

private static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, float offset,
        boolean clipShadow, Paint paint) {

    int scaledWidth = width;
    int scaledHeight = height;

    final Canvas canvas = new Canvas();
    canvas.translate(offset / 2.0f, offset / 2.0f);

    Bitmap bitmap;/* ww w .j  a  v a 2  s  . c  om*/

    final Rect from = new Rect(x, y, x + width, y + height);
    final RectF to = new RectF(0, 0, width, height);

    if (m == null || m.isIdentity()) {
        bitmap = Bitmap.createBitmap(scaledWidth + (int) offset,
                scaledHeight + (int) (clipShadow ? (offset / 2.0f) : offset), Bitmap.Config.ARGB_8888);
        paint = null;
    } else {
        RectF mapped = new RectF();
        m.mapRect(mapped, to);

        scaledWidth = Math.round(mapped.width());
        scaledHeight = Math.round(mapped.height());

        bitmap = Bitmap.createBitmap(scaledWidth + (int) offset,
                scaledHeight + (int) (clipShadow ? (offset / 2.0f) : offset), Bitmap.Config.ARGB_8888);
        canvas.translate(-mapped.left, -mapped.top);
        canvas.concat(m);
    }

    canvas.setBitmap(bitmap);
    canvas.drawRect(0.0f, 0.0f, width, height, paint);
    canvas.drawBitmap(source, from, to, SCALE_PAINT);

    return bitmap;
}

From source file:knop.psfj.utils.MathUtils.java

/**
 * Round./*  w  ww . j av a 2  s  . c o m*/
 *
 * @param d the d
 * @return the int
 */
public static int round(double d) {
    return (int) Math.round(d);

    //return Math.round(new Float(d));
}

From source file:Main.java

static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth) {
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (width > reqWidth) {
        inSampleSize = Math.round((float) width / (float) reqWidth);
    }//from w  w  w  .  j a  va  2s.  co m
    return inSampleSize;
}

From source file:Main.java

/**
 * Stack Blur v1.0 from/*from w  w w.  j  a  v a2s .  c om*/
 * http://www.quasimondo.com/StackBlurForCanvas/StackBlurDemo.html
 * Java Author: Mario Klingemann <mario at quasimondo.com>
 * http://incubator.quasimondo.com
 * <p/>
 * created Feburary 29, 2004
 * Android port : Yahel Bouaziz <yahel at kayenko.com>
 * http://www.kayenko.com
 * ported april 5th, 2012
 * <p/>
 * This is a compromise between Gaussian Blur and Box blur
 * It creates much better looking blurs than Box Blur, but is
 * 7x faster than my Gaussian Blur implementation.
 * <p/>
 * I called it Stack Blur because this describes best how this
 * filter works internally: it creates a kind of moving stack
 * of colors whilst scanning through the image. Thereby it
 * just has to add one new block of color to the right side
 * of the stack and remove the leftmost color. The remaining
 * colors on the topmost layer of the stack are either added on
 * or reduced by one, depending on if they are on the right or
 * on the left side of the stack.
 * <p/>
 * If you are using this algorithm in your code please add
 * the following line:
 * Stack Blur Algorithm by Mario Klingemann <mario@quasimondo.com>
 */

public static Bitmap fastblur(Bitmap sentBitmap, float scale, int radius) {

    int width = Math.round(sentBitmap.getWidth() * scale);
    int height = Math.round(sentBitmap.getHeight() * scale);
    sentBitmap = Bitmap.createScaledBitmap(sentBitmap, width, height, false);

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

    if (radius < 1) {
        return (null);
    }

    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 dv[] = new int[256 * divsum];
    for (i = 0; i < 256 * divsum; 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:com.idr.servlets.UploadListener.java

@Override
public void update(long aBytesRead, long aContentLength, int anItem) {
    setBytesRead(aBytesRead);/*from  w  w w .  ja va  2 s .c  o m*/
    setContentLength(aContentLength);
    setItem(anItem);
    if (getContentLength() > -1)
        this.setIsLengthKnown(true);

    if (isLengthKnown())
        this.setPercent((int) Math.round(100.00 * getBytesRead() / getContentLength()));

}

From source file:Main.java

/**
 * Returns n!. Shorthand for <code>n</code> <a
 * href="http://mathworld.wolfram.com/Factorial.html"> Factorial</a>, the
 * product of the numbers <code>1,...,n</code>.
 * <p>/*from  w ww  .j a v a  2 s  .  c o m*/
 * <Strong>Preconditions</strong>:
 * <ul>
 * <li> <code>n >= 0</code> (otherwise
 * <code>IllegalArgumentException</code> is thrown)</li>
 * <li> The result is small enough to fit into a <code>long</code>. The
 * largest value of <code>n</code> for which <code>n!</code> <
 * Long.MAX_VALUE</code> is 20. If the computed value exceeds <code>Long.MAX_VALUE</code>
 * an <code>ArithMeticException </code> is thrown.</li>
 * </ul>
 * </p>
 * 
 * @param n argument
 * @return <code>n!</code>
 * @throws ArithmeticException if the result is too large to be represented
 *         by a long integer.
 * @throws IllegalArgumentException if n < 0
 */
public static long factorial(final int n) {
    long result = Math.round(factorialDouble(n));
    if (result == Long.MAX_VALUE) {
        throw new ArithmeticException("result too large to represent in a long integer");
    }
    return result;
}

From source file:com.shvet.poi.ss.format.SimpleFraction.java

/**
 * Create a fraction given a double value and a denominator.
 *
 * @param val        double value of fraction
 * @param exactDenom the exact denominator
 * @return a SimpleFraction with the given values set.
 *///  w ww. j a  v  a  2s.com
public static SimpleFraction buildFractionExactDenominator(double val, int exactDenom) {
    int num = (int) Math.round(val * (double) exactDenom);
    return new SimpleFraction(num, exactDenom);
}

From source file:Main.java

public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;
    if (height > reqHeight || width > reqWidth) {
        if (width > height) {
            inSampleSize = Math.round((float) height / (float) reqHeight);
        } else {/*from   w  w  w  .  java 2  s. c  o m*/
            inSampleSize = Math.round((float) width / (float) reqWidth);
        }
    }
    return inSampleSize;
}

From source file:Main.java

/**
 * Inverse of {@link #getDescendantCoordRelativeToSelf(View, int[])}.
 *///from  w  w w  . j  ava 2 s . c o  m
public static float mapCoordInSelfToDescendent(View descendant, View root, int[] coord) {
    ArrayList<View> ancestorChain = new ArrayList<View>();

    float[] pt = { coord[0], coord[1] };

    View v = descendant;
    while (v != root) {
        ancestorChain.add(v);
        v = (View) v.getParent();
    }
    ancestorChain.add(root);

    float scale = 1.0f;
    Matrix inverse = new Matrix();
    int count = ancestorChain.size();
    for (int i = count - 1; i >= 0; i--) {
        View ancestor = ancestorChain.get(i);
        View next = i > 0 ? ancestorChain.get(i - 1) : null;

        pt[0] += ancestor.getScrollX();
        pt[1] += ancestor.getScrollY();

        if (next != null) {
            pt[0] -= next.getLeft();
            pt[1] -= next.getTop();
            next.getMatrix().invert(inverse);
            inverse.mapPoints(pt);
            scale *= next.getScaleX();
        }
    }

    coord[0] = (int) Math.round(pt[0]);
    coord[1] = (int) Math.round(pt[1]);
    return scale;
}

From source file:Main.java

/**
 * Formats the given number to the given number of decimals, and returns the
 * number as a string, maximum 35 characters.
 *
 * @param number//w ww.  ja v  a2  s  . co m
 * @param digitCount
 * @param separateThousands set this to true to separate thousands values
 * @param separateChar      a caracter to be paced between the "thousands"
 * @return
 */
public static String formatNumber(float number, int digitCount, boolean separateThousands, char separateChar) {

    char[] out = new char[35];

    boolean neg = false;
    if (number == 0) {
        return "0";
    }

    boolean zero = false;
    if (number < 1 && number > -1) {
        zero = true;
    }

    if (number < 0) {
        neg = true;
        number = -number;
    }

    if (digitCount > POW_10.length) {
        digitCount = POW_10.length - 1;
    }

    number *= POW_10[digitCount];
    long lval = Math.round(number);
    int ind = out.length - 1;
    int charCount = 0;
    boolean decimalPointAdded = false;

    while (lval != 0 || charCount < (digitCount + 1)) {
        int digit = (int) (lval % 10);
        lval = lval / 10;
        out[ind--] = (char) (digit + '0');
        charCount++;

        // add decimal point
        if (charCount == digitCount) {
            out[ind--] = ',';
            charCount++;
            decimalPointAdded = true;

            // add thousand separators
        } else if (separateThousands && lval != 0 && charCount > digitCount) {

            if (decimalPointAdded) {

                if ((charCount - digitCount) % 4 == 0) {
                    out[ind--] = separateChar;
                    charCount++;
                }

            } else {

                if ((charCount - digitCount) % 4 == 3) {
                    out[ind--] = separateChar;
                    charCount++;
                }
            }
        }
    }

    // if number around zero (between 1 and -1)
    if (zero) {
        out[ind--] = '0';
        charCount += 1;
    }

    // if the number is negative
    if (neg) {
        out[ind--] = '-';
        charCount += 1;
    }

    int start = out.length - charCount;

    // use this instead of "new String(...)" because of issue < Android 4.0
    return String.valueOf(out, start, out.length - start);
}