List of usage examples for java.lang Math floor
public static double floor(double a)
From source file:Main.java
public static byte[] resize(byte[] picArray, int size, boolean faceDetect) { if (picArray == null) { return null; }/* w ww. ja v a2 s .co m*/ Bitmap pic = BitmapFactory.decodeByteArray(picArray, 0, picArray.length); if (pic == null) { return null; } size = Math.min(size, Math.min(pic.getHeight(), pic.getWidth())); if (size % 2 != 0) size--; Log.i("sizes", "old width:" + pic.getWidth() + " old height:" + pic.getHeight() + " new size:" + size); Bitmap scaledPic = scale(pic, size); int width = scaledPic.getWidth(); int height = scaledPic.getHeight(); //if pic is already square, we are done now if (width == height) { return bitmapToBytes(scaledPic); } PointF mid = null; int cropcenter; if (faceDetect) mid = findFaceMid(scaledPic); Bitmap out; if (width > height) { if (mid != null) cropcenter = Math.max(size / 2, Math.min((int) Math.floor(mid.y), width - size / 2)); else cropcenter = width / 2; Log.i("CROPPING", "width:" + width + " center:" + cropcenter + " size:" + size + " left edge:" + (cropcenter - size / 2) + " right edge:" + (cropcenter + size / 2)); out = Bitmap.createBitmap(scaledPic, cropcenter - size / 2, 0, size, size); } else { if (mid != null) cropcenter = Math.max(size / 2, Math.min((int) Math.floor(mid.x), height - size / 2)); else cropcenter = height / 2; out = Bitmap.createBitmap(scaledPic, 0, 0, size, size); } return bitmapToBytes(out); }
From source file:main.java.utils.Utility.java
public static int getBase(int x) { int value = Math.abs(x); if (value == 0) return 1; else/*from w ww. j av a2s. c o m*/ return (int) (1 + Math.floor((Math.log(value) / Math.log(10.0d)))); }
From source file:Main.java
/** * Returns the closest power-of-two number less than or equal to x. * * @param x input value// w ww .j av a2 s . c o m * * @return the closest power-of-two number less then or equal to x */ public static long prevPow2(long x) { if (x < 1) { throw new IllegalArgumentException("x must be greater or equal 1"); } return (long) Math.pow(2, Math.floor(Math.log(x) / Math.log(2))); }
From source file:com.android.camera2.its.ItsUtils.java
public static MeteringRectangle[] getJsonWeightedRectsFromArray(JSONArray a, boolean normalized, int width, int height) throws ItsException { try {//from w w w. j a va2s. c om // Returns [x0,y0,x1,y1,wgt, x0,y0,x1,y1,wgt, x0,y0,x1,y1,wgt, ...] assert (a.length() % 5 == 0); MeteringRectangle[] ma = new MeteringRectangle[a.length() / 5]; for (int i = 0; i < a.length(); i += 5) { int x, y, w, h; if (normalized) { x = (int) Math.floor(a.getDouble(i + 0) * width + 0.5f); y = (int) Math.floor(a.getDouble(i + 1) * height + 0.5f); w = (int) Math.floor(a.getDouble(i + 2) * width + 0.5f); h = (int) Math.floor(a.getDouble(i + 3) * height + 0.5f); } else { x = a.getInt(i + 0); y = a.getInt(i + 1); w = a.getInt(i + 2); h = a.getInt(i + 3); } x = Math.max(x, 0); y = Math.max(y, 0); w = Math.min(w, width - x); h = Math.min(h, height - y); int wgt = a.getInt(i + 4); ma[i / 5] = new MeteringRectangle(x, y, w, h, wgt); } return ma; } catch (org.json.JSONException e) { throw new ItsException("JSON error: ", e); } }
From source file:com.jennifer.ui.util.MathUtil.java
private static double niceNum(double range, boolean round) { double exponent = Math.floor(Math.log10(range)); double fraction = range / Math.pow(10, exponent); double nickFraction; if (round) {/*from ww w.ja va 2 s. c o m*/ if (fraction < 1.5) nickFraction = 1; else if (fraction < 3) nickFraction = 2; else if (fraction < 7) nickFraction = 5; else nickFraction = 10; } else { if (fraction <= 1) nickFraction = 1; else if (fraction <= 2) nickFraction = 2; else if (fraction <= 5) nickFraction = 5; else nickFraction = 10; } return nickFraction * Math.pow(10, exponent); }
From source file:Main.java
public Dimension getPreferredScrollableViewportSize() { if (getParent() == null) return getSize(); Dimension d = getParent().getSize(); int c = (int) Math.floor((d.width - getInsets().left - getInsets().right) / 50.0); if (c == 0)//ww w . j a va 2 s.c om return d; int r = 20 / c; if (r * c < 20) ++r; return new Dimension(c * 50, r * 50); }
From source file:EncodeUtils.java
public static byte[] toBase64(byte[] in) { int inputLength = in.length; int outputLength = (int) Math.floor((4 * inputLength) / 3f) + 3; outputLength = outputLength + 2 * (int) Math.floor(outputLength / 76f); byte[] results = new byte[outputLength]; int inputIndex = 0; int outputIndex = 0; while (inputLength - inputIndex > 2) { int one = (toInt(in[inputIndex++]) << 16); int two = (toInt(in[inputIndex++]) << 8); int three = toInt(in[inputIndex++]); int quantum = one | two | three; int index = (quantum & FIRST_MASK) >> 18; outputIndex = setResult(results, outputIndex, ENCODING[index]); index = (quantum & SECOND_MASK) >> 12; outputIndex = setResult(results, outputIndex, ENCODING[index]); index = (quantum & THIRD_MASK) >> 6; outputIndex = setResult(results, outputIndex, ENCODING[index]); index = (quantum & FORTH_MASK); outputIndex = setResult(results, outputIndex, ENCODING[index]); }//from w w w . j a v a 2 s . c om switch (inputLength - inputIndex) { case 1: int quantum = in[inputIndex++] << 16; int index = (quantum & FIRST_MASK) >> 18; outputIndex = setResult(results, outputIndex, ENCODING[index]); index = (quantum & SECOND_MASK) >> 12; outputIndex = setResult(results, outputIndex, ENCODING[index]); outputIndex = setResult(results, outputIndex, (byte) '='); outputIndex = setResult(results, outputIndex, (byte) '='); break; case 2: quantum = (in[inputIndex++] << 16) + (in[inputIndex++] << 8); index = (quantum & FIRST_MASK) >> 18; outputIndex = setResult(results, outputIndex, ENCODING[index]); index = (quantum & SECOND_MASK) >> 12; outputIndex = setResult(results, outputIndex, ENCODING[index]); index = (quantum & THIRD_MASK) >> 6; outputIndex = setResult(results, outputIndex, ENCODING[index]); outputIndex = setResult(results, outputIndex, (byte) '='); break; } return results; }
From source file:Round.java
static int round(double d) { if (d < 0) { throw new IllegalArgumentException("Value must be non-negative"); }//from w w w .j ava 2 s .c om int di = (int) Math.floor(d); // integral value below (or ==) d if ((d - di) > THRESHOLD) { return di + 1; } else { return di; } }
From source file:com.algoTrader.util.RoundUtil.java
public static int getDigits(double n) { int exponent = -(int) Math.floor(Math.log10(n)); int digits = exponent >= 0 ? exponent : 0; return digits; }
From source file:uk.ac.kcl.it.MagicSquare.java
@PostConstruct public void readFile() throws IOException { // If included in an Eclipse project. InputStream stream = ClassLoader.getSystemResourceAsStream("magic-square.csv"); BufferedReader buffer = new BufferedReader(new InputStreamReader(stream)); // If in the same directory - Probably in your case... // Just comment out the 2 lines above this and uncomment the line // that follows. //BufferedReader buffer = new BufferedReader(new FileReader(filename)); String line;/*from ww w.j a v a2s .com*/ int row = 0; while ((line = buffer.readLine()) != null) { String[] vals = line.trim().split("\\s+"); // Lazy instantiation. if (matrix == null) { size = vals.length; matrix = new int[size][size]; log10 = (int) Math.floor(Math.log10(size * size)) + 1; numberFormat = String.format("%%%dd", log10); } for (int col = 0; col < size; col++) { matrix[row][col] = Integer.parseInt(vals[col]); } row++; } }