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.evgenyvyaz.cinaytaren.activity.FaceTrackerActivity.java

public double getAngle(double startLat, double startLng, double endLat, double endLng) throws JSONException {
    double longitude1 = startLng;
    double longitude2 = endLng;
    double latitude1 = Math.toRadians(startLat);
    double latitude2 = Math.toRadians(endLat);
    double longDiff = Math.toRadians(longitude2 - longitude1);
    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:org.csa.rstb.polarimetric.gpf.decompositions.hAAlpha.java

/**
 * Compute H-A-Alpha parameters for given coherency matrix T3
 *
 * @param Tr Real part of the coherency matrix
 * @param Ti Imaginary part of the coherency matrix
 * @return The H-A-Alpha parameters//from w w  w  .  j  a  v  a 2s. co m
 */
public static HAAlpha computeHAAlpha(final double[][] Tr, final double[][] Ti) {

    final double[][] EigenVectRe = new double[3][3];
    final double[][] EigenVectIm = new double[3][3];
    final double[] EigenVal = new double[3];

    final double[] lambda = new double[3];
    final double[] p = new double[3];
    final double[] alpha = new double[3];
    final double[] phi = new double[3];
    final double[] beta = new double[3];
    final double[] delta = new double[3];
    final double[] gamma = new double[3];

    PolOpUtils.eigenDecomposition(3, Tr, Ti, EigenVectRe, EigenVectIm, EigenVal);

    double sum = 0.0;
    for (int i = 0; i < 3; ++i) {
        lambda[i] = EigenVal[i];
        sum += lambda[i];
    }

    final double EPS = Constants.EPS;
    for (int j = 0; j < 3; ++j) {
        alpha[j] = FastMath.acos(norm(EigenVectRe[0][j], EigenVectIm[0][j])) * Constants.RTOD;
        beta[j] = Math.atan2(norm(EigenVectRe[2][j], EigenVectIm[2][j]),
                EPS + norm(EigenVectRe[1][j], EigenVectIm[1][j])) * Constants.RTOD;
        phi[j] = Math.atan2(EigenVectIm[0][j], EPS + EigenVectRe[0][j]);
        delta[j] = Math.atan2(EigenVectIm[1][j], EPS + EigenVectRe[1][j]) - phi[j];
        delta[j] = Math.atan2(FastMath.sin(delta[j]), FastMath.cos(delta[j]) + EPS) * Constants.RTOD;
        gamma[j] = Math.atan2(EigenVectIm[2][j], EPS + EigenVectRe[2][j]) - phi[j];
        gamma[j] = Math.atan2(FastMath.sin(gamma[j]), FastMath.cos(gamma[j]) + EPS) * Constants.RTOD;
        p[j] = lambda[j] / sum;
        if (p[j] < 0) {
            p[j] = 0;
        } else if (p[j] > 1) {
            p[j] = 1;
        }
    }

    double meanLambda = 0.0;
    double meanAlpha = 0.0;
    double meanBeta = 0.0;
    double meanDelta = 0.0;
    double meanGamma = 0.0;
    double entropy = 0.0;
    for (int k = 0; k < 3; ++k) {
        meanLambda += p[k] * lambda[k];
        meanAlpha += p[k] * alpha[k];
        meanBeta += p[k] * beta[k];
        meanDelta += p[k] * delta[k];
        meanGamma += p[k] * gamma[k];
        entropy -= p[k] * Math.log(p[k] + EPS);
    }

    entropy /= LOG_3;
    final double anisotropy = (p[1] - p[2]) / (p[1] + p[2] + EPS);

    return new HAAlpha(entropy, anisotropy, meanAlpha, meanBeta, meanDelta, meanGamma, meanLambda, alpha[0],
            alpha[1], alpha[2], lambda[0], lambda[1], lambda[2]);
}

From source file:org.csa.rstb.gpf.decompositions.hAAlpha.java

/**
 * Compute H-A-Alpha parameters for given coherency matrix T3
 *
 * @param Tr Real part of the coherency matrix
 * @param Ti Imaginary part of the coherency matrix
 * @return The H-A-Alpha parameters/*  w  ww. jav a2  s.co m*/
 */
public static HAAlpha computeHAAlpha(final double[][] Tr, final double[][] Ti) {

    final double[][] EigenVectRe = new double[3][3];
    final double[][] EigenVectIm = new double[3][3];
    final double[] EigenVal = new double[3];

    final double[] lambda = new double[3];
    final double[] p = new double[3];
    final double[] alpha = new double[3];
    final double[] phi = new double[3];
    final double[] beta = new double[3];
    final double[] delta = new double[3];
    final double[] gamma = new double[3];

    PolOpUtils.eigenDecomposition(3, Tr, Ti, EigenVectRe, EigenVectIm, EigenVal);

    double sum = 0.0;
    for (int i = 0; i < 3; ++i) {
        lambda[i] = EigenVal[i];
        sum += lambda[i];
    }

    final double EPS = Constants.EPS;
    for (int j = 0; j < 3; ++j) {
        alpha[j] = FastMath.acos(norm(EigenVectRe[0][j], EigenVectIm[0][j])) * MathUtils.RTOD;
        beta[j] = Math.atan2(norm(EigenVectRe[2][j], EigenVectIm[2][j]),
                EPS + norm(EigenVectRe[1][j], EigenVectIm[1][j])) * MathUtils.RTOD;
        phi[j] = Math.atan2(EigenVectIm[0][j], EPS + EigenVectRe[0][j]);
        delta[j] = Math.atan2(EigenVectIm[1][j], EPS + EigenVectRe[1][j]) - phi[j];
        delta[j] = Math.atan2(FastMath.sin(delta[j]), FastMath.cos(delta[j]) + EPS) * MathUtils.RTOD;
        gamma[j] = Math.atan2(EigenVectIm[2][j], EPS + EigenVectRe[2][j]) - phi[j];
        gamma[j] = Math.atan2(FastMath.sin(gamma[j]), FastMath.cos(gamma[j]) + EPS) * MathUtils.RTOD;
        p[j] = lambda[j] / sum;
        if (p[j] < 0) {
            p[j] = 0;
        } else if (p[j] > 1) {
            p[j] = 1;
        }
    }

    double meanLambda = 0.0;
    double meanAlpha = 0.0;
    double meanBeta = 0.0;
    double meanDelta = 0.0;
    double meanGamma = 0.0;
    double entropy = 0.0;
    for (int k = 0; k < 3; ++k) {
        meanLambda += p[k] * lambda[k];
        meanAlpha += p[k] * alpha[k];
        meanBeta += p[k] * beta[k];
        meanDelta += p[k] * delta[k];
        meanGamma += p[k] * gamma[k];
        entropy -= p[k] * Math.log(p[k] + EPS);
    }

    entropy /= LOG_3;
    final double anisotropy = (p[1] - p[2]) / (p[1] + p[2] + EPS);

    return new HAAlpha(entropy, anisotropy, meanAlpha, meanBeta, meanDelta, meanGamma, meanLambda, alpha[0],
            alpha[1], alpha[2], lambda[0], lambda[1], lambda[2]);
}

From source file:syncleus.dann.math.ComplexNumber.java

@Override
public final ComplexNumber log() {
    return new ComplexNumber(Math.log(this.absScalar()), Math.atan2(getImaginary(), getReal()));
}

From source file:br.liveo.ndrawer.ui.fragment.MainFragment22.java

public double getDistanceBetweenTwoCoordiantes(double lat1, double lat2, double lon1, double lon2) {
    try {/*from   w ww  .j  av  a 2  s  .  c  om*/
        double R = 6371.0; // km

        double dLat = toRad(lat2 - lat1);
        double dLon = toRad(lon2 - lon1);

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

        // return unit meter
        return d * 1000;
    } catch (Exception e) {
        return 0;
    }
}

From source file:eu.hansolo.tilesfx.tools.Location.java

public double calcBearingInDegree(final double LAT_1, final double LON_1, final double LAT_2,
        final double LON_2) {
    double lat1 = Math.toRadians(LAT_1);
    double lon1 = Math.toRadians(LON_1);
    double lat2 = Math.toRadians(LAT_2);
    double lon2 = Math.toRadians(LON_2);
    double deltaLon = lon2 - lon1;
    double deltaPhi = Math.log(Math.tan(lat2 * 0.5 + Math.PI * 0.25) / Math.tan(lat1 * 0.5 + Math.PI * 0.25));
    if (Math.abs(deltaLon) > Math.PI) {
        if (deltaLon > 0) {
            deltaLon = -(2.0 * Math.PI - deltaLon);
        } else {//from w  w w  .j av  a  2s  .  c  o  m
            deltaLon = (2.0 * Math.PI + deltaLon);
        }
    }
    double bearing = (Math.toDegrees(Math.atan2(deltaLon, deltaPhi)) + 360.0) % 360.0;
    return bearing;
}

From source file:org.openhab.binding.astro.internal.calc.SunCalc.java

private double getAzimuth(double th, double a, double phi, double d) {
    double H = th - a;
    return Math.atan2(Math.sin(H), Math.cos(H) * Math.sin(phi) - Math.tan(d) * Math.cos(phi));
}

From source file:info.wncwaterfalls.app.ResultsActivity.java

public static Location calculatePositionAtRange(Location origin, double range, double bearing) {
    double EarthRadius = 6371000; // m

    double latA = Math.toRadians(origin.getLatitude());
    double lonA = Math.toRadians(origin.getLongitude());
    double angularDistance = range / EarthRadius;
    double trueCourse = Math.toRadians(bearing);

    double lat = Math.asin(Math.sin(latA) * Math.cos(angularDistance)
            + Math.cos(latA) * Math.sin(angularDistance) * Math.cos(trueCourse));

    double dlon = Math.atan2(Math.sin(trueCourse) * Math.sin(angularDistance) * Math.cos(latA),
            Math.cos(angularDistance) - Math.sin(latA) * Math.sin(lat));

    double lon = ((lonA + dlon + Math.PI) % (Math.PI * 2)) - Math.PI;

    lat = Math.toDegrees(lat);/*from   w ww.j a va  2 s  . c om*/
    lon = Math.toDegrees(lon);

    Location destination = new Location("");
    destination.setLatitude((float) lat);
    destination.setLongitude((float) lon);

    return destination;
}

From source file:org.jax.haplotype.analysis.visualization.SimplePhylogenyTreeImageFactory.java

/**
 * Transform the tree layout so that it fits nicely in the given image
 * dimensions/* ww  w .  j a  v  a2 s .c om*/
 * @param treeLayout
 *          the layout to transform
 * @param imageWidth
 *          the image width
 * @param imageHeight
 *          the image height
 */
private void transformTreeLayout(VisualTreeNode treeLayout, int imageWidth, int imageHeight,
        FontRenderContext frc) {
    Dimension2D maximalNodeLabelDimension = this.calculateMaximalNodeDimension(treeLayout, frc);
    double widthBuffer = maximalNodeLabelDimension.getWidth() + BORDER_WHITE_SPACE;
    double heightBuffer = maximalNodeLabelDimension.getHeight() + BORDER_WHITE_SPACE;

    // perform rotation to improve the use of space
    {
        // center around 0, 0
        VisualTreeNode[] mostDistantPair = this.getMostDistantNodePair(treeLayout);
        Point2D distantPoint1 = mostDistantPair[0].getPosition();
        Point2D distantPoint2 = mostDistantPair[1].getPosition();
        double xDiff = distantPoint1.getX() - distantPoint2.getX();
        double yDiff = distantPoint1.getY() - distantPoint2.getY();
        this.translateTreeLayout(treeLayout, (xDiff / 2.0) - distantPoint1.getX(),
                (yDiff / 2.0) - distantPoint2.getY());

        // rotate
        double thetaRadians = Math.atan2(yDiff, xDiff);

        if (imageWidth >= imageHeight) {
            this.rotateTreeLayout(treeLayout, -thetaRadians);
        } else {
            this.rotateTreeLayout(treeLayout, (Math.PI / 2.0 - thetaRadians));
        }
    }

    Rectangle2D boundingRectangle = this.calculateBounds(treeLayout, null);

    // center around the middle of the display area
    this.translateTreeLayout(treeLayout, -boundingRectangle.getX(), -boundingRectangle.getY());

    // grow the image to fill a larger area
    double xScale = (imageWidth - widthBuffer) / boundingRectangle.getWidth();
    double yScale = (imageHeight - heightBuffer) / boundingRectangle.getHeight();
    double smallerScale = Math.min(xScale, yScale);

    this.scaleTreeLayout(treeLayout, smallerScale);

    // center around the middle of the display area
    boundingRectangle = this.calculateBounds(treeLayout, null);
    this.translateTreeLayout(treeLayout,
            ((imageWidth - boundingRectangle.getWidth()) / 2.0) - boundingRectangle.getX(),
            ((imageHeight - boundingRectangle.getHeight()) / 2.0) - boundingRectangle.getY());
}

From source file:uk.ac.cam.cl.dtg.util.locations.PostCodeIOLocationResolver.java

/**
 * @param lat1/*from w  w w  . j a  v a2 s .  co m*/
 *            - latitude 1
 * @param lon1
 *            - longitude 1
 * @param lat2
 *            - latitude 2
 * @param lon2
 *            - longitude 2
 * @return - distance in miles
 */
private double getLatLonDistanceInMiles(final double lat1, final double lon1, final double lat2,
        final double lon2) {
    // borrowed from http://www.movable-type.co.uk/scripts/latlong.html
    int R = 6371000;
    double phi1 = Math.toRadians(lat1);
    double phi2 = Math.toRadians(lat2);
    double deltaPhi = Math.toRadians(lat2 - lat1);
    double deltaLambda = Math.toRadians(lon2 - lon1);

    double a = Math.sin(deltaPhi / 2) * Math.sin(deltaPhi / 2)
            + Math.cos(phi1) * Math.cos(phi2) * Math.sin(deltaLambda / 2) * Math.sin(deltaLambda / 2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

    double d = R * c;

    // convert from metres to miles
    d = (d / 1000) * 0.621371;

    return d;
}