Example usage for java.lang Math max

List of usage examples for java.lang Math max

Introduction

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

Prototype

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

Source Link

Document

Returns the greater of two double values.

Usage

From source file:Main.java

private static int computeInitialSampleSize(int w, int h, int minSideLength, int maxNumOfPixels) {
    if (maxNumOfPixels == UNCONSTRAINED && minSideLength == UNCONSTRAINED)
        return 1;

    int lowerBound = (maxNumOfPixels == UNCONSTRAINED) ? 1
            : (int) Math.ceil(Math.sqrt((float) (w * h) / maxNumOfPixels));

    if (minSideLength == UNCONSTRAINED) {
        return lowerBound;
    } else {//from  w  w w . ja  v  a 2s  . co m
        int sampleSize = Math.min(w / minSideLength, h / minSideLength);
        return Math.max(sampleSize, lowerBound);
    }
}

From source file:Main.java

private static int computeInitialSampleSize(int w, int h, int minSideLength, int maxNumOfPixels) {
    if (maxNumOfPixels == UNCONSTRAINED && minSideLength == UNCONSTRAINED)
        return 1;

    int lowerBound = (maxNumOfPixels == UNCONSTRAINED) ? 1
            : (int) Math.ceil(Math.sqrt((double) (w * h) / maxNumOfPixels));

    if (minSideLength == UNCONSTRAINED) {
        return lowerBound;
    } else {//w  w w .j av a  2  s.  c om
        int sampleSize = Math.min(w / minSideLength, h / minSideLength);
        return Math.max(sampleSize, lowerBound);
    }
}

From source file:Main.java

/**
 * Return a float value within the range.
 * This is just a wrapper for Math.min() and Math.max().
 * This may be useful if you feel it confusing ("Which is min and which is max?").
 *
 * @param value    the target value//from   www  . ja v a 2  s.c o m
 * @param minValue minimum value. If value is less than this, minValue will be returned
 * @param maxValue maximum value. If value is greater than this, maxValue will be returned
 * @return float value limited to the range
 */
public static float getFloat(final float value, final float minValue, final float maxValue) {
    return Math.min(maxValue, Math.max(minValue, value));
}

From source file:RectUtils.java

/**
 * Unions the pair of source <code>Rectangle2D</code> objects and puts the
 * result into the returned <code>Rectangle2D</code> object. This method
 * extends the Rectangle2D version by checking for null parameters, the
 * returned value will also be <code>null</code> if the two input
 * rectangles are <code>null</code>
 * /*from  w  w  w .j a v  a  2 s  .c  o m*/
 * @param src1
 *            the first of a pair of <code>Rectangle2D</code> objects to
 *            be combined with each other
 * @param src2
 *            the second of a pair of <code>Rectangle2D</code> objects to
 *            be combined with each other
 * 
 */
public static Rectangle2D union(Rectangle2D src1, Rectangle2D src2) {
    Rectangle2D result = null;
    if (src1 == null && src2 == null) {
        result = null;
    } else if (src1 != null && src2 != null) {
        double x1 = Math.min(src1.getMinX(), src2.getMinX());
        double y1 = Math.min(src1.getMinY(), src2.getMinY());
        double x2 = Math.max(src1.getMaxX(), src2.getMaxX());
        double y2 = Math.max(src1.getMaxY(), src2.getMaxY());
        result = new Rectangle2D.Double();
        result.setFrameFromDiagonal(x1, y1, x2, y2);
    } else if (src1 != null) {
        double x1 = src1.getMinX();
        double y1 = src1.getMinY();
        double x2 = src1.getMaxX();
        double y2 = src1.getMaxY();
        result = new Rectangle2D.Double();
        result.setFrameFromDiagonal(x1, y1, x2, y2);
    } else {
        // only src2 is non-null
        double x1 = src2.getMinX();
        double y1 = src2.getMinY();
        double x2 = src2.getMaxX();
        double y2 = src2.getMaxY();
        result = new Rectangle2D.Double();
        result.setFrameFromDiagonal(x1, y1, x2, y2);
    }
    return result;
}

