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.nd4j.linalg.api.complex.BaseComplexFloat.java

@Override
public IComplexNumber log() {
    IComplexNumber result = dup();/*from   ww  w.j a  va  2  s  . co  m*/
    float real = (float) result.realComponent();
    float imaginary = (float) result.imaginaryComponent();
    double modulus = Math.sqrt(real * real + imaginary * imaginary);
    double arg = Math.atan2(imaginary, real);
    return result.set(Math.log(modulus), arg);
}

From source file:org.shaman.terrain.polygonal.PolygonalMapGenerator.java

/**
 * Third step, assign coastline/*  ww  w.ja  v  a2 s. co  m*/
 */
private void assignCoastline() {
    if (graph == null || coastline == null) {
        return;
    }
    Random rand = new Random(seed);
    //reset
    for (Graph.Center c : graph.centers) {
        c.water = false;
        c.border = false;
        c.ocean = false;
    }
    for (Graph.Corner c : graph.corners) {
        c.water = false;
        c.ocean = false;
    }
    //set water parameter of corners
    int waterCorners = 0;
    switch (coastline) {
    case PERLIN:
        //Fractal perlin noise
        Noise[] noise = new Noise[5];
        for (int i = 0; i < noise.length; ++i) {
            noise[i] = new Noise(rand.nextLong());
        }
        for (Graph.Corner c : graph.corners) {
            float val = 0;
            float octave = 6; //to be tuned
            float amplitude = 0.5f; //to be tuned
            for (int i = 0; i < noise.length; ++i) {
                val += noise[i].noise(c.point.x * octave, c.point.y * octave) * amplitude;
                octave *= 2;
                amplitude /= 2.5;
            }
            float dist = c.point.distanceSquared(0.5f, 0.5f);
            float distInfluence = 2.2f; //to be tuned
            float perlinOffset = -0.2f; //to be tuned
            if (val > perlinOffset + distInfluence * dist && !c.border) {
                c.water = false;
            } else {
                c.water = true;
                waterCorners++;
            }
        }
        break;

    case RADIAL:
        //radial sine waves
        double islandFactor = 1.07;
        int bumps = rand.nextInt(6) + 1;
        double startAngle = rand.nextDouble() * 2 * Math.PI;
        double dipAngle = rand.nextDouble() * 2 * Math.PI;
        double dipWidth = rand.nextDouble() * 0.5 + 0.2;
        for (Graph.Corner c : graph.corners) {
            double x = (c.point.x - 0.5) * 2.2;
            double y = (c.point.y - 0.5) * 2.2;
            double angle = Math.atan2(y, x);
            double length = 0.5 * (Math.max(Math.abs(x), Math.abs(y)) + new Vector2d(x, y).length());
            double r1 = 0.5 * 0.4 * Math.sin(startAngle + bumps * angle + Math.cos((bumps + 3) * angle));
            double r2 = 0.7 - 0.2 * Math.sin(startAngle + bumps * angle - Math.sin((bumps + 2) * angle));
            if (Math.abs(angle - dipAngle) < dipWidth || Math.abs(angle - dipAngle + 2 * Math.PI) < dipWidth
                    || Math.abs(angle - dipAngle - 2 * Math.PI) < dipWidth) {
                r1 = r2 = 0.2;
            }
            if ((length < r1 || (length > r1 * islandFactor && length < r2)) && !c.border) {
                c.water = false;
            } else {
                c.water = true;
                waterCorners++;
            }
        }
        break;
    }
    LOG.log(Level.INFO, "corners with water: {0}, without water: {1}",
            new Object[] { waterCorners, graph.corners.size() - waterCorners });

    findOceans();

    updateBiomesGeometry();
}

From source file:interfaceTisseoWS.ST3.java

