Example usage for java.lang Math cos

List of usage examples for java.lang Math cos

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public static double cos(double a) 

Source Link

Document

Returns the trigonometric cosine of an angle.

Usage

From source file:Main.java

public static void untransformBaidu(double wgLat, double wgLon, double[] latlng) {
    double x = wgLon - 0.0065, y = wgLat - 0.006;
    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);
    double gg_lon = z * Math.cos(theta);
    double gg_lat = z * Math.sin(theta);
    untransform(gg_lat, gg_lon, latlng);
}

From source file:Main.java

public static double bearingBetween(Location startLocation, Location endLocation) {
    double lat1 = DegreesToRadians(startLocation.getLatitude());
    double lon1 = DegreesToRadians(startLocation.getLongitude());

    double lat2 = DegreesToRadians(endLocation.getLatitude());
    double lon2 = DegreesToRadians(endLocation.getLongitude());

    double dLon = lon2 - lon1;

    double y = Math.sin(dLon) * Math.cos(lat2);
    double x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(dLon);
    double radiansBearing = Math.atan2(y, x);

    return RadiansToDegrees(radiansBearing);
}

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 a  va2 s .  co m*/
    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 gps2km(final double lat_a, final double lng_a, final double lat_b, final double lng_b) {
    if (!isGpsValid(lng_a, lat_a) || !isGpsValid(lng_b, lat_b)) {
        return -1;
    }/*from  w  ww .  java2  s  .  co  m*/
    final double radLat1 = (lat_a * Math.PI / 180.0);
    final double radLat2 = (lat_b * Math.PI / 180.0);
    final double a = radLat1 - radLat2;
    final double b = (lng_a - lng_b) * Math.PI / 180.0;
    final double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
            + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
    return s * EARTH_RADIUS;
}

From source file:Main.java

/**
 * Returns the bearing from one point to another.
 * @param latFrom The latitude of the point from
 * @param lonFrom The longitude of the point from
 * @param latTo The latitude of the point to
 * @param lonTo The longitude of the point to
 * @return the bearing from one point to another
 *///from w  w w  . j  a  v a2  s.  c  o m
private static double bearingTo(double latFrom, double lonFrom, double latTo, double lonTo) {
    double latitude1 = Math.toRadians(latFrom);
    double latitude2 = Math.toRadians(latTo);
    double longDiff = Math.toRadians(lonTo - lonFrom);
    double y = Math.sin(longDiff) * Math.cos(latitude2);
    double x = Math.cos(latitude1) * Math.sin(latitude2)
            - Math.sin(latitude1) * Math.cos(latitude2) * Math.cos(longDiff);

    return (Math.toDegrees(Math.atan2(y, x)) + 360) % 360;
}

From source file:Main.java

/**
 * Will return a list of random location from the given position & radius
 * @param amount/*from  w  w  w.j  av a  2s  . c  om*/
 * @param lat
 * @param lng
 * @param radius
 * @return ArrayList<Location> 
 */
public static ArrayList<Location> randomLocations(int amount, double lat, double lng, double radius) {
    ArrayList<Location> locations = new ArrayList<Location>(0);
    Location centerLocation = new Location("local");
    centerLocation.setLatitude(lat);
    centerLocation.setLongitude(lng);

    final double lngScale = Math.cos((Math.PI / 180.0) * lat);
    final double radiusDeg = radius / 111.2; // radius converted to degrees (square)

    while (locations.size() < amount) {
        Location l = new Location("local");
        double dLat = (Math.random() * radiusDeg * 2) - radiusDeg;
        double dLng = (Math.random() * radiusDeg * 2 / lngScale) - (radiusDeg / lngScale);

        l.setLatitude(centerLocation.getLatitude() + dLat);
        l.setLongitude(centerLocation.getLongitude() + dLng);
        double dist = l.distanceTo(centerLocation) / 1000.0;

        if (dist < (radius)) {
            locations.add(l);
        }
    }
    return locations;
}

From source file:Main.java

static float computeCircleX(float r, float degrees) {
    return (float) (r * Math.cos(Math.toRadians(degrees)));
}

From source file:Main.java