From source file:Main.java

/**
 * Returns maximum component width./*ww w .j  a va 2s  .  c om*/
 *
 * @param components
 *            components to process
 * @return maximum component width
 */
public static int maxWidth(final Component... components) {
    int max = 0;
    for (final Component component : components) {
        max = Math.max(max, component.getPreferredSize().width);
    }
    return max;
}

From source file:Main.java

/**
 * Centers the frame on the screen and sets its bounds. THis method should be
 * used after you call frame.pack() or frame.setSize().
 * @param frame/* w w w .  j a v a2 s  .  c  o m*/
 */
public static void centerFrame(JFrame frame) {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    Point center = ge.getCenterPoint();
    Rectangle bounds = ge.getMaximumWindowBounds();
    int w = Math.max(bounds.width / 2, Math.min(frame.getWidth(), bounds.width));
    int h = Math.max(bounds.height / 2, Math.min(frame.getHeight(), bounds.height));
    int x = center.x - w / 2, y = center.y - h / 2;

    frame.setBounds(x, y, w, h);

    if ((w == bounds.width) && (h == bounds.height)) {
        frame.setExtendedState(Frame.MAXIMIZED_BOTH);
    }

    frame.validate();
}

From source file:Main.java

private static int darkenColor(int color, float fraction) {
    return (int) Math.max(color - (color * fraction), 0);
}

From source file:Main.java

/**
 * Returns maximum component height.//  w w w  .ja  v a2 s  . c  o m
 *
 * @param components
 *            components to process
 * @return maximum component height
 */
public static int maxHeight(final Component... components) {
    int max = 0;
    for (final Component component : components) {
        max = Math.max(max, component.getPreferredSize().height);
    }
    return max;
}

From source file:Main.java

/**
 * Takes the last name of a full XML tag name. If it consists of a generic
 * part and a specific part, then the generic part is removed. examples :
 * "model.infrastructure.node-edge" becomes "edge" : "model.infrastructure'
 * becomes "infrastructure"//from w  w w. ja v a2s  .  co m
 *
 * @param fullName The full XML tag name to convert
 * @return The result
 */
public static String getSpecificLastName(String fullName) {
    int beginIndex = Math.max(Math.max(fullName.lastIndexOf('.'), fullName.lastIndexOf('-')), 0) + 1;
    return fullName.substring(beginIndex, fullName.length());
}

From source file:Main.java

/**
 * Convert a color to a HSL array./*from   w w w .jav a2  s.  c  o m*/
 * 
 * @param color The color to convert.
 * @param hsl A size-3 array to load with the HSL values.
 */
public static void colorToHsl(int color, float[] hsl) {
    float r = ((0x00ff0000 & color) >> 16) / 255.0F;
    float g = ((0x0000ff00 & color) >> 8) / 255.0F;
    float b = ((0x000000ff & color)) / 255.0F;
    float max = Math.max(Math.max(r, g), b);
    float min = Math.min(Math.min(r, g), b);
    float c = max - min;

    float hTemp = 0.0F;
    if (c == 0) {
        hTemp = 0;
    } else if (max == r) {
        hTemp = (float) (g - b) / c;
        if (hTemp < 0)
            hTemp += 6.0F;
    } else if (max == g) {
        hTemp = (float) (b - r) / c + 2.0F;
    } else if (max == b) {
        hTemp = (float) (r - g) / c + 4.0F;
    }
    float h = 60.0F * hTemp;

    float l = (max + min) * 0.5F;

    float s;
    if (c == 0) {
        s = 0.0F;
    } else {
        s = c / (1 - Math.abs(2.0F * l - 1.0F));
    }

    hsl[0] = h;
    hsl[1] = s;
    hsl[2] = l;
}