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

/**
 * Calculeaza deviatia medie de la media pentru datele primite
 *
 * @param data/*from www . j a  v  a  2  s.  c  om*/
 * @return deviatia medie
 */
public static double computeAverageFluctuations(List<Double> data) {
    double sum = 0;
    int index = 0;
    double media = 0;
    double max = 0;

    if (data.isEmpty()) {
        return 0;
    }

    for (Double i : data) {
        sum = sum + i;
    }

    media = sum / data.size();
    double dev = 0;

    for (Double i : data) {
        dev += Math.abs(media - i);
    }
    return dev / data.size();
}

From source file:Main.java

/**
 * Calculeaza deviatia maxima de la media pentru datele primite
 *
 * @param data/* w  w w .  j  a v a2s  .  c o  m*/
 * @return deviatia maxima de la medie
 */
public static double computeFluctuations(List<Double> data) {
    double sum = 0;
    int index = 0;
    double media = 0;
    double max = 0;

    if (data.isEmpty()) {
        return 0f;
    }

    for (Double i : data) {
        sum = sum + i;
    }

    media = sum / data.size();

    for (Double i : data) {
        Double difference = Math.abs(media - i);
        if (max < difference) {
            max = difference;
        }
    }
    return max;
}

From source file:Main.java

public static Camera.Size getOptimalPreviewSize2(List<Camera.Size> sizes, double targetRatio) {
    final double SMALL_VALUE = 0.001;
    if (sizes == null)
        return null;

    Camera.Size optimalSize = null;//from  w  w w  .j  a v a2s.  com
    double optimalRatio = 0;
    double minRatioDiff = Double.MAX_VALUE;

    // Try to find a size which is close to the targetRatio and has the biggest possible resolution
    for (Camera.Size size : sizes) {
        double ratio = (double) size.width / size.height;
        if (Math.abs(targetRatio - ratio) <= minRatioDiff) {
            if (optimalSize != null && Math.abs(ratio - optimalRatio) < SMALL_VALUE) {
                if (size.width < optimalSize.width)
                    continue;
            }
            optimalSize = size;
            optimalRatio = (double) size.width / size.height;
            minRatioDiff = Math.abs(targetRatio - ratio);
        }
    }
    return optimalSize;
}

From source file:Main.java

public static int findBestMotZoomValue(CharSequence stringValues, int tenDesiredZoom) {
    int tenBestValue = 0;
    for (String stringValue : COMMA_PATTERN.split(stringValues)) {
        stringValue = stringValue.trim();
        double value;
        try {//  w w  w.j av a2  s.co m
            value = Double.parseDouble(stringValue);
        } catch (NumberFormatException nfe) {
            return tenDesiredZoom;
        }
        int tenValue = (int) (10.0 * value);
        if (Math.abs(tenDesiredZoom - value) < Math.abs(tenDesiredZoom - tenBestValue)) {
            tenBestValue = tenValue;
        }
    }
    return tenBestValue;
}

From source file:Main.java

public static int getTextViewMeasureWidth(TextView textView, String text) {
    if (textView == null)
        return 0;
    TextPaint paint = textView.getPaint();
    Rect rect = new Rect();
    paint.getTextBounds(text, 0, text.length(), rect);
    return Math.abs(rect.right - rect.left);
}

From source file:Main.java

/**
 * Function: arcToCurves//from   w w  w  . jav a2s .c  o  m
 * 
 * Converts the given arc to a series of curves.
 */
public static double[] arcToCurves(double x0, double y0, double r1, double r2, double angle,
        double largeArcFlag, double sweepFlag, double x, double y) {
    x -= x0;
    y -= y0;

    if (r1 == 0 || r2 == 0) {
        return new double[0];
    }

    double fS = sweepFlag;
    double psai = angle;
    r1 = Math.abs(r1);
    r2 = Math.abs(r2);
    double ctx = -x / 2;
    double cty = -y / 2;
    double cpsi = Math.cos(psai * Math.PI / 180);
    double spsi = Math.sin(psai * Math.PI / 180);
    double rxd = cpsi * ctx + spsi * cty;
    double ryd = -1 * spsi * ctx + cpsi * cty;
    double rxdd = rxd * rxd;
    double rydd = ryd * ryd;
    double r1x = r1 * r1;
    double r2y = r2 * r2;
    double lamda = rxdd / r1x + rydd / r2y;
    double sds;

    if (lamda > 1) {
        r1 = Math.sqrt(lamda) * r1;
        r2 = Math.sqrt(lamda) * r2;
        sds = 0;
    } else {
        double seif = 1;

        if (largeArcFlag == fS) {
            seif = -1;
        }

        sds = seif * Math.sqrt((r1x * r2y - r1x * rydd - r2y * rxdd) / (r1x * rydd + r2y * rxdd));
    }

    double txd = sds * r1 * ryd / r2;
    double tyd = -1 * sds * r2 * rxd / r1;
    double tx = cpsi * txd - spsi * tyd + x / 2;
    double ty = spsi * txd + cpsi * tyd + y / 2;
    double rad = Math.atan2((ryd - tyd) / r2, (rxd - txd) / r1) - Math.atan2(0, 1);
    double s1 = (rad >= 0) ? rad : 2 * Math.PI + rad;
    rad = Math.atan2((-ryd - tyd) / r2, (-rxd - txd) / r1) - Math.atan2((ryd - tyd) / r2, (rxd - txd) / r1);
    double dr = (rad >= 0) ? rad : 2 * Math.PI + rad;

    if (fS == 0 && dr > 0) {
        dr -= 2 * Math.PI;
    } else if (fS != 0 && dr < 0) {
        dr += 2 * Math.PI;
    }

    double sse = dr * 2 / Math.PI;
    int seg = (int) Math.ceil(sse < 0 ? -1 * sse : sse);
    double segr = dr / seg;
    double t = 8 / 3 * Math.sin(segr / 4) * Math.sin(segr / 4) / Math.sin(segr / 2);
    double cpsir1 = cpsi * r1;
    double cpsir2 = cpsi * r2;
    double spsir1 = spsi * r1;
    double spsir2 = spsi * r2;
    double mc = Math.cos(s1);
    double ms = Math.sin(s1);
    double x2 = -t * (cpsir1 * ms + spsir2 * mc);
    double y2 = -t * (spsir1 * ms - cpsir2 * mc);
    double x3 = 0;
    double y3 = 0;

    double[] result = new double[seg * 6];

    for (int n = 0; n < seg; ++n) {
        s1 += segr;
        mc = Math.cos(s1);
        ms = Math.sin(s1);

        x3 = cpsir1 * mc - spsir2 * ms + tx;
        y3 = spsir1 * mc + cpsir2 * ms + ty;
        double dx = -t * (cpsir1 * ms + spsir2 * mc);
        double dy = -t * (spsir1 * ms - cpsir2 * mc);

        // CurveTo updates x0, y0 so need to restore it
        int index = n * 6;
        result[index] = x2 + x0;
        result[index + 1] = y2 + y0;
        result[index + 2] = x3 - dx + x0;
        result[index + 3] = y3 - dy + y0;
        result[index + 4] = x3 + x0;
        result[index + 5] = y3 + y0;

        x2 = x3 + dx;
        y2 = y3 + dy;
    }

    return result;
}

