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

/**
 * Calculate the azimuth to the target location from local.
 *///  w  w w.  j  av a 2  s .c  o  m
private static int getPosDirection(final double startlat, final double startlong, final double endlat,
        final double endlon) {
    double slat = Math.toRadians(startlat);
    double elat = Math.toRadians(endlat);
    double slng = Math.toRadians(startlong);
    double elng = Math.toRadians(endlon);
    double Y = Math.sin(elng - slng) * Math.cos(elat);
    double X = Math.cos(slat) * Math.sin(elat) - Math.sin(slat) * Math.cos(elat) * Math.cos(elng - slng);
    double deg = Math.toDegrees(Math.atan2(Y, X));
    double angle = (deg + 360) % 360;
    return (int) (Math.abs(angle) + (1 / 7200));
}

From source file:Main.java

/**
 * Calculates the position around a center point, depending on the distance
 * from the center, and the angle of the position around the center.
 *
 * @param center//from  www  .j  av a  2s . co m
 * @param dist
 * @param angle  in degrees, converted to radians internally
 * @return
 */
public static PointF getPosition(PointF center, float dist, float angle) {

    PointF p = new PointF((float) (center.x + dist * Math.cos(Math.toRadians(angle))),
            (float) (center.y + dist * Math.sin(Math.toRadians(angle))));
    return p;
}

From source file:Main.java

/**
 * http://snippets.dzone.com/posts/show/10687
 * @param lat1/*from  w  ww. ja v  a  2 s. c  om*/
 * @param lon1
 * @param lat2
 * @param lon2
 * @param unit   "M":miles, "K":kilometers, "N":Nautical Miles
 * @return
 */
public static double distance(double lat1, double lon1, double lat2, double lon2, String unit) {
    double theta = lon1 - lon2;
    double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2))
            + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
    dist = Math.acos(dist);
    dist = rad2deg(dist);
    dist = dist * 60 * 1.1515;
    if (unit == "K") {
        dist = dist * 1.609344;
    } else if (unit == "N") {
        dist = dist * 0.8684;
    }
    return (dist);
}

From source file:Main.java

public static double gps2m(double lat_a, double lng_a, double lat_b, double lng_b) {
    double radLat1 = (lat_a * Math.PI / 180.0);
    double radLat2 = (lat_b * Math.PI / 180.0);
    double a = radLat1 - radLat2;
    double b = (lng_a - lng_b) * Math.PI / 180.0;
    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)));
    s = s * EARTH_RADIUS;/*from www . j a v a  2s .co m*/
    s = Math.round(s * 10000) / 10000;
    return s;
}

From source file:Main.java

/**
 * Calculates the distance between two locations in KM
 *//*from  w  w w  . j a  v a2 s . c om*/
public static double checkDistance(double lat1, double lng1, double lat2, double lng2) {
    double earthRadius = 6371;

    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 * 1000;

    return dist; // in meter
}

From source file:Main.java

public static double distanceFrom(double lat1, double lng1, double lat2, double lng2) {
    // Implementation of the Haversine distance formula
    lat1 = Math.toRadians(lat1);//from  ww w. ja  va2 s.co  m
    lng1 = Math.toRadians(lng1);
    lat2 = Math.toRadians(lat2);
    lng2 = Math.toRadians(lng2);

    double dlon = lng2 - lng1;
    double dlat = lat2 - lat1;

    double a = Math.pow((Math.sin(dlat / 2)), 2)
            + Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(dlon / 2), 2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    return 3958.75 * c; // 3958: Earth radius in miles
}

From source file:Main.java

/**
 * Receives the Euler angles in radians and returns the components of the quaternion.
 * // w  w  w.  j a  va 2 s.c o m
 * @param x The x or phi axis in radians.
 * @param y The y axis or theta in radians.
 * @param z The z axis or psi in radians
 * @return A 4 positions array which contains the x, y, z and w in this order.
 */
public static final float[] eulerToQuaternion(float x, float y, float z) {
    float[] quaternion = new float[4];

    quaternion[0] = (float) ((Math.cos(x / 2) * Math.cos(y / 2) * Math.cos(z / 2))
            + (Math.sin(x / 2) * Math.sin(y / 2) * Math.sin(z / 2)));
    quaternion[1] = (float) ((Math.sin(x / 2) * Math.cos(y / 2) * Math.cos(z / 2))
            - (Math.cos(x / 2) * Math.sin(y / 2) * Math.sin(z / 2)));
    quaternion[2] = (float) ((Math.cos(x / 2) * Math.sin(y / 2) * Math.cos(z / 2))
            + (Math.sin(x / 2) * Math.cos(y / 2) * Math.sin(z / 2)));
    quaternion[3] = (float) ((Math.cos(x / 2) * Math.cos(y / 2) * Math.sin(z / 2))
            - (Math.sin(x / 2) * Math.sin(y / 2) * Math.cos(z / 2)));

    return quaternion;
}

From source file:Main.java

public static double distanceTo(double fromLat, double fromLng, double toLat, double toLng) {

    double fromLatRad = calculateRadian(fromLat);
    double fromLngRad = calculateRadian(fromLng);
    double toLatRad = calculateRadian(toLat);
    double toLngRad = calculateRadian(toLng);

    return EARTH_RADIUS * (Math.PI / 2 - Math.asin(Math.sin(fromLatRad) * Math.sin(toLatRad)
            + Math.cos(fromLngRad - toLngRad) * Math.cos(toLatRad) * Math.cos(fromLatRad)));
}

From source file:Main.java

/**
 * Rotate a set of coordinates by a given angle
 * @param c_x/*from  w w w  .  j  a  v a 2 s  . com*/
 * @param c_y
 * @param thetab
 * @param x
 * @param y
 * @return
 */
public static double[] rotateCoord(double c_x, double c_y, double thetab, double x, double y) {
    double prc_x = x - c_x;
    double prc_y = y - c_y;
    double r = Math.sqrt(prc_x * prc_x + prc_y * prc_y);
    double theta = Math.atan2(prc_y, prc_x);
    theta = theta + thetab * Math.PI / 180.0;
    double pc_x = r * Math.cos(theta);
    double pc_y = r * Math.sin(theta);
    double p[] = new double[2];
    p[0] = pc_x + c_x;
    p[1] = pc_y + c_y;
    return p;
}

From source file:Main.java

/**
 * From http://stackoverflow.com/a/19498994/423980
 * @return  distance between 2 points, stored as 2 pair location;
 *///from   www .  j  a v  a2s .c o  m
public static double distanceFrom(double lat1, double lng1, double lat2, double lng2) {
    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 = EARTH_RADIOUS * c;
    return Double.valueOf(dist * METER_CONVERSION).floatValue();
}