Example usage for java.lang Math rint

List of usage examples for java.lang Math rint

Introduction

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

Prototype

public static double rint(double a) 

Source Link

Document

Returns the double value that is closest in value to the argument and is equal to a mathematical integer.

Usage

From source file:de.tor.tribes.ui.algo.TimeFrameVisualizer.java

private void renderDayMarkers(long pStart, long pEnd, Graphics2D pG2D) {
    Date d = new Date(pStart);
    d = DateUtils.setHours(d, 0);//from  w w  w. ja v  a 2s  .c o m
    d = DateUtils.setMinutes(d, 0);
    d = DateUtils.setSeconds(d, 0);
    d = DateUtils.setMilliseconds(d, 0);

    for (long mark = d.getTime(); mark <= pEnd; mark += DateUtils.MILLIS_PER_HOUR) {
        int markerPos = Math.round((mark - pStart) / DateUtils.MILLIS_PER_MINUTE);
        pG2D.setColor(Color.YELLOW);
        pG2D.fillRect(markerPos, 20, 2, 10);
        pG2D.setColor(Color.WHITE);
        pG2D.setFont(pG2D.getFont().deriveFont(Font.BOLD, 10.0f));
        pG2D.fillRect(markerPos - 5, 15, 12, 10);
        pG2D.setColor(Color.BLACK);
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date(mark));
        NumberFormat f = NumberFormat.getNumberInstance();
        f.setMinimumIntegerDigits(2);
        f.setMaximumFractionDigits(2);
        pG2D.drawString(f.format(cal.get(Calendar.HOUR_OF_DAY)), markerPos - 5, 23);
    }

    long currentDay = d.getTime();
    while (currentDay < pEnd) {
        currentDay += DateUtils.MILLIS_PER_DAY;
        int markerPos = Math.round((currentDay - pStart) / DateUtils.MILLIS_PER_MINUTE);
        pG2D.setColor(new Color(123, 123, 123));
        pG2D.fillRect(markerPos, 15, 3, 30);
        SimpleDateFormat f = new SimpleDateFormat("dd.MM.yy");
        String dayLabel = f.format(currentDay);
        Rectangle2D labelBounds = pG2D.getFontMetrics().getStringBounds(dayLabel, pG2D);
        pG2D.setColor(Color.YELLOW);
        int labelWidth = (int) labelBounds.getWidth() + 5;
        pG2D.fillRect(markerPos - (int) Math.rint(labelWidth / 2), 15, labelWidth, 10);
        pG2D.setColor(Color.BLACK);
        pG2D.setFont(pG2D.getFont().deriveFont(Font.BOLD, 10.0f));
        pG2D.drawString(dayLabel, markerPos - (int) Math.rint(labelWidth / 2) + 2, 23);
    }
}

From source file:de.xirp.ui.util.SWTUtil.java

/**
 * Rotates the given point around the given center.
 * //from  ww  w . ja v  a2s .  c om
 * @param p
 *            the point to rotate
 * @param center
 *            the center of rotation
 * @param phi
 *            the angle for rotation in radian.
 * @return the rotated point
 */
public static final Point rotate(Point p, Point center, double phi) {
    double cosPhi = MathLib.cos(phi);
    double sinPhi = MathLib.sin(phi);

    Point newPoint = new Point(p.x, p.y);

    newPoint.x = newPoint.x - center.x;
    newPoint.y = newPoint.y - center.y;

    double newX = sinPhi * newPoint.x + cosPhi * newPoint.y + center.x;
    double newY = -cosPhi * newPoint.x + sinPhi * newPoint.y + center.y;

    newPoint.x = (int) Math.rint(newX);
    newPoint.y = (int) Math.rint(newY);
    return newPoint;
}

From source file:org.apache.velocity.tools.generic.MathTool.java

/**
 * Takes the original argument(s) and returns the resulting value as
 * an instance of the best matching type (Integer, Long, or Double).
 * If either an argument or the result is not an integer (i.e. has no
 * decimal when rendered) the result will be returned as a Double.
 * If not and the result is < -2147483648 or > 2147483647, then a
 * Long will be returned.  Otherwise, an Integer will be returned.
 */// ww  w. jav a2  s  .  c  o m
