List of usage examples for java.lang Math ceil
public static double ceil(double a)
From source file:Main.java
public static Bitmap drawableToBitmap(Drawable drawable, float ratio) { int width = (int) Math.ceil(drawable.getIntrinsicWidth() * ratio); int height = (int) Math.ceil(drawable.getIntrinsicHeight() * ratio); Bitmap bitmap = Bitmap.createBitmap(width, height, drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565); Canvas canvas = new Canvas(bitmap); canvas.scale(ratio, ratio);/* w w w.j av a 2 s.c o m*/ drawable.setBounds(0, 0, width, height); drawable.draw(canvas); return bitmap; }
From source file:Main.java
/** * Creates a new {@link HashSet} with the given expected size. It uses the * default load factor (0.75) to estimate the proper number of elements for * the data structure to avoid a rehash or resize when the given number of * elements are added.//from ww w.j av a 2s. c om * * @param <ValueType> * The type of the value in the set. * @param size * The size. Must be positive. * @return * A new hash map with the given expected size. */ public static <ValueType> HashSet<ValueType> createHashSetWithSize(final int size) { final int initialCapacity = (int) Math.ceil(size / DEFAULT_LOAD_FACTOR); return new HashSet<ValueType>(initialCapacity, DEFAULT_LOAD_FACTOR); }
From source file:Main.java
/** * Creates a new {@link LinkedHashSet} with the given expected size. It uses the * default load factor (0.75) to estimate the proper number of elements for * the data structure to avoid a rehash or resize when the given number of * elements are added./*from w w w .jav a2 s.co m*/ * * @param <ValueType> * The type of the value in the set. * @param size * The size. Must be positive. * @return * A new hash map with the given expected size. */ public static <ValueType> LinkedHashSet<ValueType> createLinkedHashSetWithSize(final int size) { final int initialCapacity = (int) Math.ceil(size / DEFAULT_LOAD_FACTOR); return new LinkedHashSet<ValueType>(initialCapacity, DEFAULT_LOAD_FACTOR); }
From source file:Main.java
/** * Splits the given array into blocks of given size and adds padding to the * last one, if necessary./*w w w . j a va 2 s . c o m*/ * * @param byteArray * the array. * @param blocksize * the block size. * @return a list of blocks of given size. */ public static List<byte[]> splitAndPad(byte[] byteArray, int blocksize) { List<byte[]> blocks = new ArrayList<byte[]>(); int numBlocks = (int) Math.ceil(byteArray.length / (double) blocksize); for (int i = 0; i < numBlocks; i++) { byte[] block = new byte[blocksize]; Arrays.fill(block, (byte) 0x00); if (i + 1 == numBlocks) { // the last block int remainingBytes = byteArray.length - (i * blocksize); System.arraycopy(byteArray, i * blocksize, block, 0, remainingBytes); } else { System.arraycopy(byteArray, i * blocksize, block, 0, blocksize); } blocks.add(block); } return blocks; }
From source file:Main.java
/** * Creates a new {@link HashMap} with the given expected size. It uses the * default load factor (0.75) to estimate the proper number of elements for * the data structure to avoid a rehash or resize when the given number of * elements are added./*from w w w . j ava 2s . c om*/ * * @param <KeyType> * The type for the key of the map. * @param <ValueType> * The type of the value in the map. * @param size * The size. Must be positive. * @return * A new hash map with the given expected size. */ public static <KeyType, ValueType> HashMap<KeyType, ValueType> createHashMapWithSize(final int size) { final int initialCapacity = (int) Math.ceil(size / DEFAULT_LOAD_FACTOR); return new HashMap<KeyType, ValueType>(initialCapacity, DEFAULT_LOAD_FACTOR); }
From source file:Main.java
/** This method returns the smallest double value that is smaller than * <code> d = x * 10<sup>exp</exp></code> where x is rounded up to * the closest integer./*from w w w. j a v a 2 s .c o m*/ * @param d the double value to be rounded * @param exp the exponent of 10 to which d should be rounded * @return <code> Math.ceil(x) * 10<sup>exp</sup></code> */ public static double ceil(double d, int exp) { double x = 1.0 * Math.pow(10.0, (double) exp); return Math.ceil(d / x) * x; }
From source file:Main.java
/** * Applies a gaussian blur of the given radius to the given {@link BufferedImage} using a kernel * convolution.//from w ww. jav a 2s . c o m * * @param source The source image. * @param radius The blur radius, in pixels. * @return A new, blurred image, or the source image if no blur is performed. */ public static BufferedImage blurredImage(BufferedImage source, double radius) { if (radius == 0) { return source; } final int r = (int) Math.ceil(radius); final int rows = r * 2 + 1; final float[] kernelData = new float[rows * rows]; final double sigma = radius / 3; final double sigma22 = 2 * sigma * sigma; final double sqrtPiSigma22 = Math.sqrt(Math.PI * sigma22); final double radius2 = radius * radius; double total = 0; int index = 0; double distance2; int x, y; for (y = -r; y <= r; y++) { for (x = -r; x <= r; x++) { distance2 = 1.0 * x * x + 1.0 * y * y; if (distance2 > radius2) { kernelData[index] = 0; } else { kernelData[index] = (float) (Math.exp(-distance2 / sigma22) / sqrtPiSigma22); } total += kernelData[index]; ++index; } } for (index = 0; index < kernelData.length; index++) { kernelData[index] /= total; } // We first pad the image so the kernel can operate at the edges. BufferedImage paddedSource = paddedImage(source, r); BufferedImage blurredPaddedImage = operatedImage(paddedSource, new ConvolveOp(new Kernel(rows, rows, kernelData), ConvolveOp.EDGE_ZERO_FILL, null)); return blurredPaddedImage.getSubimage(r, r, source.getWidth(), source.getHeight()); }
From source file:Main.java
/** * Partition a list into <code>num</code> sub-lists. If the list does * not divide evenly, the extra 'n' elements are split across the * first 'n' lists. There will be no more lists than elements returned (i.e. no empty lists tacked on to the end) *//*from ww w. j av a2 s. c o m*/ public static <T> List<List<T>> partition(List<T> list, int num) { if (num < 1) { throw new IllegalArgumentException("Number of sub-lists must be greater than zero"); } List<List<T>> result = new ArrayList<List<T>>(); int index = 0; int listsRemaining = num; int elementsRemaining = list.size(); while (elementsRemaining > 0) { int size = (int) Math.ceil(elementsRemaining / (listsRemaining + 0.0)); List<T> subList = list.subList(index, index + size); result.add(subList); listsRemaining--; elementsRemaining -= size; index += size; } if (elementsRemaining != 0) { throw new IllegalStateException( String.format("Loop exited with %d elements still remaining", elementsRemaining)); } return result; }
From source file:Base64.java
/** * Base-64 encodes the supplied block of data. Line wrapping is not * applied on output.//from w w w. j a v a2 s . c o m * * @param bytes The block of data that is to be Base-64 encoded. * @return A <code>String</code> containing the encoded data. */ public static String encode(byte[] bytes) { int length = bytes.length; if (length == 0) return ""; StringBuffer buffer = new StringBuffer((int) Math.ceil((double) length / 3d) * 4); int remainder = length % 3; length -= remainder; int block; int i = 0; while (i < length) { block = ((bytes[i++] & 0xff) << 16) | ((bytes[i++] & 0xff) << 8) | (bytes[i++] & 0xff); buffer.append(ALPHABET.charAt(block >>> 18)); buffer.append(ALPHABET.charAt((block >>> 12) & 0x3f)); buffer.append(ALPHABET.charAt((block >>> 6) & 0x3f)); buffer.append(ALPHABET.charAt(block & 0x3f)); } if (remainder == 0) return buffer.toString(); if (remainder == 1) { block = (bytes[i] & 0xff) << 4; buffer.append(ALPHABET.charAt(block >>> 6)); buffer.append(ALPHABET.charAt(block & 0x3f)); buffer.append("=="); return buffer.toString(); } block = (((bytes[i++] & 0xff) << 8) | ((bytes[i]) & 0xff)) << 2; buffer.append(ALPHABET.charAt(block >>> 12)); buffer.append(ALPHABET.charAt((block >>> 6) & 0x3f)); buffer.append(ALPHABET.charAt(block & 0x3f)); buffer.append("="); return buffer.toString(); }
From source file:Main.java
private static int computeInitialSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) { double w = options.outWidth; double h = options.outHeight; int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels)); int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(Math.floor(w / minSideLength), Math.floor(h / minSideLength)); if (upperBound < lowerBound) { // return the larger one when there is no overlapping zone. return lowerBound; }//from w ww.j av a2 s . c om if ((maxNumOfPixels == -1) && (minSideLength == -1)) { return 1; } else if (minSideLength == -1) { return lowerBound; } else { return upperBound; } }