public double getDistance(double x1, double y1, double x2, double y2) {
    double d2r = Math.PI / 180;

    try {/*ww  w  . j a  v  a  2 s  . co  m*/
        double dlong = (x2 - x1) * d2r;
        double dlat = (y2 - y1) * d2r;
        double a = Math.pow(Math.sin(dlat / 2.0), 2)
                + Math.cos(y1 * d2r) * Math.cos(y2 * d2r) * Math.pow(Math.sin(dlong / 2.0), 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        double d = 6367 * c;

        return d;

    } catch (Exception e) {
    }
    return -1.0;
}

From source file:org.geogebra.common.geogebra3D.kernel3D.geos.Geo3DVec.java

public double arg() {
    return Math.atan2(y, x);
}

From source file:org.esa.nest.gpf.AzimuthShiftOp.java

private double estimateAzOffsets(final Band mBandI, final Band mBandQ, final Band sBandI, final Band sBandQ,
        final Rectangle backwardRectangle, final Rectangle forwardRectangle, final double spectralSeparation) {

    final Tile mTileIBack = getSourceTile(mBandI, backwardRectangle);
    final Tile mTileQBack = getSourceTile(mBandQ, backwardRectangle);
    final Tile sTileIBack = getSourceTile(sBandI, backwardRectangle);
    final Tile sTileQBack = getSourceTile(sBandQ, backwardRectangle);
    final double[] mIBackArray = (double[]) mTileIBack.getDataBuffer().getElems();
    final double[] mQBackArray = (double[]) mTileQBack.getDataBuffer().getElems();
    final double[] sIBackArray = (double[]) sTileIBack.getDataBuffer().getElems();
    final double[] sQBackArray = (double[]) sTileQBack.getDataBuffer().getElems();

    final Tile mTileIFor = getSourceTile(mBandI, forwardRectangle);
    final Tile mTileQFor = getSourceTile(mBandQ, forwardRectangle);
    final Tile sTileIFor = getSourceTile(sBandI, forwardRectangle);
    final Tile sTileQFor = getSourceTile(sBandQ, forwardRectangle);
    final double[] mIForArray = (double[]) mTileIFor.getDataBuffer().getElems();
    final double[] mQForArray = (double[]) mTileQFor.getDataBuffer().getElems();
    final double[] sIForArray = (double[]) sTileIFor.getDataBuffer().getElems();
    final double[] sQForArray = (double[]) sTileQFor.getDataBuffer().getElems();

    final int arrayLength = mIBackArray.length;
    final double[] backIntReal = new double[arrayLength];
    final double[] backIntImag = new double[arrayLength];
    complexArrayMultiplication(mIBackArray, mQBackArray, sIBackArray, sQBackArray, backIntReal, backIntImag);

    final double[] forIntReal = new double[arrayLength];
    final double[] forIntImag = new double[arrayLength];
    complexArrayMultiplication(mIForArray, mQForArray, sIForArray, sQForArray, forIntReal, forIntImag);

    final double[] diffIntReal = new double[arrayLength];
    final double[] diffIntImag = new double[arrayLength];
    complexArrayMultiplication(backIntReal, backIntImag, forIntReal, forIntImag, diffIntReal, diffIntImag);

    double meanReal = 0.0;
    double meanImag = 0.0;
    for (int i = 0; i < arrayLength; i++) {
        meanReal += diffIntReal[i];/*from www  .  j  a v  a2 s  .c o  m*/
        meanImag += diffIntImag[i];
    }
    meanReal /= arrayLength;
    meanImag /= arrayLength;

    final double phase = Math.atan2(meanImag, meanReal);
    final double offset = phase
            / (2 * Math.PI * spectralSeparation * subSwath[subSwathIndex - 1].azimuthTimeInterval);

    return offset;
}

From source file:org.esa.snap.engine_utilities.eo.GeoUtils.java

/**
 * // Given starting (GLON1,GLAT1) and end points (GLON2,GLAT2)
 * // calculate distance in meters and initial headings from start to
 * // end (return variable head1),//w w w. j  a  va2 s . c o m
 * // and from end to start point (return variable head2)
 * <p>
 * // Input:
 * // lon1:   longitude
 * // lat1:   latitude
 * // lon2:   longitude
 * // lat2:   latitude
 * <p>
 * // Output:
 * // dist:   distance in m
 * // head1:   azimuth in degrees mesured in the direction North east south west
 * //         from (lon1,lat1) to (lon2, lat2)
 * // head2:   azimuth in degrees mesured in the direction North east south west
 * //         from (lon2,lat2) to (lon1, lat1)
 *
 * @param lon1
 * @param lat1
 * @param lon2
 * @param lat2
 * @return
 */
public static DistanceHeading vincenty_inverse(double lon1, double lat1, double lon2, double lat2) {

    final DistanceHeading output = new DistanceHeading();

    if ((Math.abs(lon1 - lon2) < EPS5) && (Math.abs(lat1 - lat2) < EPS5)) {
        output.distance = 0;
        output.heading1 = -1;
        output.heading2 = -1;
        return output;
    }

    lat1 *= Constants.DTOR;
    lat2 *= Constants.DTOR;
    lon1 *= Constants.DTOR;
    lon2 *= Constants.DTOR;

    // Model WGS84:
    //    F=1/298.25722210;   // flattening
    final double F = 0.0; //defF;

    final double R = 1 - F;
    double TU1 = R * FastMath.tan(lat1);
    double TU2 = R * FastMath.tan(lat2);
    final double CU1 = 1.0 / Math.sqrt(TU1 * TU1 + 1.0);
    final double SU1 = CU1 * TU1;
    final double CU2 = 1.0 / Math.sqrt(TU2 * TU2 + 1.0);
    double S = CU1 * CU2;
    double BAZ = S * TU2;
    double FAZ = BAZ * TU1;
    double X = lon2 - lon1;

    double SX, CX, SY, CY, Y, SA, C2A, CZ, E, C, D;
    do {
        SX = FastMath.sin(X);
        CX = FastMath.cos(X);
        TU1 = CU2 * SX;
        TU2 = BAZ - SU1 * CU2 * CX;
        SY = Math.sqrt(TU1 * TU1 + TU2 * TU2);
        CY = S * CX + FAZ;
        Y = Math.atan2(SY, CY);
        SA = S * SX / SY;
        C2A = -SA * SA + 1.;
        CZ = FAZ + FAZ;
        if (C2A > 0.)
            CZ = -CZ / C2A + CY;
        E = CZ * CZ * 2. - 1.;
        C = ((-3. * C2A + 4.) * F + 4.) * C2A * F / 16.;
        D = X;
        X = ((E * CY * C + CZ) * SY * C + Y) * SA;
        X = (1. - C) * X * F + lon2 - lon1;
    } while (Math.abs(D - X) > (0.01));

    FAZ = Math.atan2(TU1, TU2);
    BAZ = Math.atan2(CU1 * SX, BAZ * CX - SU1 * CU2) + Constants.PI;
    X = Math.sqrt((1. / R / R - 1.) * C2A + 1.) + 1.;
    X = (X - 2.) / X;
    C = 1. - X;
    C = (X * X / 4. + 1.) / C;
    D = (0.375 * X * X - 1.) * X;
    X = E * CY;
    S = 1. - E - E;
    S = ((((SY * SY * 4. - 3.) * S * CZ * D / 6. - X) * D / 4. + CZ) * SY * D + Y) * C * WGS84.a * R;

    output.distance = S;
    output.heading1 = FAZ * Constants.RTOD;
    output.heading2 = BAZ * Constants.RTOD;

    while (output.heading1 < 0)
        output.heading1 += 360;
    while (output.heading2 < 0)
        output.heading2 += 360;

    return output;
}

From source file:org.deegree.geometry.linearization.CurveLinearizer.java

/**
 * Returns a linearized version (i.e. a {@link LineStringSegment}) of the given {@link CubicSpline}.
 * <p>//from w w  w . ja  v  a2 s . c  o  m
 * A cubic spline consists of n polynomials of degree 3: S<sub>j</sub>(x) = a<sub>j</sub> +
 * b<sub>j</sub>*(x-x<sub>j</sub>) + c<sub>j</sub>*(x-x<sub>j</sub>)<sup>2</sup> +
 * d<sub>j</sub>*(x-x<sub>j</sub>)<sup>3</sup>; that acts upon the interval [x<sub>j</sub>,x<sub>j+1</sub>], 0 <=j<
 * n.
 * <p>
 * The algorithm for generating points on a spline defined with only control points and starting/ending tangents can
 * be found at <a
 * href="http://persson.berkeley.edu/128A/lec14-2x3.pdf">http://persson.berkeley.edu/128A/lec14-2x3.pdf</a> (last
 * visited 19/08/09)
 * 
 * @param spline
 *            curve segment to be linearized, must not be <code>null</code>
 * @param crit
 *            determines the interpolation quality / number of interpolation points, must not be <code>null</code>
 * @return linearized version of the input segment, never <code>null</code>
 */
public LineStringSegment linearizeCubicSpline(CubicSpline spline, LinearizationCriterion crit) {

    if (spline.getCoordinateDimension() != 2) {
        throw new UnsupportedOperationException(
                "Linearization of the cubic spline is only suported for a spline in 2D.");
    }

    Points controlPts = spline.getControlPoints();
    // build an array of Point in order to sort it in ascending order
    Point[] pts = new Point[controlPts.size()];
    // n denotes the # of polynomials, that is one less than the # of control pts
    int n = controlPts.size() - 1;
    for (int i = 0; i <= n; i++) {
        pts[i] = controlPts.get(i);
    }

    double startTan = Math.atan2(spline.getVectorAtStart().get1(), spline.getVectorAtStart().get0());
    double endTan = Math.atan2(spline.getVectorAtEnd().get1(), spline.getVectorAtEnd().get0());

    boolean ascending = true;
    if (pts[0].get0() > pts[1].get0()) {
        ascending = false;
    }

    for (int i = 0; i <= n - 1; i++) {
        if (ascending) {
            if (pts[i].get0() > pts[i + 1].get0()) {
                throw new UnsupportedOperationException(
                        "It is expected that the control points are ordered on the X-axis either ascendingly or descendingly.");
            }
        } else {
            if (pts[i].get0() < pts[i + 1].get0()) {
                throw new UnsupportedOperationException(
                        "It is expected that the control points are ordered on the X-axis either ascendingly or descendingly.");
            }
        }
    }

    if (!ascending) {
        // interchange the elements so that they are ordered ascendingly (on the X-axis)
        for (int i = 0; i <= (n / 2); i++) {
            Point aux = pts[i];
            pts[i] = pts[n - i];
            pts[n - i] = aux;
        }
        // also reverse the starting and ending tangents
        startTan = Math.atan2(-spline.getVectorAtEnd().get1(), -spline.getVectorAtEnd().get0());
        endTan = Math.atan2(-spline.getVectorAtStart().get1(), -spline.getVectorAtStart().get0());
    }

    // break-up the pts into xcoor in ycoor
    double xcoor[] = new double[n + 1];
    double ycoor[] = new double[n + 1];
    for (int i = 0; i <= n; i++) {
        xcoor[i] = pts[i].get0();
        ycoor[i] = pts[i].get1();
    }

    double[] h = new double[n];
    for (int i = 0; i <= n - 1; i++) {
        h[i] = xcoor[i + 1] - xcoor[i];
    }

    double[][] matrixA = constructMatrixA(h, n);

    double[] vectorb = constructVectorB(n, ycoor, h, startTan, endTan);

    double[] vectorx = solveLinearEquation(matrixA, vectorb);

    int numPoints = -1;
    if (crit instanceof NumPointsCriterion) {
        numPoints = ((NumPointsCriterion) crit).getNumberOfPoints();
    } else if (crit instanceof MaxErrorCriterion) {
        numPoints = ((MaxErrorCriterion) crit).getMaxNumPoints();
        if (numPoints <= 0) {
            throw new UnsupportedOperationException(
                    "Linearization of the cubic spline with MaxErrorCriterion is currently not supported, unless the number of points is provided.");
            // TODO it is mathematically hard to get an expression of the numOfPoints with respect to the error;
            // there would be two work-arounds as I can see them: 1) through a trial-and-error procedure determine
            // how small should the sampling interval be, so that the difference in value is less than the
            // given error; 2) use the mathematical expression used for the arc/circle (something with Math.acos...)
            // - it needs a good approximation for the radius.
        }
    }

    double[] interpolated = interpolateSpline(n, h, xcoor, ycoor, vectorx, numPoints);

    // populate a list of points, so that later a LineStringSegment can be built from it
    List<Point> iPoints = new ArrayList<Point>();
    ICRS crs = spline.getControlPoints().get(0).getCoordinateSystem();
    PrecisionModel pm = spline.getControlPoints().get(0).getPrecision();
    for (int i = 0; i < numPoints; i++) {
        iPoints.add(
                new DefaultPoint(null, crs, pm, new double[] { interpolated[2 * i], interpolated[2 * i + 1] }));
    }

    LineStringSegment lineSegment = geomFac.createLineStringSegment(new PointsList(iPoints));

    return lineSegment;
}

From source file:org.spout.api.math.Vector2.java

/**
 * Returns a Vector2Polar object with the same value as the given Vector2
 *
 * @param o Vector2 to use/*  w ww .  ja va  2s .com*/
 * @return
 */
public static Vector2Polar toVector2Polar(Vector2 o) {
    return new Vector2Polar(o.length(), Math.atan2(o.getY(), o.getX()));
}

From source file:de.sanandrew.mods.turretmod.entity.projectile.EntityTurretProjectile.java

@Override
public void setThrowableHeading(double x, double y, double z, float recoil, float randMulti) {
    float vecNormal = MathHelper.sqrt(x * x + y * y + z * z);
    x /= vecNormal;// w w w  . j  a  v  a 2 s  . com
    y /= vecNormal;
    z /= vecNormal;
    x += this.rand.nextGaussian() * (this.rand.nextBoolean() ? -1 : 1) * 0.007499999832361937D * randMulti;
    y += this.rand.nextGaussian() * (this.rand.nextBoolean() ? -1 : 1) * 0.007499999832361937D * randMulti;
    z += this.rand.nextGaussian() * (this.rand.nextBoolean() ? -1 : 1) * 0.007499999832361937D * randMulti;
    x *= recoil;
    y *= recoil;
    z *= recoil;
    this.motionX = x;
    this.motionY = y;
    this.motionZ = z;
    float vecPlaneNormal = MathHelper.sqrt(x * x + z * z);
    this.prevRotationYaw = this.rotationYaw = (float) (Math.atan2(x, z) * 180.0D / Math.PI);
    this.prevRotationPitch = this.rotationPitch = (float) (Math.atan2(y, vecPlaneNormal) * 180.0D / Math.PI);
}

From source file:org.esa.nest.eo.GeoUtils.java

/**
 * // Given starting (GLON1,GLAT1) and end points (GLON2,GLAT2)
 * // calculate distance in meters and initial headings from start to
 * // end (return variable head1),/*from   www  .ja  v a  2 s  . c o m*/
 * // and from end to start point (return variable head2)
 * <p/>
 * // Input:
 * // lon1:   longitude
 * // lat1:   latitude
 * // lon2:   longitude
 * // lat2:   latitude
 * <p/>
 * // Output:
 * // dist:   distance in m
 * // head1:   azimuth in degrees mesured in the direction North east south west
 * //         from (lon1,lat1) to (lon2, lat2)
 * // head2:   azimuth in degrees mesured in the direction North east south west
 * //         from (lon2,lat2) to (lon1, lat1)
 * @param lon1
 * @param lat1
 * @param lon2
 * @param lat2
 * @return
 */
public static DistanceHeading vincenty_inverse(double lon1, double lat1, double lon2, double lat2) {

    final DistanceHeading output = new DistanceHeading();

    if ((Math.abs(lon1 - lon2) < EPS5) && (Math.abs(lat1 - lat2) < EPS5)) {
        output.distance = 0;
        output.heading1 = -1;
        output.heading2 = -1;
        return output;
    }

    lat1 *= org.esa.beam.util.math.MathUtils.DTOR;
    lat2 *= org.esa.beam.util.math.MathUtils.DTOR;
    lon1 *= org.esa.beam.util.math.MathUtils.DTOR;
    lon2 *= org.esa.beam.util.math.MathUtils.DTOR;

    // Model WGS84:
    //    F=1/298.25722210;   // flattening
    final double F = 0.0; //defF;

    final double R = 1 - F;
    double TU1 = R * Math.tan(lat1);
    double TU2 = R * Math.tan(lat2);
    final double CU1 = 1.0 / Math.sqrt(TU1 * TU1 + 1.0);
    final double SU1 = CU1 * TU1;
    final double CU2 = 1.0 / Math.sqrt(TU2 * TU2 + 1.0);
    double S = CU1 * CU2;
    double BAZ = S * TU2;
    double FAZ = BAZ * TU1;
    double X = lon2 - lon1;

    double SX, CX, SY, CY, Y, SA, C2A, CZ, E, C, D;
    do {
        SX = Math.sin(X);
        CX = Math.cos(X);
        TU1 = CU2 * SX;
        TU2 = BAZ - SU1 * CU2 * CX;
        SY = Math.sqrt(TU1 * TU1 + TU2 * TU2);
        CY = S * CX + FAZ;
        Y = Math.atan2(SY, CY);
        SA = S * SX / SY;
        C2A = -SA * SA + 1.;
        CZ = FAZ + FAZ;
        if (C2A > 0.)
            CZ = -CZ / C2A + CY;
        E = CZ * CZ * 2. - 1.;
        C = ((-3. * C2A + 4.) * F + 4.) * C2A * F / 16.;
        D = X;
        X = ((E * CY * C + CZ) * SY * C + Y) * SA;
        X = (1. - C) * X * F + lon2 - lon1;
    } while (Math.abs(D - X) > (0.01));

    FAZ = Math.atan2(TU1, TU2);
    BAZ = Math.atan2(CU1 * SX, BAZ * CX - SU1 * CU2) + Math.PI;
    X = Math.sqrt((1. / R / R - 1.) * C2A + 1.) + 1.;
    X = (X - 2.) / X;
    C = 1. - X;
    C = (X * X / 4. + 1.) / C;
    D = (0.375 * X * X - 1.) * X;
    X = E * CY;
    S = 1. - E - E;
    S = ((((SY * SY * 4. - 3.) * S * CZ * D / 6. - X) * D / 4. + CZ) * SY * D + Y) * C * WGS84.a * R;

    output.distance = S;
    output.heading1 = FAZ * org.esa.beam.util.math.MathUtils.RTOD;
    output.heading2 = BAZ * org.esa.beam.util.math.MathUtils.RTOD;

    while (output.heading1 < 0)
        output.heading1 += 360;
    while (output.heading2 < 0)
        output.heading2 += 360;

    return output;
}