protected Number matchType(Number in1, Number in2, double out) {
    //NOTE: if we just checked class types, we could miss custom
    //      extensions of java.lang.Number, and if we only checked
    //      the mathematical value, $math.div('3.0', 1) would render
    //      as '3'.  To get the expected result, we check what we're
    //      concerned about: the rendered string.

    // first check if the result is a whole number
    boolean isWhole = (Math.rint(out) == out);

    if (isWhole) {
        // assume that 1st arg is not null,
        // check for floating points
        String in = in1.toString();
        isWhole = (in.indexOf('.') < 0);

        // if we don't have a decimal yet but do have a second arg
        if (isWhole && in2 != null) {
            in = in2.toString();
            isWhole = (in.indexOf('.') < 0);
        }
    }

    if (!isWhole) {
        return new Double(out);
    } else if (out > Integer.MAX_VALUE || out < Integer.MIN_VALUE) {
        return new Long((long) out);
    } else {
        return new Integer((int) out);
    }
}

From source file:com.silverpeas.gallery.ImageHelper.java

private static void createWatermark(final OutputStream watermarkedTargetStream, final String watermarkLabel,
        final BufferedImage image, final int percentSizeWatermark) throws IOException {

    final int imageWidth = image.getWidth();
    final int imageHeight = image.getHeight();

    // cration du buffer a la mme taille
    final BufferedImage outputBuf = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);

    final double max = Math.max(imageWidth, imageHeight);

    // recherche de la taille du watermark en fonction de la taille de la photo
    int size = 8;
    if (max < 600) {
        size = 8;//from w  w  w .jav  a 2s  . c  om
    }
    if (max >= 600 && max < 750) {
        size = 10;
    }
    if (max >= 750 && max < 1000) {
        size = 12;
    }
    if (max >= 1000 && max < 1250) {
        size = 14;
    }
    if (max >= 1250 && max < 1500) {
        size = 16;
    }
    if (max >= 1500 && max < 1750) {
        size = 18;
    }
    if (max >= 1750 && max < 2000) {
        size = 20;
    }
    if (max >= 2000 && max < 2250) {
        size = 22;
    }
    if (max >= 2250 && max < 2500) {
        size = 24;
    }
    if (max >= 2500 && max < 2750) {
        size = 26;
    }
    if (max >= 2750 && max < 3000) {
        size = 28;
    }
    if (max >= 3000) {
        size = (int) Math.rint(max * percentSizeWatermark / 100);
    }
    final Watermarker watermarker = new Watermarker(imageWidth, imageHeight);
    watermarker.addWatermark(image, outputBuf, new Font("Arial", Font.BOLD, size), watermarkLabel, size);
    ImageIO.write(outputBuf, "JPEG", watermarkedTargetStream);
}

From source file:de.xirp.ui.util.SWTUtil.java

/**
 * Rotates the given point around the given center.
 * // ww  w.j a v a 2s.  c om
 * @param p
 *            the point to rotate
 * @param center
 *            the center of rotation
 * @param phi
 *            the angle for rotation in radian.
 * @return the rotated point
 */
public static final DoublePoint rotate(DoublePoint p, DoublePoint center, double phi) {
    double cosPhi = MathLib.cos(phi);
    double sinPhi = MathLib.sin(phi);

    DoublePoint newPoint = new DoublePoint(p.x, p.y);

    newPoint.x = newPoint.x - center.x;
    newPoint.y = newPoint.y - center.y;

    double newX = sinPhi * newPoint.x + cosPhi * newPoint.y + center.x;
    double newY = -cosPhi * newPoint.x + sinPhi * newPoint.y + center.y;

    newPoint.x = (int) Math.rint(newX);
    newPoint.y = (int) Math.rint(newY);
    return newPoint;
}

From source file:de.tor.tribes.ui.algo.TimeFrameVisualizer.java

