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

public static Camera.Size findClosetPreviewSize(Camera camera, Point preferSize) {
    int preferX = preferSize.x;
    int preferY = preferSize.y;
    Camera.Parameters parameters = camera.getParameters();
    List<Camera.Size> allSupportSizes = parameters.getSupportedPreviewSizes();
    Log.d(TAG, "all support preview size: " + dumpPreviewSizeList(allSupportSizes));
    int minDiff = Integer.MAX_VALUE;
    int index = 0;
    for (int i = 0; i < allSupportSizes.size(); i++) {
        Camera.Size size = allSupportSizes.get(i);
        int x = size.width;
        int y = size.height;

        int diff = Math.abs(x - preferX) + Math.abs(y - preferY);
        if (diff < minDiff) {
            minDiff = diff;/*from ww  w.ja  va 2s  .c  o  m*/
            index = i;
        }
    }

    Camera.Size size = allSupportSizes.get(index);
    return size;
}

From source file:Main.java

public static Bitmap multiply(Bitmap bitmap1, Bitmap bitmap2) {
    double alpha = 0.1;
    int width = bitmap1.getWidth();
    int height = bitmap1.getHeight();
    Bitmap grayBitmap1 = toGrayScale(bitmap1);
    Bitmap grayBitmap2 = toGrayScale(bitmap2);
    Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    for (int x = 0; x < width; x++)
        for (int y = 0; y < height; y++) {
            int value1 = Color.blue(grayBitmap1.getPixel(x, y));
            int value2 = Color.blue(grayBitmap2.getPixel(x, y));
            int resultValue = (int) (Math.abs((value2 / 255.0) * (value1 / 255.0)) * 255);
            //                resultValue = (resultValue>255/2)?255:0;
            result.setPixel(x, y, Color.rgb(resultValue, resultValue, resultValue));
        }/* w w  w.  jav  a  2  s. co m*/
    //        grayBitmap1.recycle();
    //        grayBitmap2.recycle();
    return result;
}

From source file:Main.java