/**
 * Metoda care calculeaza viteza cu care se deplaseaza intre 2 coordonate
 * date(Lat+long) intr-un interval de timp [time_1,time_2]. Calculam
 * distanta dintre cele doua coordonate folosind formula " Great-circle
 * distance" viteza = distanta transformata in m/diferenta dintre cei 2
 * timpi primiti ca parametru/*www  . ja v a2  s.  co  m*/
 */
public static double computeSpeed(double lat_1, double long_1, long time_1, double lat_2, double long_2,
        long time_2) {
    double speed; // speed->m/s
    double distanceKm;
    double distanceM;
    long time;
    double r = 6378.137;

    double e = (double) Math.acos(
            Math.sin(lat_1) * Math.sin(lat_2) + Math.cos(lat_1) * Math.cos(lat_2) * Math.cos(long_2 - long_1));
    e = e / 180 * (double) Math.PI;
    distanceKm = e * r;
    distanceM = distanceKm * 1000;
    time = (time_2 - time_1) / 1000;
    speed = distanceM / time;

    return speed;
}

From source file:Main.java

private static void findConstants(double[] n_p, double[] n_m, double[] d_p, double[] d_m, double[] bd_p,
        double[] bd_m, double std_dev) {
    double div = Math.sqrt(2 * 3.141593) * std_dev;
    double x0 = -1.783 / std_dev;
    double x1 = -1.723 / std_dev;
    double x2 = 0.6318 / std_dev;
    double x3 = 1.997 / std_dev;
    double x4 = 1.6803 / div;
    double x5 = 3.735 / div;
    double x6 = -0.6803 / div;
    double x7 = -0.2598 / div;
    int i;//from   w  w  w  .ja v  a2s.c  o m

    n_p[0] = x4 + x6;
    n_p[1] = (Math.exp(x1) * (x7 * Math.sin(x3) - (x6 + 2 * x4) * Math.cos(x3))
            + Math.exp(x0) * (x5 * Math.sin(x2) - (2 * x6 + x4) * Math.cos(x2)));
    n_p[2] = (2 * Math.exp(x0 + x1) * ((x4 + x6) * Math.cos(x3) * Math.cos(x2)
            - x5 * Math.cos(x3) * Math.sin(x2) - x7 * Math.cos(x2) * Math.sin(x3)) + x6 * Math.exp(2 * x0)
            + x4 * Math.exp(2 * x1));
    n_p[3] = (Math.exp(x1 + 2 * x0) * (x7 * Math.sin(x3) - x6 * Math.cos(x3))
            + Math.exp(x0 + 2 * x1) * (x5 * Math.sin(x2) - x4 * Math.cos(x2)));
    n_p[4] = 0.0;

    d_p[0] = 0.0;
    d_p[1] = -2 * Math.exp(x1) * Math.cos(x3) - 2 * Math.exp(x0) * Math.cos(x2);
    d_p[2] = 4 * Math.cos(x3) * Math.cos(x2) * Math.exp(x0 + x1) + Math.exp(2 * x1) + Math.exp(2 * x0);
    d_p[3] = -2 * Math.cos(x2) * Math.exp(x0 + 2 * x1) - 2 * Math.cos(x3) * Math.exp(x1 + 2 * x0);
    d_p[4] = Math.exp(2 * x0 + 2 * x1);

    for (i = 0; i <= 4; i++) {
        d_m[i] = d_p[i];
    }

    n_m[0] = 0.0;
    for (i = 1; i <= 4; i++) {
        n_m[i] = n_p[i] - d_p[i] * n_p[0];
    }

    double sum_n_p, sum_n_m, sum_d;
    double a, b;

    sum_n_p = 0.0;
    sum_n_m = 0.0;
    sum_d = 0.0;

    for (i = 0; i <= 4; i++) {
        sum_n_p += n_p[i];
        sum_n_m += n_m[i];
        sum_d += d_p[i];
    }

    a = sum_n_p / (1.0 + sum_d);
    b = sum_n_m / (1.0 + sum_d);

    for (i = 0; i <= 4; i++) {
        bd_p[i] = d_p[i] * a;
        bd_m[i] = d_m[i] * b;
    }
}

From source file:Main.java

/**
 * Function: arcToCurves//from  w w w .  j  av  a  2s  . 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;
}