Example usage for java.lang Math abs

List of usage examples for java.lang Math abs

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public static double abs(double a) 

Source Link

Document

Returns the absolute value of a double value.

Usage

From source file:Main.java

/**
 * Get the optimal preview size for the given screen size.
 * @param sizes//w w  w  . jav  a2 s. c o m
 * @param screenWidth
 * @param screenHeight
 * @return
 */
public static Size getOptimalPreviewSize(List<Size> sizes, int screenWidth, int screenHeight) {
    double aspectRatio = ((double) screenWidth) / screenHeight;
    Size optimalSize = null;
    for (Iterator<Size> iterator = sizes.iterator(); iterator.hasNext();) {
        Size currSize = iterator.next();
        double curAspectRatio = ((double) currSize.width) / currSize.height;
        //do the aspect ratios equal?
        if (Math.abs(aspectRatio - curAspectRatio) < epsilon) {
            //they do
            if (optimalSize != null) {
                //is the current size smaller than the one before
                if (optimalSize.height > currSize.height && optimalSize.width > currSize.width) {
                    optimalSize = currSize;
                }
            } else {
                optimalSize = currSize;
            }
        }
    }
    if (optimalSize == null) {
        //did not find a size with the correct aspect ratio.. let's choose the smallest instead
        for (Iterator<Size> iterator = sizes.iterator(); iterator.hasNext();) {
            Size currSize = iterator.next();
            if (optimalSize != null) {
                //is the current size smaller than the one before
                if (optimalSize.height > currSize.height && optimalSize.width > currSize.width) {
                    optimalSize = currSize;
                } else {
                    optimalSize = currSize;
                }
            } else {
                optimalSize = currSize;
            }

        }
    }
    return optimalSize;
}

From source file:Main.java

public static int imageDifference(BufferedImage imgA, BufferedImage imgB) {

    int dHeight = Math.abs(imgA.getHeight() - imgB.getHeight());
    int dWidth = Math.abs(imgA.getWidth() - imgB.getWidth());

    int diff = 3 * 255 * dHeight * dWidth;
    for (int y = 0; y < Math.min(imgA.getHeight(), imgB.getHeight()); y++) {
        for (int x = 0; x < Math.min(imgA.getWidth(), imgB.getWidth()); x++) {
            final int colourA = imgA.getRGB(x, y);
            final int colourB = imgB.getRGB(x, y);

            diff += Math.abs((int) ((colourA & 0x00ff0000) >> 16) - (int) ((colourB & 0x00ff0000) >> 16));
            diff += Math.abs((int) ((colourA & 0x0000ff00) >> 8) - (int) ((colourB & 0x0000ff00) >> 8));
            diff += Math.abs((int) (colourA & 0x000000ff) - (int) (colourB & 0x000000ff));
        }/*from   ww  w.  j  a  va 2  s  .co  m*/
    }

    return diff;
}

From source file:Main.java

public static double transformlng(double lng, double lat) {
    double ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + 0.1 * lng * lat + 0.1 * Math.sqrt(Math.abs(lng));
    ret += (20.0 * Math.sin(6.0 * lng * pi) + 20.0 * Math.sin(2.0 * lng * pi)) * 2.0 / 3.0;
    ret += (20.0 * Math.sin(lng * pi) + 40.0 * Math.sin(lng / 3.0 * pi)) * 2.0 / 3.0;
    ret += (150.0 * Math.sin(lng / 12.0 * pi) + 300.0 * Math.sin(lng / 30.0 * pi)) * 2.0 / 3.0;
    return ret;/* ww  w.j  av a 2 s .c  o m*/
}

From source file:Main.java

public static boolean isZero(float f) {
    return Float.compare(Math.abs(f), EPSILON) < 0;
}

From source file:Main.java

public static void RGBtoHSL(int r, int g, int b, float[] hsl) {
    final float rf = r / 255f;
    final float gf = g / 255f;
    final float bf = b / 255f;

    final float max = Math.max(rf, Math.max(gf, bf));
    final float min = Math.min(rf, Math.min(gf, bf));
    final float deltaMaxMin = max - min;

    float h, s;//from w w w . j  ava  2 s .c o  m
    float l = (max + min) / 2f;

    if (max == min) {
        // Monochromatic
        h = s = 0f;
    } else {
        if (max == rf) {
            h = ((gf - bf) / deltaMaxMin) % 6f;
        } else if (max == gf) {
            h = ((bf - rf) / deltaMaxMin) + 2f;
        } else {
            h = ((rf - gf) / deltaMaxMin) + 4f;
        }

        s = deltaMaxMin / (1f - Math.abs(2f * l - 1f));
    }

    hsl[0] = (h * 60f) % 360f;
    hsl[1] = s;
    hsl[2] = l;
}

From source file:Main.java

public static byte[] createSequence(byte begin, byte end) {
    int sign = end > begin ? 1 : -1;
    int len = Math.abs(end - begin) + 1;
    byte[] seq = new byte[len];
    for (int i = 0; i < len; i++) {
        seq[i] = (byte) (begin + i * sign);
    }/*w  ww.j a va  2 s.  c om*/
    return seq;
}

From source file:Main.java

/**
 * Returns true if {@code value} is 'close' to it's closest decimal value. Close is currently
 * defined as it's difference being < 0.001.
 *///from w  w  w  .  j  a  v a2  s.com
private static boolean isClose(float value, float targetValue) {
    return Math.abs(value - targetValue) < 0.001f;
}

From source file:Main.java

/**
 * Compute the distance between two colors. The alpha channel is ignored. Returned distance is
 * always >= 0.//from w ww.  j a va  2s. com
 */
public static final int distance(int r, int g, int b, int color) {
    return Math.abs(r - Color.red(color)) + Math.abs(g - Color.green(color)) + Math.abs(b - Color.blue(color));
}

From source file:Main.java

private static double reduceSinAngle(double radians) {
    radians %= Math.PI * 2.0;/*from  w  w  w  .ja  va2s . c  om*/
    if (Math.abs(radians) > Math.PI) {
        radians = radians - (Math.PI * 2.0);
    }
    if (Math.abs(radians) > Math.PI / 2) {
        radians = Math.PI - radians;
    }
    return radians;
}

From source file:Main.java

public static void drawImage(BufferedImage image, Graphics g, Component parent) {
    Dimension size = parent.getSize();
    Color background = parent.getBackground();
    if (image != null && size.width > 0) {
        double ratio = (double) image.getHeight(null) / image.getWidth(null);

        int effectiveWidth = 1;
        int effectiveHeight = (int) ratio;

        while (effectiveHeight < size.height && effectiveWidth < size.width) {
            effectiveWidth++;// www .  j av  a 2 s  . co  m
            effectiveHeight = (int) (ratio * effectiveWidth);
        }

        g.setColor(background);
        g.fillRect(0, 0, size.width, size.height);

        int cornerx = Math.abs((size.width - effectiveWidth) / 2);
        int cornery = Math.abs((size.height - effectiveHeight) / 2);
        g.drawImage(image, cornerx, cornery, effectiveWidth + cornerx, effectiveHeight + cornery, 0, 0,
                image.getWidth(null), image.getHeight(null), null);
    }
}