Example usage for java.lang Math min

List of usage examples for java.lang Math min

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public static double min(double a, double b) 

Source Link

Document

Returns the smaller of two double values.

Usage

From source file:at.tuwien.ifs.commons.util.MathUtils.java

/** contrains a value within the given lower and upper boundaries */
public static final int constrainWithin(int i, int lower, int upper) {
    return Math.max(lower, Math.min(i, upper));
}

From source file:Main.java

/**
 * Creates an animation to fade the dialog opacity from 0 to 1, wait at 1
 * and then fade to 0 and dispose.//from   ww w . j  a  v a 2s .c o  m
 *
 * @param dialog the dialog to display
 * @param delay the delay in ms before starting and between each change
 * @param incrementSize the increment size
 * @param displayTime the time in ms the dialog is fully visible
 */
public static void fadeInAndOut(final JDialog dialog, final int delay, final float incrementSize,
        final int displayTime) {
    final Timer timer = new Timer(delay, null);
    timer.setRepeats(true);
    timer.addActionListener(new ActionListener() {
        private float opacity = 0;
        private boolean displayed = false;

        @Override
        public void actionPerformed(ActionEvent e) {

            if (!displayed) {
                opacity += incrementSize;
                dialog.setOpacity(Math.min(opacity, 1)); // requires java 1.7
                if (opacity >= 1) {
                    timer.setDelay(displayTime);
                    displayed = true;
                }
            } else {
                timer.setDelay(delay);
                opacity -= incrementSize;
                dialog.setOpacity(Math.max(opacity, 0)); // requires java 1.7
                if (opacity < 0) {
                    timer.stop();
                    dialog.dispose();
                }
            }
        }
    });

    dialog.setOpacity(0); // requires java 1.7
    timer.start();
    dialog.setVisible(true);
}

From source file:de.tudarmstadt.ukp.experiments.argumentation.sequence.significance.LiddellsExactTest.java

public static double pValue(int b, int d) {
    double r = Math.max(b, d);
    double s = Math.min(b, d);

    double f = r / (s + 1);

    double mm = 2 * (s + 1);
    double nn = 2 * r;

    double betaPar1 = 1.0 / (1 + mm * f / nn);
    double betaPar2 = nn / 2;
    double betaPar3 = mm / 2;

    double beta = Beta.regularizedBeta(betaPar1, betaPar2, betaPar3);

    double pValue = 2 * (1 - (1 - beta));

    //        if (pValue < 0.1) {
    //            System.out.println(r);
    //            System.out.println(s);
    //        }/*from   w  ww .j av a  2  s. co m*/

    return pValue;
}

From source file:TextFieldViews.java

public static void displayView(View view, int indent, Document doc, PrintStream out) {
    String name = view.getClass().getName();
    for (int i = 0; i < indent; i++) {
        out.print("\t");
    }//w  w  w. j av a  2s.  c  om

    int start = view.getStartOffset();
    int end = view.getEndOffset();
    out.println(name + "; offsets [" + start + ", " + end + "]");
    int viewCount = view.getViewCount();
    if (viewCount == 0) {
        int length = Math.min(32, end - start);
        try {
            String txt = doc.getText(start, length);
            for (int i = 0; i < indent + 1; i++) {
                out.print("\t");
            }
            out.println("[" + txt + "]");
        } catch (BadLocationException e) {
        }
    } else {
        for (int i = 0; i < viewCount; i++) {
            displayView(view.getView(i), indent + 1, doc, out);
        }
    }
}

From source file:Main.java

public static byte[] copyOfRange(final byte[] original, final int from, final int to) {
    final int newLength = to - from;
    if (newLength < 0) {
        throw new IllegalArgumentException(from + " > " + to);
    }/*w w w .j a v a2 s .  c o  m*/
    final byte[] copy = new byte[newLength];
    System.arraycopy(original, from, copy, 0, Math.min(original.length - from, newLength));
    return copy;
}

From source file:Main.java