public static double[] findClosePointsForDrawingArc(int x1, int x2, int y1, int y2) {
    double[] xy = new double[2];
    double d = 5;
    double angle = 0;
    //        if ((x1 - x2) != 0) {
    // tan a = y2-y1/x2-x1
    //        System.out.println(" x1  = " + x1 + " y1  = " + y1 + " |  x2  = " + x2 + " |  y2  = " + y2);
    //        angle = Math.toDegrees(Math.atan2(Math.abs(y1 - y2), Math.abs(x1 - x2)));
    //        xy[0] = (Math.cos(angle) * d) + x2;
    //        xy[1] = (Math.sin(angle) * d) + y2;
    //        System.out.println("xt " + xy[0] + "  yt " + xy[1] + " angel would be : " + angle + " and Different number : " + d);

    angle = Math.toDegrees(Math.atan2(Math.abs(y1 - y2), Math.abs(x1 - x2)));
    System.out.println("----------------------------------vvvvv----------------------------------------");
    System.out.println(" ");
    System.out.println("x1 " + x1 + "  y1 " + y1 + " |  x2 " + x2 + " y2 " + y2);
    System.out.println("Math.abs(x1-x2)" + Math.abs(x1 - x2) + "  |  Math.abs(y1-y2) " + Math.abs(y1 - y2)
            + "  |   angel would be Math.toDegrees(Math.atan2(Math.abs(y1 - y2), Math.abs(x1 - x2))) : " + angle
            + " |   Different number : " + d);
    if (x1 > x2) {
        if (y1 > y2) {
            System.out.println(/*w  w  w.jav a2 s .  c  om*/
                    "x1>x2 && y1>y2 | x2= " + x2 + " | cos(angle) = " + Math.cos(angle) + " | d = " + d);
            xy[0] = x2 + (Math.abs(Math.cos(angle)) * d);
            System.out.println(
                    "x1>x2 && y1>y2 | x2= " + x2 + " | sin(angle) = " + Math.sin(angle) + " | d = " + d);
            xy[1] = y2 + (Math.abs(Math.sin(angle)) * d);
        } else if (y1 < y2) {
            System.out.println(
                    "x1>x2 && y1< y2 | x2= " + x2 + " | cos(angle) = " + Math.cos(angle) + " | d = " + d);
            xy[0] = x2 + (Math.abs(Math.cos(angle)) * d);
            System.out.println(
                    "x1>x2 && y1< y2 | x2= " + x2 + " | sin(angle) = " + Math.sin(angle) + " | d = " + d);
            xy[1] = y2 - (Math.abs(Math.sin(angle)) * d);
        } else {
            System.out.println(
                    "x1>x2 && y1 = y2 | x2= " + x2 + " | cos(angle) = " + Math.cos(angle) + " | d = " + d);
            xy[0] = x2 + (Math.abs(Math.cos(angle)) * d);
            System.out.println(
                    "x1>x2 && y1 = y2 | x2= " + x2 + " | sin(angle) = " + Math.sin(angle) + " | d = " + d);
            xy[1] = y2;
        }

    } else if (x1 < x2) {
        if (y1 > y2) {
            System.out.println(
                    "x1<x2 && y1>y2 | x2= " + x2 + " | cos(angle) = " + Math.cos(angle) + " | d = " + d);
            xy[0] = x2 - (Math.abs(Math.cos(angle)) * d);
            System.out.println(
                    "x1<x2 && y1>y2 | x2= " + x2 + " | sin(angle) = " + Math.sin(angle) + " | d = " + d);
            xy[1] = y2 + (Math.abs(Math.sin(angle)) * d);
        } else if (y1 < y2) {
            System.out.println(
                    "x1<x2 && y1< y2 | x2= " + x2 + " | cos(angle) = " + Math.cos(angle) + " | d = " + d);
            xy[0] = x2 - (Math.abs(Math.cos(angle)) * d);
            System.out.println(
                    "x1<x2 && y1< y2 | x2= " + x2 + " | sin(angle) = " + Math.sin(angle) + " | d = " + d);
            xy[1] = y2 - (Math.abs(Math.sin(angle)) * d);
        } else {
            System.out.println(
                    "x1<x2 && y1 =y2 | x2= " + x2 + " | cos(angle) = " + Math.cos(angle) + " | d = " + d);
            xy[0] = x2 - (Math.abs(Math.cos(angle)) * d);
            System.out.println(
                    "x1<x2 && y1 =y2 | x2= " + x2 + " | sin(angle) = " + Math.sin(angle) + " | d = " + d);
            xy[1] = y2;
        }
    } else {
        if (y1 > y2) {
            System.out.println(
                    "x1=x2 && y1>y2 | x2= " + x2 + " | cos(angle) = " + Math.cos(angle) + " | d = " + d);
            xy[0] = x2;
            System.out.println(
                    "x1=x2 && y1>y2 | x2= " + x2 + " | sin(angle) = " + Math.sin(angle) + " | d = " + d);
            xy[1] = y2 + (Math.abs(Math.sin(angle) * d));
        } else if (y1 < y2) {
            System.out.println(
                    "x1=x2 && y1< y2 | x2= " + x2 + " | cos(angle) = " + Math.cos(angle) + " | d = " + d);
            xy[0] = x2;
            System.out.println(
                    "x1=x2 && y1< y2 | x2= " + x2 + " | sin(angle) = " + Math.sin(angle) + " | d = " + d);
            xy[1] = y2 - (Math.abs(Math.sin(angle)) * d);
        } else {
            System.out.println(
                    "x1=x2 && y1 =y2 | x2= " + x2 + " | cos(angle) = " + Math.cos(angle) + " | d = " + d);
            xy[0] = x2;
            System.out.println(
                    "x1=x2 && y1 =y2 | x2= " + x2 + " | sin(angle) = " + Math.sin(angle) + " | d = " + d);
            xy[1] = y2;
        }
    }
    System.out.println(" X1 = " + x1 + " | X2 = " + x2 + " | Y1 = " + y1 + " | Y2 = " + y2 + " | X target = "
            + xy[0] + " | Y target = " + xy[1]);
    System.out.println(" ");
    System.out.println("--------------------------------^^^^----------------------------------------");
    //        } else {
    //            if (y2 > y1) {
    //                System.out.println("(x1 - x2) == 0 | X1 = " + x1 + " | X2 = " + x2 + " | Y1 = " + y1 + " | Y2 = " + y2 +   " | X target = " + x1 + " | Y target = " + (y2 + ((y2 - y1) / 12)));
    //                xy[0] = (x2);
    //                xy[1] = y2 + ((y2 - y1) / 12);
    //            } else {
    //                System.out.println("(x1 - x2) == 0 | X1 = " + x1 + " | X2 = " + x2 + " | Y1 = " + y1 + " | Y2 = " + y2  + " | X target = " + x1 + " | Y target = " + (y2 - ((y2 - y1) / 12)));
    //                xy[0] = (x2);
    //                xy[1] = y2 - ((y2 - y1 )/ 12);
    //            }
    //        }
    return xy;
}

