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:org.spout.api.math.Vector2.java

/**
 * Returns a Vector2Polar object with the same value as this Vector2
 *
 * @return/* w w  w  . j a  v  a  2s .  c  om*/
 */
public Vector2Polar toVector2Polar() {
    return new Vector2Polar(length(), Math.atan2(y, x));
}

From source file:com.huahcoding.metrojam.BackTrackActivity.java

public double distance(double lat1, double lon1, double lat2, double lon2) {
    double R = 6371; // km
    double dLat = toRad(lat2 - lat1);
    double dLon = toRad(lon2 - lon1);
    lat1 = toRad(lat1);//from  w w  w . j  a  v a2  s .  com
    lat2 = toRad(lat2);

    double a = Math.sin(dLat / 2.0) * Math.sin(dLat / 2.0)
            + Math.sin(dLon / 2.0) * Math.sin(dLon / 2.0) * Math.cos(lat1) * Math.cos(lat2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1.0 - a));
    double d = R * c;
    return d;
}

From source file:org.orekit.frames.CIRF2000Frame.java

/** Update the frame to the given date.
 * <p>The update considers the nutation and precession effects from IERS data.</p>
 * @param date new value of the date// w  w w .  ja va  2 s .  c om
 * @exception OrekitException if the nutation model data embedded in the
 * library cannot be read
 */
protected void updateFrame(final AbsoluteDate date) throws OrekitException {

    //    offset from J2000.0 epoch
    final double t = date.durationFrom(AbsoluteDate.J2000_EPOCH);

    // evaluate pole motion in celestial frame
    setInterpolatedPoleCoordinates(t);

    // set up the bias, precession and nutation rotation
    final double x2Py2 = xCurrent * xCurrent + yCurrent * yCurrent;
    final double zP1 = 1 + Math.sqrt(1 - x2Py2);
    final double r = Math.sqrt(x2Py2);
    final double sPe2 = 0.5 * (sCurrent + Math.atan2(yCurrent, xCurrent));
    final double cos = Math.cos(sPe2);
    final double sin = Math.sin(sPe2);
    final double xPr = xCurrent + r;
    final double xPrCos = xPr * cos;
    final double xPrSin = xPr * sin;
    final double yCos = yCurrent * cos;
    final double ySin = yCurrent * sin;
    final Rotation bpn = new Rotation(zP1 * (xPrCos + ySin), -r * (yCos + xPrSin), r * (xPrCos - ySin),
            zP1 * (yCos - xPrSin), true);

    // set up the transform from parent GCRF
    setTransform(new Transform(bpn, Vector3D.ZERO));

}

From source file:com.buzz.buzzdata.MongoBuzz.java

public double haversine(double lat1, double lng1, double lat2, double lng2) {
    double r = 6371 / 1.6; // average radius of the earth in miles
    double dLat = Math.toRadians(lat2 - lat1);
    double dLon = 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(dLon / 2) * Math.sin(dLon / 2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    double d = r * c;
    return round(d, 1);
}

From source file:com.eyekabob.util.EyekabobHelper.java

/**
 * Gets distance from current location to given lat/lon.
 * @param lat//from w  ww.j a v  a 2s . co  m
 * @param lon
 * @param context
 * @return
 */
public static long getDistance(double lat, double lon, Context context) {
    Location location = EyekabobHelper.getLocation(context);

    if (location == null) {
        return -1;
    }

    double currentLat = location.getLatitude();
    double currentLon = location.getLongitude();
    int R = 3959; // Earth radius in miles.
    double dLat = Math.toRadians(currentLat - lat);
    double dLon = Math.toRadians(currentLon - lon);
    lat = Math.toRadians(lat);
    currentLat = Math.toRadians(currentLat);
    double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
            + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat) * Math.cos(currentLat);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    return Math.round(R * c);
}

From source file:syncleus.dann.data.vector.Vector.java

/**
 * Obtain the angle of a particular dimension.
 *
 * @param dimension The dimension you want the angle of. the first dimension is 1.
 *                  the last is one less than the total number of dimensions.
 * @return returns a value representing the angle between Pi/2 and -Pi/2
 * @since 1.0//from  w w w  . j a va 2s  . co m
 */
public double getAngularComponent(final int dimension) {
    if (dimension <= 0)
        throw new IllegalArgumentException(DIMENSIONS_BELOW_ONE);
    if ((dimension - 1) > this.data.length)
        throw new IllegalArgumentException(
                "dimensions is larger than the dimensionality (minus 1) of this point");

    final double[] currentCoords = this.data.clone();
    double squaredSum = 0.0;
    for (int coordinateIndex = currentCoords.length - 1; coordinateIndex >= dimension; coordinateIndex--)
        squaredSum += Math.pow(currentCoords[coordinateIndex], 2.0);

    if (dimension == (this.getDimension() - 1))
        return Math.atan2(Math.sqrt(squaredSum), currentCoords[dimension - 1]);
    else {
        if (currentCoords[dimension - 1] == 0.0d)
            return Math.PI / 2.0d;

        return Math.atan(Math.sqrt(squaredSum) / currentCoords[dimension - 1]);
    }
}