public static int HSLtoRGB(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://from w w  w . jav  a 2 s  . c o m
        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

/**
 * Trims the transparent pixels from the given {@link BufferedImage} (returns a sub-image).
 *
 * @param source The source image.//from  w ww  . j a  v  a 2  s.  c o  m
 * @return A new, trimmed image, or the source image if no trim is performed.
 */
public static BufferedImage trimmedImage(BufferedImage source) {
    final int minAlpha = 1;
    final int srcWidth = source.getWidth();
    final int srcHeight = source.getHeight();
    Raster raster = source.getRaster();
    int l = srcWidth, t = srcHeight, r = 0, b = 0;

    int alpha, x, y;
    int[] pixel = new int[4];
    for (y = 0; y < srcHeight; y++) {
        for (x = 0; x < srcWidth; x++) {
            raster.getPixel(x, y, pixel);
            alpha = pixel[3];
            if (alpha >= minAlpha) {
                l = Math.min(x, l);
                t = Math.min(y, t);
                r = Math.max(x, r);
                b = Math.max(y, b);
            }
        }
    }

    if (l > r || t > b) {
        // No pixels, couldn't trim
        return source;
    }

    return source.getSubimage(l, t, r - l + 1, b - t + 1);
}

From source file:Main.java

/**
 * This method determines if the direction of a substring is right-to-left.
 * If the string is empty that determination is based on the default system language
 * Locale.getDefault()./*from   w ww .j a v  a  2 s.c  om*/
 * The method can handle invalid substring definitions (start > end etc.), in which case the
 * method returns False.
 *
 * @return True if the text direction is right-to-left, false otherwise.
 */
public static boolean isRTL(CharSequence s, int start, int end) {
    if (s == null || s.length() == 0) {
        // empty string --> determine the direction from the default language
        return isRTL(Locale.getDefault());
    }

    if (start == end) {
        // if no character is selected we need to expand the selection
        start = Math.max(0, --start);
        if (start == end) {
            end = Math.min(s.length(), ++end);
        }
    }

    try {
        Bidi bidi = new Bidi(s.subSequence(start, end).toString(), Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
        return !bidi.baseIsLeftToRight();
    } catch (IndexOutOfBoundsException e) {
        return false;
    }
}

From source file:lu.lippmann.cdb.ext.hydviga.ui.GapsUIUtil.java

public static ChartPanel buildGapChartPanel(final Instances dataSet, final int dateIdx, final Attribute attr,
        final int gapsize, final int position) throws Exception {
    Instances filteredDs = WekaDataProcessingUtil.buildFilteredByAttributesDataSet(dataSet,
            new int[] { attr.index(), dateIdx });
    filteredDs = WekaDataProcessingUtil.buildFilteredDataSet(filteredDs, 0, filteredDs.numAttributes() - 1,
            Math.max(0, position - GapsUtil.VALUES_BEFORE_AND_AFTER_RATIO * gapsize),
            Math.min(position + gapsize + GapsUtil.VALUES_BEFORE_AND_AFTER_RATIO * gapsize,
                    filteredDs.numInstances() - 1));

    final ChartPanel cp = TimeSeriesChartUtil.buildChartPanelForAllAttributes(filteredDs, false,
            WekaDataStatsUtil.getFirstDateAttributeIdx(filteredDs), null);

    final XYPlot xyp = (XYPlot) cp.getChart().getPlot();
    xyp.getDomainAxis().setLabel("");
    xyp.getRangeAxis().setLabel("");

    final Marker gapBeginMarker = new ValueMarker(dataSet.instance(Math.max(0, position - 1)).value(dateIdx));
    gapBeginMarker.setPaint(Color.RED);
    gapBeginMarker.setLabel("Gap begin");
    gapBeginMarker.setLabelAnchor(RectangleAnchor.TOP_LEFT);
    gapBeginMarker.setLabelTextAnchor(TextAnchor.TOP_RIGHT);
    cp.getChart().getXYPlot().addDomainMarker(gapBeginMarker);

    final Marker gapEndMarker = new ValueMarker(
            dataSet.instance(Math.min(dataSet.numInstances() - 1, position + gapsize)).value(dateIdx));
    gapEndMarker.setPaint(Color.RED);
    gapEndMarker.setLabel("Gap end");
    gapEndMarker.setLabelAnchor(RectangleAnchor.TOP_RIGHT);
    gapEndMarker.setLabelTextAnchor(TextAnchor.TOP_LEFT);
    cp.getChart().getXYPlot().addDomainMarker(gapEndMarker);

    addExportPopupMenu(filteredDs, cp);/*from   w w w .ja va  2 s  .  c o m*/

    return cp;
}

From source file:Main.java

/**
 * Resolves the relative path of the specified artifact
 *
 * @param outputDirectory typically the parent directory of the directory containing the makefile
 * @param file//from w ww .java  2s . c o  m
 * @return
 */
protected static String resolveRelativePath(File outputDirectory, File file) throws IOException {
    String resolvedPath = file.getCanonicalPath();

    String strOutputDirectoryPath = outputDirectory.getCanonicalPath();
    String strFilePath = file.getCanonicalPath();
    //System.out.println( "Resolving " + strFilePath + " against " + strOutputDirectoryPath );

    if (strFilePath.startsWith(strOutputDirectoryPath)) {
        // Simple case where file is in a subdirectory of outputDirectory
        resolvedPath = strFilePath.substring(strOutputDirectoryPath.length() + 1);
    } else {
        // Look for commonality in paths
        List<String> outputDirectoryPathParts = splitPath(outputDirectory.getCanonicalFile());
        List<String> filePathParts = splitPath(file.getCanonicalFile());
        int commonDepth = 0;
        int maxCommonDepth = Math.min(outputDirectoryPathParts.size(), filePathParts.size());
        for (int i = 0; (i < maxCommonDepth)
                && outputDirectoryPathParts.get(i).equals(filePathParts.get(i)); i++) {
            commonDepth++;
        }
        // If there is a common root build a relative path between the common roots
        if (commonDepth > 0) {
            final StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("..");
            for (int i = 0; i < outputDirectoryPathParts.size() - commonDepth - 1; i++) {
                stringBuilder.append(File.separator);
                stringBuilder.append("..");
            }
            for (int i = commonDepth; i < filePathParts.size(); i++) {
                stringBuilder.append(File.separator);
                stringBuilder.append(filePathParts.get(i));
            }
            resolvedPath = stringBuilder.toString();
        } else {
            if (IS_WINDOWS) {
                // Windows has no common root directory, cannot resolve a path
                // across drives so ...
                throw new IOException("Unable to resolve relative path across windows drives");
            }

            // no intersection between paths so calculate a path via the root directory
            final StringBuilder stringBuilder = new StringBuilder();

            File depthCheck = outputDirectory.getParentFile();
            while (depthCheck != null) {
                if (stringBuilder.length() > 0) {
                    stringBuilder.append(File.separator);
                }
                stringBuilder.append("..");
                depthCheck = depthCheck.getParentFile();
            }

            resolvedPath = stringBuilder.toString() + strFilePath;
        }
    }

    //System.out.println( "resolvedPath = " + resolvedPath );
    return resolvedPath;
}