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:com.eclipsesource.tabris.demos.entrypoints.GeolocationDemo.java

private double distFrom(double lat1, double lon1, double lat2, double lon2) {
    double d2r = Math.PI / 180;
    double dlong = (lon2 - lon1) * d2r;
    double dlat = (lat2 - lat1) * d2r;
    double a = Math.pow(Math.sin(dlat / 2.0), 2)
            + Math.cos(lat1 * d2r) * Math.cos(lat2 * d2r) * Math.pow(Math.sin(dlong / 2.0), 2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    return 6367 * c;
}

From source file:naftoreiclag.villagefive.util.math.Vec2.java

@Deprecated
public double angleTo(Vec2 loc) {
    double ang = Math.atan2(loc.getY() - this.getY(), loc.getX() - this.getX());

    return ang > 0 ? ang : ang + Math.PI * 2;
}

From source file:org.envirocar.app.util.Util.java

/**
 * Returns the distance of two points in kilometers.
 * /*from   w  ww.  j av  a  2s  .c om*/
 * @param lat1
 * @param lng1
 * @param lat2
 * @param lng2
 * @return distance in km
 */
public static double getDistance(double lat1, double lng1, double lat2, double lng2) {

    double earthRadius = 6369;
    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 = earthRadius * c;

    return dist;

}

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

/**
 * Function to create the edge shape while drawing
 * @param down//from  w  w w .j  ava 2  s . c o m
 * @param out
 */
private void transformEdgeShape(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(x1, y1);
    float dx = x2 - x1;
    float dy = y2 - y1;
    float thetaRadians = (float) Math.atan2(dy, dx);
    xform.rotate(thetaRadians);
    float dist = (float) Math.sqrt(dx * dx + dy * dy);
    xform.scale((double) dist / rawEdge.getBounds().getWidth(), 1.0d);
    edgeShape = xform.createTransformedShape(rawEdge);
}

From source file:naftoreiclag.villagefive.util.math.Vec2.java

public Angle getAngle() {
    return new Angle(Math.atan2(this.getY(), this.getX()));
}

From source file:com.net2plan.libraries.GraphUtils.java

/** Computes the Haversine distance between two points, that is, the shortest distance over the Earth's surface.
 * //from   w w  w . j  a  v a 2  s .  c om
 * <p><b>Important</b>: It is assumed an Earth's radius equal to {@link com.net2plan.utils.Constants#EARTH_RADIUS_IN_KM Constants.EARTH_RADIUS_IN_KM }</p>
 * 
 * <p><b>Important</b>: Coordinates are assumed to be given in degress, conversions to radians are made internally.</p>
 * 
 * @param point1 Point 1 (x-coord is equal to longitude, y-coord is equal to latitude)
 * @param point2 Point 2 (x-coord is equal to longitude, y-coord is equal to latitude)
 * @return Haversine distance between two points
 * @see <a href="http://www.movable-type.co.uk/scripts/latlong.html">Calculate distance, bearing and more between Latitude/Longitude points</a> */
public static double computeHaversineDistanceInKm(Point2D point1, Point2D point2) {
    double lon1 = point1.getX();
    double lat1 = point1.getY();
    double lon2 = point2.getX();
    double lat2 = point2.getY();

    double dLat = Math.toRadians(lat2 - lat1);
    double dLon = Math.toRadians(lon2 - lon1);
    double sindLat = Math.sin(dLat / 2);
    double sindLon = Math.sin(dLon / 2);
    double a = Math.pow(sindLat, 2)
            + Math.pow(sindLon, 2) * Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2));
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    return Constants.EARTH_RADIUS_IN_KM * c;
}

From source file:net.famzangl.minecraft.minebot.ai.AIHelper.java