/**Render default view if there is no timeframe yet*/
private void renderNoInfoView(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.setPaint(new TexturePaint(STROKED, new Rectangle(0, 0, 3, 3)));
    g2d.fillRect(0, 0, getWidth(), getHeight());
    Font f = g2d.getFont().deriveFont(Font.BOLD, 14.0f);
    g2d.setFont(f);//ww w.  j  a  va2s  .  c om
    Rectangle2D bounds = g2d.getFontMetrics().getStringBounds("Kein Zeitfenster aktiv", g);
    int dx = 10;
    if (getWidth() > bounds.getWidth()) {
        dx = (int) Math.rint((getWidth() - bounds.getWidth()) / 2);
    }

    int dy = 10;
    if (getHeight() > bounds.getHeight()) {
        dy = (int) Math.rint((getHeight() - bounds.getHeight()) / 2);
    }
    g2d.setColor(Color.black);
    g2d.drawString("Kein Zeitfenster aktiv", dx, dy);
}

From source file:org.opensha.commons.data.function.EvenlyDiscretizedFunc.java

/**
 * It finds out whether the X values are within tolerance of an integer value
 * @param tol tolerance value to consider  rounding errors
 *
 * @return true if all X values are within the tolerance of an integer value
 * else returns false//w  ww . java 2 s . c om
 */
public boolean areAllXValuesInteger(double tolerance) {

    double diff;
    // check that min X and delta are integer values
    diff = Math.abs(minX - Math.rint(minX));
    if (diff > tolerance)
        return false;
    diff = Math.abs(delta - Math.rint(delta));
    if (diff > tolerance)
        return false;
    return true;

}

From source file:org.opensingular.internal.lib.commons.xml.MElement.java

/**
 * Cria um no indicado pelo nome com o texto resultado da convero do
 * double segundo a preciso desejada.//from ww w.java2 s .  com
 *
 * @param nome     do element ou atributo a ser criado
 * @param valor    a ser atribuito
 * @param precisao Informa quantas casas depois d virgula deseja-se manter.
 *                 Se for negativo arredonta os digitos antes da virgula.
 * @return MElement criado ou dono do atributo criado
 */
public final MElement addElement(String nome, double valor, int precisao) {
    double m = Math.pow(10, precisao);
    String sValor = Double.toString(Math.rint(Math.round(valor * m)) / m);
    return addElement(nome, sValor);
}

From source file:de.tor.tribes.ui.views.DSWorkbenchFormFrame.java

private void centerFormOnMap() {
    List<AbstractForm> selection = getSelectedForms();
    if (selection.isEmpty()) {
        showError("Keine Zeichnung gewhlt");
        return;/*from ww  w .  java2 s  .  co m*/
    }
    Rectangle r = selection.get(0).getBounds();

    if (r != null) {
        DSWorkbenchMainFrame.getSingleton().centerPosition((int) Math.rint(r.getCenterX()),
                (int) Math.rint(r.getCenterY()));
    } else {
        showInfo("Ein Mittelpunkt kann fr diese Zeichnung nicht bestimmt werden");
    }
}

From source file:org.orekit.frames.PEFFrame.java

/** Set the reference points array.
 * @param t offset from J2000.0 epoch in seconds
 *//*from w w  w  .  java2s  . com*/
private void setReferencePoints(final double t) {

    final int n = dpsiRef.length;
    final int nM12 = (n - 1) / 2;

    // evaluate new location of center interval
    final double newTCenter = h * Math.floor(t / h);

    // shift reusable reference points
    int iMin = 0;
    int iMax = n;
    final int shift = (int) Math.rint((newTCenter - tCenter) / h);
    if (!Double.isNaN(tCenter) && (Math.abs(shift) < n)) {
        if (shift >= 0) {
            System.arraycopy(dpsiRef, shift, dpsiRef, 0, n - shift);
            System.arraycopy(depsRef, shift, depsRef, 0, n - shift);
            iMin = n - shift;
        } else {
            System.arraycopy(dpsiRef, 0, dpsiRef, -shift, n + shift);
            System.arraycopy(depsRef, 0, depsRef, -shift, n + shift);
            iMax = -shift;
        }
    }

    // compute new reference points
    tCenter = newTCenter;
    for (int i = iMin; i < iMax; ++i) {
        computeNutationElements(tCenter + (i - nM12) * h);
        dpsiRef[i] = dpsiCurrent;
        depsRef[i] = depsCurrent;
    }

}