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

/**
 * This method starts as two and then marks all the multiples of two as
 * false, and then continues and does the same thing for three, five etc.
 * /*from  w ww  .  j a  v a2  s  .c  o m*/
 * @param integers
 *            An boolean array of numbers.
 * @return An boolean array with prime numbers marked as true and their
 *         multiples as false.
 */
private static Boolean[] removeMultiples(Boolean[] integers) {
    Boolean[] prime = integers;

    int end = (int) Math.sqrt(prime.length);

    for (int i = 2; i <= end; i++) {
        for (int j = i + i; j < prime.length; j += i) {
            if (j % i == 0) {
                prime[j] = false;
            }
        }
    }

    return prime;
}

From source file:bwem.util.Utils.java

public static double norm(final int dx, final int dy) {
    return Math.sqrt(squaredNorm(dx, dy));
}

From source file:Main.java

/**
 * Function: arcToCurves/*from   w w  w  .j a  v  a2  s.  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:Data.Utilities.java

public static Double haversine(double lat1, double lon1, double lat2, double lon2) {
    double dLat = Math.toRadians(lat2 - lat1);
    double dLon = Math.toRadians(lon2 - lon1);
    lat1 = Math.toRadians(lat1);//from   w w  w. jav a 2 s.c o  m
    lat2 = Math.toRadians(lat2);

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

From source file:Main.java

static void drawArrow(Graphics g1, int x1, int y1, int x2, int y2) {
    // x1 and y1 are coordinates of circle or rectangle
    // x2 and y2 are coordinates of circle or rectangle, to this point is directed the arrow
    Graphics2D g = (Graphics2D) g1.create();
    double dx = x2 - x1;
    double dy = y2 - y1;
    double angle = Math.atan2(dy, dx);
    int len = (int) Math.sqrt(dx * dx + dy * dy);
    AffineTransform t = AffineTransform.getTranslateInstance(x1, y1);
    t.concatenate(AffineTransform.getRotateInstance(angle));
    g.transform(t);/*from  ww  w .j  a va2 s.  c  om*/
    g.drawLine(0, 0, len, 0);
    int basePosition = len - ARROW_HEAD_SIZE.width;
    int height = ARROW_HEAD_SIZE.height;
    g.fillPolygon(new int[] { len, basePosition, basePosition, len }, new int[] { 0, -height, height, 0 }, 4);
}

From source file:Main.java

/**
 * Returns variance among values in array
 * @param array   A {@code double} array 
 * @return returns variance among elements of {@code array}
 *///from   w  w  w .  j a v a  2s.c  o m
public static double variance(double[] array) {
    double sum = 0, mean = 0, var = 0;
    for (double d : array)
        sum += d;
    mean = sum / array.length;
    double temp = 0;
    for (double d : array)
        temp += (mean - d) * (mean - d);
    var = temp / array.length;
    return Math.sqrt(var);
}

From source file:Main.java

public static float distance(float x1, float y1, float x2, float y2) {
    float x = x1 - x2;
    float y = y1 - y2;
    return (float) Math.sqrt(x * x + y * y);
}

From source file:Main.java

/**
 * This method calculates maximum size of both width and height of bitmap.
 * It is twice the device screen diagonal for default implementation (extra quality to zoom image).
 * Size cannot exceed max texture size./*ww  w.j  a  va  2  s  .c om*/
 *
 * @return - max bitmap size in pixels.
 */
@SuppressWarnings({ "SuspiciousNameCombination", "deprecation" })
public static int calculateMaxBitmapSize(@NonNull Context context) {
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();

    Point size = new Point();
    int width, height;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
        display.getSize(size);
        width = size.x;
        height = size.y;
    } else {
        width = display.getWidth();
        height = display.getHeight();
    }

    int screenDiagonal = (int) Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2));

    Canvas canvas = new Canvas();
    return Math.min(screenDiagonal * 2,
            Math.min(canvas.getMaximumBitmapWidth(), canvas.getMaximumBitmapHeight()));
}

From source file:Main.java

public static double[] wgs84togcj02(double lng, double lat) {
    if (out_of_china(lng, lat)) {
        return new double[] { lng, lat };
    }//from  w ww  . j  a va2s  .c  o m
    double dlat = transformlat(lng - 105.0, lat - 35.0);
    double dlng = transformlng(lng - 105.0, lat - 35.0);
    double radlat = lat / 180.0 * pi;
    double magic = Math.sin(radlat);
    magic = 1 - ee * magic * magic;
    double sqrtmagic = Math.sqrt(magic);
    dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * pi);
    dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * pi);
    double mglat = lat + dlat;
    double mglng = lng + dlng;
    return new double[] { mglng, mglat };
}

From source file:com.opengamma.analytics.math.TrigonometricFunctionUtils.java

public static double acosh(final double x) {
    final double y = x * x - 1;
    Validate.isTrue(y >= 0, "|x|>=1.0 for real solution");
    return Math.log(x + Math.sqrt(x * x - 1));
}