From source file:Main.java

public static Camera.Size getOptimalPreviewSize(int displayOrientation, int width, int height,
        Camera.Parameters parameters) {//from w  w w.jav a  2s  .co m
    double targetRatio = (double) width / height;
    List<Camera.Size> sizes = parameters.getSupportedPreviewSizes();
    Camera.Size optimalSize = null;
    double minDiff = Double.MAX_VALUE;
    int targetHeight = height;

    if (displayOrientation == 90 || displayOrientation == 270) {
        targetRatio = (double) height / width;
    }

    // Try to find an size match aspect ratio and size

    for (Camera.Size size : sizes) {
        double ratio = (double) size.width / size.height;

        if (Math.abs(ratio - targetRatio) <= ASPECT_TOLERANCE) {
            if (Math.abs(size.height - targetHeight) < minDiff) {
                optimalSize = size;
                minDiff = Math.abs(size.height - targetHeight);
            }
        }
    }

    // Cannot find the one match the aspect ratio, ignore
    // the requirement

    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 void appendGmtOffset(StringBuilder sb, final int gmtOffset) {
    sb.append("GMT");

    if (gmtOffset < 0) {
        sb.append('-');
    } else {/*from  ww  w.  j a  va  2  s  .co m*/
        sb.append('+');
    }

    final int p = Math.abs(gmtOffset);
    sb.append(p / DateUtils.HOUR_IN_MILLIS); // Hour

    final int min = (p / (int) DateUtils.MINUTE_IN_MILLIS) % 60;
    if (min != 0) { // Show minutes if non-zero
        sb.append(':');
        if (min < 10) {
            sb.append('0');
        }
        sb.append(min);
    }
}

From source file:Main.java

public static boolean approxEquals(double a, double b, double precision) {
    return (Math.abs(a - b) < precision);
}

From source file:Util.java

public static boolean almostEquals(double d1, double d2, double epsilon) {
    return Math.abs(d1 - d2) < epsilon;
}

From source file:Main.java

/**
 * Calculates the number of quarters between two given dates
 * /*from  w  w  w . j  av  a 2  s.c  o m*/
 * @return Number of quarters
 * @param date1
 *          First given date cannot be null
 * @param date2
 *          Second given date cannot be null
 */
public static int getQuartersBetweenDates(LocalDate date1, LocalDate date2) {
    LocalDate beginn = null;
    LocalDate end = null;
    if (date1.isBefore(date2)) {
        beginn = date1;
        end = date2;
    } else {
        beginn = date2;
        end = date1;
    }
    int quarters = getQuarter(end) - getQuarter(beginn);
    int years = end.get(ChronoField.YEAR) - beginn.get(ChronoField.YEAR);
    quarters += years * 4;
    return Math.abs(quarters);
}

From source file:Main.java

public static float vectorMagnitudeSquare(float[] vector) {
    float accum = 0;

    for (int i = 0; i < vector.length; i++) {
        accum += Math.pow(vector[i], 2);
    }//w  w  w . j a v a  2 s.  c o  m

    return Math.abs(accum);
}

From source file:bwem.util.Utils.java

public static int queenWiseNorm(final int dx, final int dy) {
    return Math.max(Math.abs(dx), Math.abs(dy));
}