Example usage for java.lang Math sqrt

List of usage examples for java.lang Math sqrt

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public static double sqrt(double a) 

Source Link

Document

Returns the correctly rounded positive square root of a double value.

Usage

From source file:Main.java

public static double distFrom(double lat1, double lng1, double lat2, double lng2) {
    double earthRadius = 3958.75;
    double dLat = Math.toRadians(lat2 - lat1);
    double dLng = Math.toRadians(lng2 - lng1);
    double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(Math.toRadians(lat1))
            * Math.cos(Math.toRadians(lat2)) * Math.sin(dLng / 2) * Math.sin(dLng / 2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    double dist = earthRadius * c;

    int meterConversion = 1609;

    return dist * meterConversion;
}

From source file:Main.java

public static double distance(double lat1, double lng1, double lat2, double lng2) {
    double earthRadius = 6371000; //meters
    double dLat = Math.toRadians(lat2 - lat1);
    double dLng = Math.toRadians(lng2 - lng1);
    double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(Math.toRadians(lat1))
            * Math.cos(Math.toRadians(lat2)) * Math.sin(dLng / 2) * Math.sin(dLng / 2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    double distance = (double) (earthRadius * c);

    return distance;
}

From source file:Main.java

public static double getRMSAverage(byte[] audioData) {
    int sum = 0;//  w w w . ja v  a 2s  . co m
    // look at every other value (odd indexed value)
    for (int i = 1; i < audioData.length; i += 2) {
        // 1. square each sample, sum the squared sample
        sum += audioData[i] * audioData[i];
    }
    // 2. divide the sum by the number of samples
    // 3. take the square root of step 2 (ROOT-MEAN-SQUARED RMS value)
    return Math.sqrt(sum / (double) (audioData.length / 2));
}

From source file:Main.java

public static double pointDistance(PointF a, PointF b) {
    return Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2));
}

From source file:Main.java

/**
 * Get the display size in inches./*from  w w w  .  jav a2 s.c  o m*/
 * @return the value in inches
 */
public static double getDisplaySizeInches(Context context) {
    DisplayMetrics dm = context.getResources().getDisplayMetrics();

    double x = Math.pow((double) dm.widthPixels / (double) dm.densityDpi, 2);
    double y = Math.pow((double) dm.heightPixels / (double) dm.densityDpi, 2);

    return Math.sqrt(x + y);
}

From source file:Main.java

public static void transformBaidu(double wgLat, double wgLon, double[] latlng) {
    double d[] = new double[2];
    transform(wgLat, wgLon, d);/*from   w w w  .j av a  2s . c om*/
    double x = d[1], y = d[0];
    double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
    double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
    latlng[1] = z * Math.cos(theta) + 0.0065;
    latlng[0] = z * Math.sin(theta) + 0.006;
}

From source file:Main.java

public static double haversine(double lat1, double lon1, double lat2, double lon2) {
    final double R = 6372.8; // In kilometers

    double dLat = Math.toRadians(lat2 - lat1);
    double dLon = Math.toRadians(lon2 - lon1);
    lat1 = Math.toRadians(lat1);// www .jav  a2  s.  co  m
    lat2 = Math.toRadians(lat2);

    double a = Math.pow(Math.sin(dLat / 2), 2)
            + Math.pow(Math.sin(dLon / 2), 2) * Math.cos(lat1) * Math.cos(lat2);
    double c = 2 * Math.asin(Math.sqrt(a));
    return R * c;
}

From source file:Main.java

/**
 * Gets a float array of two lengths representing a rectangles width and height
 * The order of the corners in the input float array is:
 * 0------->1/*from   w  ww . jav a  2s  .c o  m*/
 * ^        |
 * |        |
 * |        v
 * 3<-------2
 *
 * @param corners the float array of corners (8 floats)
 * @return the float array of width and height (2 floats)
 */
public static float[] getRectSidesFromCorners(float[] corners) {
    return new float[] {
            (float) Math.sqrt(Math.pow(corners[0] - corners[2], 2) + Math.pow(corners[1] - corners[3], 2)),
            (float) Math.sqrt(Math.pow(corners[2] - corners[4], 2) + Math.pow(corners[3] - corners[5], 2)) };
}

From source file:Main.java

public static double getScreenSize(Activity activity) {
    if (dm == null) {
        dm = new DisplayMetrics();
    }//  ww w  .j  a  v  a  2s .c o  m
    activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
    double x = Math.pow(dm.widthPixels / dm.densityDpi, 2);
    double y = Math.pow(dm.heightPixels / dm.densityDpi, 2);
    return Math.sqrt(x + y);
}

From source file:Main.java

public static double distFrom(double lat1, double lng1, double lat2, double lng2) {
    double earthRadius = 3958.75;
    double dLat = Math.toRadians(lat2 - lat1);
    double dLng = Math.toRadians(lng2 - lng1);
    double sindLat = Math.sin(dLat / 2);
    double sindLng = Math.sin(dLng / 2);
    double a = Math.pow(sindLat, 2)
            + Math.pow(sindLng, 2) * Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2));
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    double dist = earthRadius * c;

    if (dist >= 1000) {
        int distInInt = (int) dist;
        dist = (int) distInInt;
    } else if (dist >= 100) {
        dist = (((int) (dist * 10)) / 10.0);
    } else {/*ww  w.j  av a  2 s.  com*/
        dist = (((int) (dist * 100)) / 100.0);
    }
    return dist;
}