private boolean face(double x, double y, double z, float yawInfluence, float pitchInfluence) {

    LOGGER.trace(MARKER_FACING,//from ww w.  j a v  a2  s .c om
            "facing " + x + "," + y + "," + z + " using influence: " + yawInfluence + ";" + pitchInfluence);
    final double d0 = x - mc.thePlayer.posX;
    final double d1 = z - mc.thePlayer.posZ;
    final double d2 = y - mc.thePlayer.posY - mc.thePlayer.getEyeHeight();
    final double d3 = d0 * d0 + d2 * d2 + d1 * d1;

    if (d3 >= 2.500000277905201E-7D) {
        final float rotationYaw = mc.thePlayer.rotationYaw;
        final float rotationPitch = mc.thePlayer.rotationPitch;

        final float yaw = (float) (Math.atan2(d1, d0) * 180.0D / Math.PI) - 90.0F;
        final float pitch = (float) -(Math.atan2(d2, Math.sqrt(d0 * d0 + d1 * d1)) * 180.0D / Math.PI);
        float yawChange = closestRotation(yaw - rotationYaw);
        float pitchChange = pitch - rotationPitch;
        assert -Math.PI <= yawChange && yawChange <= Math.PI;
        float yawClamp = Math.min(Math.abs(MAX_YAW_CHANGE / yawChange), 1);
        float pitchClamp = Math.min(Math.abs(MAX_PITCH_CHANGE / pitchChange), 1);
        float clamp = Math.min(yawClamp, pitchClamp);
        if (yawInfluence <= 0 && pitchInfluence <= 0) {
            // only test, do not set
            return Math.abs(yawChange) < .01 && Math.abs(pitchChange) < .01;
        }

        yawInfluence = Math.min(yawInfluence, clamp);
        pitchInfluence = Math.min(pitchInfluence, clamp);
        // TODO: Make this linear?

        mc.thePlayer.setAngles(yawChange / 0.15f * yawInfluence, -pitchChange / 0.15f * pitchInfluence);
        invalidateObjectMouseOver();

        LOGGER.trace(MARKER_FACING, "facing clamped at " + clamp + ", new influence:  " + yawInfluence + ";"
                + pitchInfluence + ", done: " + (clamp > .999));

        return clamp > .999;
    }
    return true;
}

From source file:PathLength.java

/**
 * Returns the point that is at the given length along the path.
 * @param length The length along the path
 * @return The point at the given length
 *//*from ww  w.  j  ava 2  s . c om*/
public Point2D pointAtLength(float length) {
    int upperIndex = findUpperIndex(length);
    if (upperIndex == -1) {
        // Length is off the end of the path.
        return null;
    }

    PathSegment upper = (PathSegment) segments.get(upperIndex);

    if (upperIndex == 0) {
        // Length was probably zero, so return the upper point.
        return new Point2D.Float(upper.getX(), upper.getY());
    }

    PathSegment lower = (PathSegment) segments.get(upperIndex - 1);

    // Now work out where along the line would be the length.
    float offset = length - lower.getLength();

    // Compute the slope.
    double theta = Math.atan2(upper.getY() - lower.getY(), upper.getX() - lower.getX());

    float xPoint = (float) (lower.getX() + offset * Math.cos(theta));
    float yPoint = (float) (lower.getY() + offset * Math.sin(theta));

    return new Point2D.Float(xPoint, yPoint);
}

From source file:eu.vital.orchestrator.rest.EvaluationRESTService.java

private double distance(double lat1, double lon1, double lat2, double lon2) {
    int R = 6371; // Radius of the earth in km
    double dLat = deg2rad(lat2 - lat1); // deg2rad below
    double dLon = deg2rad(lon2 - lon1);
    double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
            + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(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; // Distance in km
    return d;//from ww  w  . jav  a2  s  . c o m
}

From source file:net.mceoin.cominghome.LocationService.java

/**
 * Calculate distance in meters between two points.
 *
 * @param lat1 Latitude for point 1//from   w  ww.  j a  v  a2 s . c o m
 * @param lng1 Longitude for point 1
 * @param lat2 Latitude for point 2
 * @param lng2 Longitude for point 2
 * @return distance in meters
 */
public static float distFrom(double lat1, double lng1, double lat2, double lng2) {
    double earthRadius = 3958.75;
    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 = earthRadius * c;

    int meterConversion = 1609;

    return (float) (dist * meterConversion);
}