List of usage examples for java.lang Math round
public static long round(double a)
From source file:Main.java
@SuppressWarnings("unused") public static int getMatrixRotateDegrees(Matrix matrix) { synchronized (MATRIX_VALUES) { matrix.getValues(MATRIX_VALUES); final float skewX = MATRIX_VALUES[Matrix.MSKEW_X]; final float scaleX = MATRIX_VALUES[Matrix.MSCALE_X]; //noinspection SuspiciousNameCombination final int degrees = (int) Math.round(Math.atan2(skewX, scaleX) * (180 / Math.PI)); if (degrees < 0) { return Math.abs(degrees); } else if (degrees > 0) { return 360 - degrees; } else {/*www . j a v a 2 s .c o m*/ return 0; } } }
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) { final int heightRatio = Math.round((float) height / (float) reqHeight); final int widthRatio = Math.round((float) width / (float) reqWidth); inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; }/*from w w w.j ava 2 s.c om*/ return inSampleSize; }
From source file:Main.java
public static double getDistance(double lat1, double lng1, double lat2, double lng2) { double radLat1 = rad(lat1); double radLat2 = rad(lat2); double a = radLat1 - radLat2; double b = rad(lng1) - rad(lng2); double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2))); s = s * EARTH_RADIUS;//from w w w .j av a 2 s . c om s = Math.round(s * 10000d) / 10000d; s = s * 1000; return s; }
From source file:Main.java
public static void constrainTo(Dimension dim, int width, int height) { boolean widthBigger = dim.width > dim.height; constrainTo(dim, widthBigger ? width : height); if ((widthBigger && dim.height > height) || (!widthBigger && dim.width > width)) { int size = (int) Math.round(widthBigger ? (height / (double) dim.height) * width : (width / (double) dim.width) * dim.height); constrainTo(dim, size);/*from ww w . ja va 2 s. c o m*/ } }
From source file:Main.java
private 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) { // Calculate ratios of height and width to requested height and width final int heightRatio = Math.round((float) height / (float) reqHeight); final int widthRatio = Math.round((float) width / (float) reqWidth); // Choose the smallest ratio as inSampleSize value, this will guarantee // a final image with both dimensions larger than or equal to the // requested height and width. inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; }//www. j av a2s . c om return inSampleSize; }
From source file:Main.java
/** * Formats a float value to the given number of decimals. Returns the length of the string. The string begins at * [endIndex] - [return value] and ends at [endIndex]. It's up to you to check indexes correctness. * /*w ww.j a va 2s.co m*/ * Parameter [endIndex] can be helpful when you want to append some text to formatted value. * * @return number of characters of formatted value */ public static int formatFloat(final char[] formattedValue, float value, int endIndex, int digits, char separator) { if (digits >= POW10.length) { formattedValue[endIndex - 1] = '.'; return 1; } boolean negative = false; if (value == 0) { formattedValue[endIndex - 1] = '0'; return 1; } if (value < 0) { negative = true; value = -value; } if (digits > POW10.length) { digits = POW10.length - 1; } value *= POW10[digits]; long lval = Math.round(value); int index = endIndex - 1; int charCount = 0; while (lval != 0 || charCount < (digits + 1)) { int digit = (int) (lval % 10); lval = lval / 10; formattedValue[index--] = (char) (digit + '0'); charCount++; if (charCount == digits) { formattedValue[index--] = separator; charCount++; } } if (negative) { formattedValue[index--] = '-'; charCount++; } return charCount; }
From source file:Main.java
private 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 . j a va 2s . c o m*/ inSampleSize = Math.round((float) width / (float) reqWidth); } return inSampleSize; }
From source file:Main.java
private static int calculateBatteryPercentage(int level, int scale) { return Math.round((level / (float) scale) * 100); }
From source file:Main.java
public static int scaleColor(int color, float factor, boolean scaleAlpha) { return Color.argb(scaleAlpha ? (Math.round(Color.alpha(color) * factor)) : Color.alpha(color), Math.round(Color.red(color) * factor), Math.round(Color.green(color) * factor), Math.round(Color.blue(color) * factor)); }
From source file:Main.java
public static void fillVerGradient(Graphics g, Color[] colors, int x, int y, int w, int h) { int steps = colors.length; double dx = (double) w / (double) steps; int x1 = x;//from w w w . j a v a 2s . c o m for (int i = 0; i < steps; i++) { int x2 = x + (int) Math.round((double) i * dx); g.setColor(colors[i]); if (i == (steps - 1)) { g.fillRect(x1, y, x + w - x1, h); } else { g.fillRect(x1, y, x2 - x1, h); } x1 = x2; } }