Example usage for java.lang Math ceil

List of usage examples for java.lang Math ceil

Introduction

In this page you can find the example usage for java.lang Math ceil.

Prototype

public static double ceil(double a) 

Source Link

Document

Returns the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer.

Usage

From source file:Main.java

private static int computeInitialSampleSize(BitmapFactory.Options options, int minSideLength,
        int maxNumOfPixels) {
    double w = options.outWidth;
    double h = options.outHeight;

    final int UNCONSTRAINED = -1;
    int lowerBound = (maxNumOfPixels == UNCONSTRAINED) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
    int upperBound = (minSideLength == UNCONSTRAINED) ? 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;
    }//  w ww . j  a  va2 s.  com

    if ((maxNumOfPixels == UNCONSTRAINED) && (minSideLength == UNCONSTRAINED)) {
        return 1;
    } else if (minSideLength == UNCONSTRAINED) {
        return lowerBound;
    } else {
        return upperBound;
    }
}

From source file:Main.java

/**
 * Creates and returns image from the given text.
 * @param text input text/*ww  w .  jav a2 s.  co  m*/
 * @param font text font
 * @return image with input text
 */
public static BufferedImage createImageFromText(String text, Font font) {
    //You may want to change these setting, or make them parameters
    boolean isAntiAliased = true;
    boolean usesFractionalMetrics = false;
    FontRenderContext frc = new FontRenderContext(null, isAntiAliased, usesFractionalMetrics);
    TextLayout layout = new TextLayout(text, font, frc);
    Rectangle2D bounds = layout.getBounds();
    int w = (int) Math.ceil(bounds.getWidth());
    int h = (int) Math.ceil(bounds.getHeight()) + 2;
    BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); //for example;
    Graphics2D g = image.createGraphics();
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, w, h);
    g.setColor(Color.BLACK);
    g.setFont(font);
    Object antiAliased = isAntiAliased ? RenderingHints.VALUE_TEXT_ANTIALIAS_ON
            : RenderingHints.VALUE_TEXT_ANTIALIAS_OFF;
    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, antiAliased);
    Object fractionalMetrics = usesFractionalMetrics ? RenderingHints.VALUE_FRACTIONALMETRICS_ON
            : RenderingHints.VALUE_FRACTIONALMETRICS_OFF;
    g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, fractionalMetrics);
    g.drawString(text, (float) -bounds.getX(), (float) -bounds.getY());
    g.dispose();

    return image;
}

From source file:Main.java

public static void invalidateGlobalRegion(View view, RectF childBounds) {
    //childBounds.offset(view.getTranslationX(), view.getTranslationY());
    if (DEBUG_INVALIDATE)
        Log.v(TAG, "-------------");
    while (view.getParent() != null && view.getParent() instanceof View) {
        view = (View) view.getParent();
        view.getMatrix().mapRect(childBounds);
        view.invalidate((int) Math.floor(childBounds.left), (int) Math.floor(childBounds.top),
                (int) Math.ceil(childBounds.right), (int) Math.ceil(childBounds.bottom));
        if (DEBUG_INVALIDATE) {
            Log.v(TAG,/*from  w  w w .j  av  a 2 s.c o m*/
                    "INVALIDATE(" + (int) Math.floor(childBounds.left) + "," + (int) Math.floor(childBounds.top)
                            + "," + (int) Math.ceil(childBounds.right) + ","
                            + (int) Math.ceil(childBounds.bottom));
        }
    }
}

From source file:edu.asu.ca.kaushik.algorithms.permvector.utils.SCPHF.java

public static int SCPHF_LLL(int t, int k, int v) {
    double covP = 1.0d;

    for (int i = 1; i < t; i++) {
        covP = covP * (1.0d - 1 / Math.pow(v, i));
    }/*from ww w. j ava  2s  . c o m*/

    double q = 1.0d - covP;

    double d = CombinatoricsUtils.binomialCoefficientDouble(k, t)
            - CombinatoricsUtils.binomialCoefficientDouble(k - t, t);

    return (int) Math.ceil((1 + Math.log(d)) / (-Math.log(q)));
}

From source file:Main.java

/**
 * Computes a reasonable number of labels for a data range.
 * /*  w  w w . j a va2  s. c om*/
 * @param start start value
 * @param end final value
 * @param approxNumLabels desired number of labels
 * @return double[] array containing {start value, end value, increment}
 */
