List of usage examples for java.lang Math round
public static long round(double a)
From source file:Util.java
/** * Returns closest point on segment to point * /* w w w .j a va 2s .c o m*/ * @param sx1 * segment x coord 1 * @param sy1 * segment y coord 1 * @param sx2 * segment x coord 2 * @param sy2 * segment y coord 2 * @param px * point x coord * @param py * point y coord * @return closets point on segment to point */ public static Point getClosestPointOnSegment(int sx1, int sy1, int sx2, int sy2, int px, int py) { double xDelta = sx2 - sx1; double yDelta = sy2 - sy1; if ((xDelta == 0) && (yDelta == 0)) { throw new IllegalArgumentException("Segment start equals segment end"); } double u = ((px - sx1) * xDelta + (py - sy1) * yDelta) / (xDelta * xDelta + yDelta * yDelta); final Point closestPoint; if (u < 0) { closestPoint = new Point(sx1, sy1); } else if (u > 1) { closestPoint = new Point(sx2, sy2); } else { closestPoint = new Point((int) Math.round(sx1 + u * xDelta), (int) Math.round(sy1 + u * yDelta)); } return closestPoint; }
From source file:Main.java
/** * Convert HSL (hue-saturation-lightness) components to a RGB color. * <ul>//www. ja v a 2 s.c o m * <li>hsl[0] is Hue [0 .. 360)</li> * <li>hsl[1] is Saturation [0...1]</li> * <li>hsl[2] is Lightness [0...1]</li> * </ul> * If hsv values are out of range, they are pinned. * * @param hsl 3 element array which holds the input HSL components. * @return the resulting RGB color */ public static int HSLToColor(float[] hsl) { final float h = hsl[0]; final float s = hsl[1]; final float l = hsl[2]; final float c = (1f - Math.abs(2 * l - 1f)) * s; final float m = l - 0.5f * c; final float x = c * (1f - Math.abs((h / 60f % 2f) - 1f)); final int hueSegment = (int) h / 60; int r = 0, g = 0, b = 0; switch (hueSegment) { case 0: r = Math.round(255 * (c + m)); g = Math.round(255 * (x + m)); b = Math.round(255 * m); break; case 1: r = Math.round(255 * (x + m)); g = Math.round(255 * (c + m)); b = Math.round(255 * m); break; case 2: r = Math.round(255 * m); g = Math.round(255 * (c + m)); b = Math.round(255 * (x + m)); break; case 3: r = Math.round(255 * m); g = Math.round(255 * (x + m)); b = Math.round(255 * (c + m)); break; case 4: r = Math.round(255 * (x + m)); g = Math.round(255 * m); b = Math.round(255 * (c + m)); break; case 5: case 6: r = Math.round(255 * (c + m)); g = Math.round(255 * m); b = Math.round(255 * (x + m)); break; } r = Math.max(0, Math.min(255, r)); g = Math.max(0, Math.min(255, g)); b = Math.max(0, Math.min(255, b)); return Color.rgb(r, g, b); }
From source file:Main.java
public static Point libgdxToScreenCoordinates(Context context, float x, float y) { WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); Display display = wm.getDefaultDisplay(); Point size = new Point(); display.getSize(size);/*from w ww . j a v a2 s .co m*/ // Log.d(CucumberInstrumentation.TAG, String.format("center: [%d/%d]", size.x / 2, size.y / 2)); Point point = new Point(); point.x = Math.round((size.x / 2f) + x); point.y = Math.round((size.y / 2f) + y); // Log.d(CucumberInstrumentation.TAG, String.format("coords: [%d/%d]", point.x, point.y)); return point; }
From source file:Main.java
/** * Convert HSL (hue-saturation-lightness) components to a RGB color. * <ul>/*from ww w.ja va2s. c om*/ * <li>hsl[0] is Hue [0 .. 360)</li> * <li>hsl[1] is Saturation [0...1]</li> * <li>hsl[2] is Lightness [0...1]</li> * </ul> * If hsv values are out of range, they are pinned. * * @param hsl 3-element array which holds the input HSL components * @return the resulting RGB color */ public static int HSLToColor(float[] hsl) { final float h = hsl[0]; final float s = hsl[1]; final float l = hsl[2]; final float c = (1f - Math.abs(2 * l - 1f)) * s; final float m = l - 0.5f * c; final float x = c * (1f - Math.abs((h / 60f % 2f) - 1f)); final int hueSegment = (int) h / 60; int r = 0, g = 0, b = 0; switch (hueSegment) { case 0: r = Math.round(255 * (c + m)); g = Math.round(255 * (x + m)); b = Math.round(255 * m); break; case 1: r = Math.round(255 * (x + m)); g = Math.round(255 * (c + m)); b = Math.round(255 * m); break; case 2: r = Math.round(255 * m); g = Math.round(255 * (c + m)); b = Math.round(255 * (x + m)); break; case 3: r = Math.round(255 * m); g = Math.round(255 * (x + m)); b = Math.round(255 * (c + m)); break; case 4: r = Math.round(255 * (x + m)); g = Math.round(255 * m); b = Math.round(255 * (c + m)); break; case 5: case 6: r = Math.round(255 * (c + m)); g = Math.round(255 * m); b = Math.round(255 * (x + m)); break; } r = constrain(r, 0, 255); g = constrain(g, 0, 255); b = constrain(b, 0, 255); return Color.rgb(r, g, b); }
From source file:Main.java
private static int getTextWidth(@Nullable final CharSequence text, final TextPaint paint) { if (TextUtils.isEmpty(text)) { return 0; }//from www. java2 s. c o m final int length = text.length(); final float[] widths = new float[length]; final int count; final Typeface savedTypeface = paint.getTypeface(); try { paint.setTypeface(getTextTypeface(text)); count = paint.getTextWidths(text, 0, length, widths); } finally { paint.setTypeface(savedTypeface); } int width = 0; for (int i = 0; i < count; i++) { width += Math.round(widths[i] + 0.5f); } return width; }
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; }//from www. j a v a 2 s. co m return inSampleSize; }
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/*from w w w. ja v a 2 s.com*/ final int heightRatio = Math.round((float) height / (float) reqHeight); final int widthRatio = Math.round((float) width / (float) reqWidth); inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; } return inSampleSize; }
From source file:Main.java
/** * Convert XYZ to RGB.//from w w w. j av a 2 s . c o m * * Convert equation and source code is from ImageJ's plugin Color Space Converter * http://rsbweb.nih.gov/ij/plugins/download/Color_Space_Converter.java * * @param X * @param Y * @param Z * @return RGB in int array. */ private static int[] XYZtoRGB(double X, double Y, double Z) { int[] result = new int[3]; double x = X / 100.0; double y = Y / 100.0; double z = Z / 100.0; // [r g b] = [X Y Z][Mi] double r = (x * Mi[0][0]) + (y * Mi[0][1]) + (z * Mi[0][2]); double g = (x * Mi[1][0]) + (y * Mi[1][1]) + (z * Mi[1][2]); double b = (x * Mi[2][0]) + (y * Mi[2][1]) + (z * Mi[2][2]); // assume sRGB if (r > 0.0031308) { r = ((1.055 * Math.pow(r, 1.0 / 2.4)) - 0.055); } else { r = (r * 12.92); } if (g > 0.0031308) { g = ((1.055 * Math.pow(g, 1.0 / 2.4)) - 0.055); } else { g = (g * 12.92); } if (b > 0.0031308) { b = ((1.055 * Math.pow(b, 1.0 / 2.4)) - 0.055); } else { b = (b * 12.92); } r = (r < 0) ? 0 : r; g = (g < 0) ? 0 : g; b = (b < 0) ? 0 : b; // convert 0..1 into 0..255 result[0] = (int) Math.round(r * 255); result[1] = (int) Math.round(g * 255); result[2] = (int) Math.round(b * 255); return result; }
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 {/*from w ww . j a va2 s. c o m*/ inSampleSize = Math.round((float) width / (float) reqWidth); } } return inSampleSize; }
From source file:de.tudarmstadt.ukp.wikipedia.util.ApiUtilities.java
/** * Prints a progress counter.//from ww w.j av a2 s.com * @param counter Indicates the position in the task. * @param size Size of the overall task. * @param step How many parts should the progress counter have? * @param mode Sets the output mode. * @param text The text that should be print along with the progress indicator. */ public static void printProgressInfo(int counter, int size, int step, ProgressInfoMode mode, String text) { if (size < step) { return; } if (counter % (size / step) == 0) { double progressPercent = counter * 100 / size; progressPercent = 1 + Math.round(progressPercent * 100) / 100.0; if (mode.equals(ApiUtilities.ProgressInfoMode.TEXT)) { logger.info(text + ": " + progressPercent + " - " + OS.getUsedMemory() + " MB"); } else if (mode.equals(ApiUtilities.ProgressInfoMode.DOTS)) { System.out.print("."); if (progressPercent >= 100) { System.out.println(); } } } }