From source file:uk.ac.susx.tag.method51.twitter.geocoding.geonames.GeonamesSPARQLLocationDatabase.java

public static double euclideanDistance(double[] p1, double[] p2) {

    double EARTH_RADIUS = 6371;

    double lat1 = p1[0];
    double lon1 = p1[1];

    double lat2 = p2[0];
    double lon2 = p2[1];

    double dLat = Math.toRadians(lat2 - lat1);
    double dLng = Math.toRadians(lon2 - lon1);
    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 x = (lon2 - lon1) * Math.cos((lat1 + lat2) / 2);

    double y = (lat2 - lat1);

    //double dist = Math.sqrt( x*x + y*y ) * EARTH_RADIUS;

    double dist = c * EARTH_RADIUS;
    return dist;//from  w ww. jav a  2 s  .com
}

From source file:com.l2jfree.gameserver.gameobjects.instance.L2BoatInstance.java

/**
 * @param x/*w  w w .j a va  2s  .c  om*/
 * @param y
 * @param z
 */
public void moveToLocation(int x, int y, int z, float speed) {
    final int curX = getX();
    final int curY = getY();

    // Calculate distance (dx,dy) between current position and destination
    final int dx = (x - curX);
    final int dy = (y - curY);
    double distance = Math.sqrt(dx * dx + dy * dy);

    if (_log.isDebugEnabled())
        _log.debug("distance to target:" + distance);

    // Define movement angles needed
    // ^
    // | X (x,y)
    // | /
    // | /distance
    // | /
    // |/ angle
    // X ---------->
    // (curx,cury)

    double cos;
    double sin;
    sin = dy / distance;
    cos = dx / distance;
    // Create and Init a MoveData object
    MoveData m = new MoveData();

    // Calculate the Nb of ticks between the current position and the destination
    int ticksToMove = (int) (GameTimeManager.TICKS_PER_SECOND * distance / speed);

    // Calculate and set the heading of the L2Creature
    int heading = (int) (Math.atan2(-sin, -cos) * 10430.378350470452724949566316381);
    heading += 32768;
    getPosition().setHeading(heading);

    if (_log.isDebugEnabled())
        _log.debug("dist:" + distance + "speed:" + speed + " ttt:" + ticksToMove + " heading:" + heading);

    m._xDestination = x;
    m._yDestination = y;
    m._zDestination = z; // this is what was requested from client
    m._heading = 0; // initial value for coordinate sync
    m.onGeodataPathIndex = -1; // Initialize not on geodata path
    m._moveStartTime = GameTimeManager.getGameTicks();

    if (_log.isDebugEnabled())
        _log.debug("time to target:" + ticksToMove);

    // Set the L2Creature _move object to MoveData object
    _move = m;

    MovementController.getInstance().add(this, ticksToMove);
}

From source file:pl.edu.icm.visnow.lib.utils.ImageUtilities.java

public static BufferedImage compensateLensDistortion(BufferedImage img, double k) {
    if (img == null) {
        return null;
    }// ww w .  j  a  va  2  s .c  om

    int w = img.getWidth();
    int h = img.getHeight();
    BufferedImage out = new BufferedImage(w, h, img.getType());

    int x0 = (int) Math.floor(w / 2) + 1;
    int y0 = (int) Math.floor(h / 2) + 1;

    double ru, theta, ww, rd;
    int xd, yd;

    for (int x = 0; x < w; x++) {
        for (int y = 0; y < h; y++) {
            ru = Math.sqrt((x - x0) * (x - x0) + (y - y0) * (y - y0));
            theta = Math.atan2(y - y0, x - x0);
            ww = Math.pow(ru / (2 * k) + Math.sqrt((ru * ru) / (4 * k * k) + 1 / (27 * k * k * k)), 1.0 / 3.0);
            rd = ww - 1 / (3 * k * ww);

            //nearest neighbour---------------------------------------
            xd = (int) Math.round(rd * Math.cos(theta)) + x0;
            yd = (int) Math.round(rd * Math.sin(theta)) + y0;
            if (xd >= 0 && yd >= 0 && xd < w && yd < h) {
                //piksel nowy x,y = piksel stary xd,yd
                out.setRGB(x, y, img.getRGB(xd, yd));
            }
            //---------------------------------------------------------
        }
    }
    return out;
}

From source file:lu.lippmann.cdb.graph.mouse.CadralEditingGraphMousePlugin.java

/**
 * Function to draw the arrow while drawing
 * @param down/*from  www .j  a v  a2  s  .co m*/
 * @param out
 */
private void transformArrowShape(Point2D down, Point2D out) {
    float x1 = (float) down.getX();
    float y1 = (float) down.getY();
    float x2 = (float) out.getX();
    float y2 = (float) out.getY();
    AffineTransform xform = AffineTransform.getTranslateInstance(x2, y2);
    float dx = x2 - x1;
    float dy = y2 - y1;
    float thetaRadians = (float) Math.atan2(dy, dx);
    xform.rotate(thetaRadians);
    arrowShape = xform.createTransformedShape(rawArrowShape);
}