private static double[] computeLabels(final double start, final double end, final int approxNumLabels,
        double minimumJump) {
    if (Math.abs(start - end) < 0.0000001f) {
        return new double[] { start, start, 0 };
    }
    double s = start;
    double e = end;
    boolean switched = false;
    if (s > e) {
        switched = true;
        double tmp = s;
        s = e;
        e = tmp;
    }
    double xStep = roundUp(Math.max(Math.abs(s - e) / approxNumLabels, minimumJump));
    // Compute x starting point so it is a multiple of xStep.
    double xStart = xStep * Math.ceil(s / xStep);
    double xEnd = xStep * Math.floor(e / xStep);
    if (switched) {
        return new double[] { xEnd, xStart, -1.0 * xStep };
    }
    return new double[] { xStart, xEnd, xStep };
}

From source file:edu.emory.mathcs.nlp.common.util.MathUtils.java

static public int ceil(double d) {
    return (int) Math.ceil(d);
}

From source file:com.galenframework.specs.RangeValue.java

private static int convertValue(double value, int precision) {
    if (value > 0) {
        return (int) Math.floor(value * Math.pow(10, precision));
    } else {//ww  w .j a v a2  s . com
        return (int) Math.ceil(value * Math.pow(10, precision));
    }
}

From source file:Main.java

public static Bitmap getColorPreviewBitmap(final Context context, final int color, final boolean border) {
    if (context == null)
        return null;
    final float density = context.getResources().getDisplayMetrics().density;
    final int width = (int) (32 * density), height = (int) (32 * density);

    final Bitmap bm = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    final Canvas canvas = new Canvas(bm);

    final int rectangleSize = (int) (density * 5);
    final int numRectanglesHorizontal = (int) Math.ceil(width / rectangleSize);
    final int numRectanglesVertical = (int) Math.ceil(height / rectangleSize);
    final Rect r = new Rect();
    boolean verticalStartWhite = true;
    for (int i = 0; i <= numRectanglesVertical; i++) {

        boolean isWhite = verticalStartWhite;
        for (int j = 0; j <= numRectanglesHorizontal; j++) {

            r.top = i * rectangleSize;//  w w  w . j  av a2  s . co m
            r.left = j * rectangleSize;
            r.bottom = r.top + rectangleSize;
            r.right = r.left + rectangleSize;
            final Paint paint = new Paint();
            paint.setColor(isWhite ? Color.WHITE : Color.GRAY);

            canvas.drawRect(r, paint);

            isWhite = !isWhite;
        }

        verticalStartWhite = !verticalStartWhite;

    }
    canvas.drawColor(color);
    if (border) {
        final Paint paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setStrokeWidth(1f * density);
        final float[] points = new float[] { 0, 0, width, 0, 0, 0, 0, height, width, 0, width, height, 0,
                height, width, height };
        canvas.drawLines(points, paint);
    }
    return bm;
}

From source file:Main.java

public static Bitmap getColorPreviewBitmap(final Context context, final int color, final boolean border) {
    if (context == null)
        return null;
    final float density = context.getResources().getDisplayMetrics().density;
    final int width = (int) (32 * density), height = (int) (32 * density);

    final Bitmap bm = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    final Canvas canvas = new Canvas(bm);

    final int rectrangleSize = (int) (density * 5);
    final int numRectanglesHorizontal = (int) Math.ceil(width / rectrangleSize);
    final int numRectanglesVertical = (int) Math.ceil(height / rectrangleSize);
    final Rect r = new Rect();
    boolean verticalStartWhite = true;
    for (int i = 0; i <= numRectanglesVertical; i++) {

        boolean isWhite = verticalStartWhite;
        for (int j = 0; j <= numRectanglesHorizontal; j++) {

            r.top = i * rectrangleSize;/*from  w  w w. j a  v  a 2  s  .com*/
            r.left = j * rectrangleSize;
            r.bottom = r.top + rectrangleSize;
            r.right = r.left + rectrangleSize;
            final Paint paint = new Paint();
            paint.setColor(isWhite ? Color.WHITE : Color.GRAY);

            canvas.drawRect(r, paint);

            isWhite = !isWhite;
        }

        verticalStartWhite = !verticalStartWhite;

    }
    canvas.drawColor(color);
    if (border) {
        final Paint paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setStrokeWidth(1f * density);
        final float[] points = new float[] { 0, 0, width, 0, 0, 0, 0, height, width, 0, width, height, 0,
                height, width, height };
        canvas.drawLines(points, paint);
    }
    return bm;
}

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 == UNCONSTRAINED) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
    int upperBound = (minSideLength == UNCONSTRAINED) ? 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  ww  w  .  java 2 s .c  o  m

    if ((maxNumOfPixels == UNCONSTRAINED) && (minSideLength == UNCONSTRAINED)) {
        return 1;
    } else if (minSideLength == UNCONSTRAINED) {
        return lowerBound;
    } else {
        return upperBound;
    }
}