Example usage for java.lang Math atan2

List of usage examples for java.lang Math atan2

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public static double atan2(double y, double x) 

Source Link

Document

Returns the angle theta from the conversion of rectangular coordinates ( x ,  y ) to polar coordinates (r, theta).

Usage

From source file:Main.java

/**
 * Receives a quaternion and returns the same in Euler angles.
 * //  www.  j  a va 2  s  .  c  om
 * @param q0 First component of the quaternion, the independent term.
 * @param q1 Second component of the quaternion, the i term.
 * @param q2 Third component of the quaternion, the j term.
 * @param q3 Forth component of the quaternion, th k term.
 * @return A 3 position array which contains the x, y and z in this order.
 */
public static final float[] quatenionToEuler(float q0, float q1, float q2, float q3) {
    float[] euler = new float[3];

    euler[0] = (float) Math.atan2(2 * (q0 * q1 + q2 * q3), 1 - 2 * (q1 * q1 + q2 * q2));
    euler[1] = (float) Math.asin(2 * (q0 * q2 - q3 * q1));
    euler[2] = (float) Math.atan2(2 * (q0 * q3 + q1 * q2), 1 - 2 * (q2 * q2 + q3 * q3));

    return euler;
}

From source file:Main.java

/**
 * Calculate the azimuth to the target location from local.
 *//*from w ww .ja va2s  . 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

public static float atan2(float a, float b) {
    return (float) Math.atan2(a, b);
}

From source file:Main.java

/**
 * From http://stackoverflow.com/a/19498994/423980
 * @return  distance between 2 points, stored as 2 pair location;
 *//*from ww  w . j  a v a  2 s. co  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();
}

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

private static double extractAngle(AffineTransform at) {
    Point2D p0 = new Point();
    Point2D p1 = new Point(1, 0);
    Point2D pp0 = at.transform(p0, null);
    Point2D pp1 = at.transform(p1, null);
    double dx = pp1.getX() - pp0.getX();
    double dy = pp1.getY() - pp0.getY();
    double angle = Math.atan2(dy, dx);
    return angle;
}

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
 *///ww w. j  a va2s.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

public static float angle(float x1, float y1, float x2, float y2) {
    return (float) Math.atan2(y2 - y1, x2 - x1);
}

From source file:Main.java

public static double[] gcj02tobd09(double lng, double lat) {
    double z = Math.sqrt(lng * lng + lat * lat) + 0.00002 * Math.sin(lat * x_pi);
    double theta = Math.atan2(lat, lng) + 0.000003 * Math.cos(lng * x_pi);
    double bd_lng = z * Math.cos(theta) + 0.0065;
    double bd_lat = z * Math.sin(theta) + 0.006;
    return new double[] { bd_lng, bd_lat };
}

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(/*from  ww  w .  j a  v  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;
}