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

/**
 * Scales the given rectangle by the given scale factor.
 *
 * @param rect        The rectangle to scale.
 * @param scaleFactor The factor to scale by.
 * @return The scaled rectangle.//from   ww  w .j  a v a  2s. c  o m
 */
public static Rectangle scaleRectangle(Rectangle rect, float scaleFactor) {
    return new Rectangle(Math.round(rect.x * scaleFactor), Math.round(rect.y * scaleFactor),
            Math.round(rect.width * scaleFactor), Math.round(rect.height * scaleFactor));
}

From source file:ImageClip.java

/**
 * Clips the input image to the specified shape
 * //www .  j a  v  a2 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:de.mpc.pia.tools.PIATools.java

/**
 * Round a double value and keeping (at max) the given number of decimal
 * places.// w w w.  j a v  a  2  s. c o  m
 *
 * @param value
 * @param dec
 * @return
 */
public static double round(double value, int dec) {
    double factor = Math.pow(10, dec);
    return Math.round(value * factor) / factor;
}

From source file:Main.java

/**
 * Checks if a bitmap is null and returns a placeholder in its place
 */// w w  w.j a  v  a2 s .c  om
private static int dpToPx(Context mContext, int dp) {
    float density = mContext.getResources().getDisplayMetrics().density;
    return Math.round((float) dp * density);
}

From source file:Main.java

public static int foldEnd(int length, int numberOfFolds, int fold) {
    return Math.round((length / ((float) numberOfFolds)) * (fold));
}

From source file:Main.java

/**
 * Returns the given amount of meters in rounded yards if the default locale
 * is US, in rounded meters otherwise./*from www .ja va  2  s .  co  m*/
 * @param meters
 * @return int
 */
public static int getLocalizedRoundedMeters(final float meters) {
    Locale defaultLocale = Locale.getDefault();
    if (defaultLocale.equals(Locale.US)) {
        return Math.round(meters / 0.9144f);
    } else {
        return Math.round(meters);
    }
}

From source file:Main.java

private static int calcInSampleSize(BitmapFactory.Options opts, int reqWidth, int reqHeight) {
    int height = opts.outHeight;
    int width = opts.outWidth;
    int inSampleSize = 1;
    if (height > reqHeight || width > reqWidth) {
        int heightRatio = 1;
        if (reqHeight > 0) {
            heightRatio = Math.round((float) height / (float) reqHeight);
        }/*from w ww.j a  v  a  2 s  .  c  o m*/
        int widthRatio = 1;
        if (reqWidth > 0) {
            widthRatio = Math.round((float) width / (float) reqWidth);
        }
        inSampleSize = (heightRatio < widthRatio) ? heightRatio : widthRatio;
    }
    return inSampleSize;
}

From source file:Main.java

public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {

    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 {/*w w w  .j  ava  2 s. c o m*/
            inSampleSize = Math.round((float) width / (float) reqWidth);
        }
    }
    return inSampleSize;
}

From source file:Main.java

public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {

    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);

        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        inSampleSize = inSampleSize < 2 ? 2 : inSampleSize;
    }/*from w  ww  .  j a  va 2 s.c o  m*/

    return inSampleSize;
}

From source file:Main.java

/**
 * decode the image bitmap according to specified size
 * /* ww  w  .j av a 2 s.  co m*/
 * @param f
 * @param maxSize
 * @return
 */
public static Bitmap decodeFile(File f, int maxSize) {
    Bitmap b = null;
    try {
        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;

        FileInputStream fis = new FileInputStream(f);
        BitmapFactory.decodeStream(fis, null, o);
        fis.close();

        int scale = 1;
        if (o.outHeight > maxSize || o.outWidth > maxSize) {
            scale = (int) Math.pow(2, (int) Math
                    .round(Math.log(maxSize / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
        }

        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        fis = new FileInputStream(f);
        b = BitmapFactory.decodeStream(fis, null, o2);
        fis.close();
    } catch (IOException e) {
    }
    return b;
}