From source file:Main.java

public static int XiAnSection(int startId, int endId) {

    if (startId < 20 && endId < 20) {
        return Math.abs(startId - endId);
    }//from ww  w. j ava2 s .  c  o m

    if (startId >= 20 && endId >= 20 && startId <= 40 && endId <= 40) {
        return Math.abs(startId - endId);
    } else {

        if (startId <= 19) {
            int a = Math.abs(startId - 10);
            int b = Math.abs(endId - 29);
            return a + b;
        } else {
            int a = Math.abs(startId - 29);
            int b = Math.abs(endId - 10);
            return a + b;
        }

    }

}

From source file:Main.java

public static void copyMoveSegment(Object source, Object dest, int size, int index, int segmentSize,
        int destIndex) {

    boolean forward = index < destIndex;
    int sliceSize = forward ? index : destIndex;

    System.arraycopy(source, 0, dest, 0, sliceSize);

    sliceSize = forward ? size - destIndex - segmentSize : size - index - segmentSize;

    int sliceIndex = forward ? destIndex + segmentSize : index + segmentSize;

    System.arraycopy(source, sliceIndex, dest, sliceIndex, sliceSize);
    System.arraycopy(source, index, dest, destIndex, segmentSize);

    sliceSize = Math.abs(index - destIndex);
    sliceIndex = forward ? index + segmentSize : destIndex;

    int targetSliceIndex = forward ? index : destIndex + segmentSize;

    System.arraycopy(source, sliceIndex, dest, targetSliceIndex, sliceSize);
}

From source file:Main.java

private static Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w, int h) {
    final double ASPECT_TOLERANCE = 0.05;
    double targetRatio = (double) w / h;

    if (sizes == null) {
        return null;
    }//from w w  w .  j  av a  2s .  co  m

    Camera.Size optimalSize = null;

    double minDiff = Double.MAX_VALUE;

    int targetHeight = h;

    // Find size
    for (Camera.Size size : sizes) {
        double ratio = (double) size.width / size.height;
        if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) {
            continue;
        }
        if (Math.abs(size.height - targetHeight) < minDiff) {
            optimalSize = size;
            minDiff = Math.abs(size.height - targetHeight);
        }
    }

    if (optimalSize == null) {
        minDiff = Double.MAX_VALUE;
        for (Camera.Size size : sizes) {
            if (Math.abs(size.height - targetHeight) < minDiff) {
                optimalSize = size;
                minDiff = Math.abs(size.height - targetHeight);
            }
        }
    }
    return optimalSize;
}

From source file:Main.java

public static Bitmap filterImage(Bitmap src) {
    if (src == null) {
        return null;
    }/*from ww w. j  a  v a2  s  . c om*/

    final float minV = 0.7f;
    final float maxV = 0.85f;

    float[] hsvVals = getMainHSV(src);
    float deltaV = 0;
    if (hsvVals[2] < minV) {
        deltaV = minV - hsvVals[2];
    } else if (hsvVals[2] > maxV) {
        deltaV = maxV - hsvVals[2];
        if (hsvVals[1] < 0.1f) {
            deltaV *= 2;
        }
    }
    if (Math.abs(deltaV) < 0.0001f) {
        return src;
    }

    int w = src.getWidth();
    int h = src.getHeight();
    int[] pixels = new int[w * h];
    src.getPixels(pixels, 0, w, 0, 0, w, h);
    float[] HSV = new float[3];
    int index = 0;
    for (int y = 0; y < h; ++y) {
        for (int x = 0; x < w; ++x) {
            Color.colorToHSV(pixels[index], HSV);
            HSV[2] = clamp(HSV[2] + deltaV, 0f, 1f);
            pixels[index] = perferBlue(Color.HSVToColor(HSV));
            ++index;
        }
    }
    return Bitmap.createBitmap(pixels, w, h, Bitmap.Config.ARGB_8888);
}