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.nextbreakpoint.nextfractal.mandelbrot.core.Expression.java

public static double funcPha(Number x) {
    return Math.atan2(x.i(), x.r());
}

From source file:dz.alkhwarizmix.winrak.java.model.vo.WinrakPosition.java

/**
 * http://www.movable-type.co.uk/scripts/latlong.html
 *///from  ww w  . j av a  2s .  c o m
@Override
public int distanceTo(final IWinrakPosition pos2) {
    final WinrakPosition pos1 = this;
    final Double R = 6371000.0; // metres
    final Double radLat1 = Math.toRadians(pos1.getLat());
    final Double radLat2 = Math.toRadians(pos2.getLat());
    final Double deltaLat = Math.toRadians(pos2.getLat() - pos1.getLat());
    final Double deltaLng = Math.toRadians(pos2.getLng() - pos1.getLng());
    final Double a = (Math.sin(deltaLat / 2) * Math.sin(deltaLat / 2))
            + (Math.cos(radLat1) * Math.cos(radLat2) * Math.sin(deltaLng / 2) * Math.sin(deltaLng / 2));
    final Double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    final Double d = R * c;
    return (d.intValue());
}

From source file:com.github.mfpdev.marketing.StoreCategorySegmentResolverResource.java

private double distance(double lon1, double lat1, double lon2, double lat2) {
    double dLat = Math.toRadians(lat2 - lat1); // Javascript functions in radians
    double dLon = 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(dLon / 2) * Math.sin(dLon / 2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    return EARTH_RADIUS * c; // Distance in km
}

From source file:edu.stanford.cfuller.imageanalysistools.util.VoronoiDiagram.java

protected Hyperplane<Euclidean2D> constructBisectingHyperplane(Vector2D firstPoint, Vector2D secondPoint) {

    Vector2D middlePoint = firstPoint.add(secondPoint).scalarMultiply(0.5);
    Vector2D difference = secondPoint.subtract(firstPoint);

    double angle = Math.atan2(difference.getY(), difference.getX());

    angle -= Math.PI / 2.0; // rotate 90 degrees

    Line bisector = new Line(middlePoint, angle);

    return bisector;

}

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

@Override
public void onUpdate() {
    this.isAirBorne = true;

    if (this.shooterCache != null && this.getDistanceToEntity(this.shooterCache) > this.maxDist) {
        this.setDead();
        return;//from  w ww . j a va2s . c o m
    }

    this.doCollisionCheck();

    this.posX += this.motionX;
    this.posY += this.motionY;
    this.posZ += this.motionZ;
    float f2 = MathHelper.sqrt(this.motionX * this.motionX + this.motionZ * this.motionZ);
    this.rotationYaw = (float) (Math.atan2(this.motionX, this.motionZ) * 180.0D / Math.PI);

    this.rotationPitch = (float) (Math.atan2(this.motionY, f2) * 180.0D / Math.PI);
    while (this.rotationPitch - this.prevRotationPitch < -180.0F) {
        this.prevRotationPitch -= 360.0F;
    }

    while (this.rotationPitch - this.prevRotationPitch >= 180.0F) {
        this.prevRotationPitch += 360.0F;
    }

    while (this.rotationYaw - this.prevRotationYaw < -180.0F) {
        this.prevRotationYaw -= 360.0F;
    }

    while (this.rotationYaw - this.prevRotationYaw >= 180.0F) {
        this.prevRotationYaw += 360.0F;
    }

    this.rotationPitch = this.prevRotationPitch + (this.rotationPitch - this.prevRotationPitch) * 0.2F;
    this.rotationYaw = this.prevRotationYaw + (this.rotationYaw - this.prevRotationYaw) * 0.2F;
    float speed = this.getSpeedMultiplierAir();

    if (this.isInWater()) {
        for (int i = 0; i < 4; i++) {
            float disPos = 0.25F;
            this.world.spawnParticle(EnumParticleTypes.WATER_BUBBLE, this.posX - this.motionX * disPos,
                    this.posY - this.motionY * disPos, this.posZ - this.motionZ * disPos, this.motionX,
                    this.motionY, this.motionZ);
        }

        speed = this.getSpeedMultiplierLiquid();
    }

    if (this.isWet()) {
        this.extinguish();
    }

    this.motionX *= speed;
    this.motionY *= speed;
    this.motionZ *= speed;
    this.motionY -= this.getArc() * 0.1F;
    this.setPosition(this.posX, this.posY, this.posZ);
    this.doBlockCollisions();
}

From source file:ActualizadorLocal.Clientes.ClienteNodos.java

public void procesarDatos(String datos) throws SQLException {
    //Preprocesamos los datos, para darle un nombre al array:

    datos = "{\"nodos\":" + datos + "}";

    JSONParser parser = new JSONParser();

    try {/*from  w w  w.j  a va  2s .  c  o m*/
        JSONObject obj = (JSONObject) parser.parse(datos);
        JSONArray lista = (JSONArray) obj.get("nodos");

        for (int i = 0; i < lista.size(); i++) {
            long a0 = (long) ((JSONObject) lista.get(i)).get("idNodo");
            String a1 = (String) ((JSONObject) lista.get(i)).get("nombre");
            double a2 = (double) ((JSONObject) lista.get(i)).get("latitud");
            double a3 = (double) ((JSONObject) lista.get(i)).get("longitud");

            nodos.add(a0);

            //Tenemos que calcular el polgono para la visualizacin de los datos mediante GOOGLE FUSION TABLES:

            double lat = Math.toRadians(new Double(a2));
            double lon = Math.toRadians(new Double(a3));

            double radio = 50.0 / 6378137.0;

            String cadena_poligono = "<Polygon><outerBoundaryIs><LinearRing><coordinates>";

            for (int j = 0; j <= 360; j = j + 15) {
                double r = Math.toRadians(j);
                double lat_rad = Math
                        .asin(Math.sin(lat) * Math.cos(radio) + Math.cos(lat) * Math.sin(radio) * Math.cos(r));
                double lon_rad = Math.atan2(Math.sin(r) * Math.sin(radio) * Math.cos(lat),
                        Math.cos(radio) - Math.sin(lat) * Math.sin(lat_rad));
                double lon_rad_f = ((lon + lon_rad + Math.PI) % (2 * Math.PI)) - Math.PI;

                cadena_poligono = cadena_poligono + Math.toDegrees(lon_rad_f) + "," + Math.toDegrees(lat_rad)
                        + ",0.0 ";
            }

            cadena_poligono = cadena_poligono + "</coordinates></LinearRing></outerBoundaryIs></Polygon>";

            this.InsertarDatos("\"" + (String) Long.toString(a0) + "\",\"" + a1 + "\",\"" + Double.toString(a2)
                    + "\",\"" + Double.toString(a3) + "\",\"" + cadena_poligono + "\"");

        }
    } catch (Exception e) {
        System.err.println(e.getMessage());
    }

    syncDB();

}

From source file:com.t_oster.visicut.misc.Helper.java

/**
* Compute the rotation angle of an affine transformation.
* Counter-clockwise rotation is considered positive.
*
* method taken from http://javagraphics.blogspot.com/
*
* @return rotation angle in radians (beween -pi and pi),
*  or NaN if the transformation is bogus.
*///from   w  w w . j  a v a2  s  .  co  m
public static double getRotationAngle(AffineTransform transform) {
    transform = (AffineTransform) transform.clone();
    // Eliminate any post-translation
    transform.preConcatenate(
            AffineTransform.getTranslateInstance(-transform.getTranslateX(), -transform.getTranslateY()));
    Point2D p1 = new Point2D.Double(1, 0);
    p1 = transform.transform(p1, p1);
    return Math.atan2(p1.getY(), p1.getX());
}

From source file:android.support.wear.widget.util.ArcSwipe.java

@VisibleForTesting
float getAngle(double x, double y) {
    double relativeX = x - (mBounds.width() / 2);
    double relativeY = y - (mBounds.height() / 2);
    double rowAngle = Math.atan2(relativeX, relativeY);
    double angle = -Math.toDegrees(rowAngle) - 180;
    if (angle < 0) {
        angle += 360;/*w  w w . ja v a  2 s.  c  o m*/
    }
    return (float) angle;
}

From source file:org.eclipse.dawnsci.analysis.dataset.roi.fitting.EllipseCoordinatesFunction.java

/**
 * Calculate angles of closest points on ellipse to targets
 * @param initParameters geometric parameters
 * @return array of all initial parameters
 *//*  www  .j  a va  2  s.  c om*/
InitialGuess calcAllInitValues(double[] initParameters) {
    double[] init = new double[n + PARAMETERS];
    for (int i = 0; i < initParameters.length; i++) {
        init[i] = initParameters[i];
    }
    final double a = initParameters[0];
    final double b = initParameters[1];
    final double alpha = initParameters[2];
    final double x = initParameters[3];
    final double y = initParameters[4];
    final double twopi = 2 * Math.PI;
    angleDerivative.setRadii(a, b);
    angleDerivative.setAngle(alpha);

    // work out the angle values for the closest points on ellipse
    final IndexIterator itx = X.getIterator();
    final IndexIterator ity = Y.getIterator();
    int i = PARAMETERS;
    while (itx.hasNext() && ity.hasNext()) {
        final double Xc = X.getElementDoubleAbs(itx.index) - x;
        final double Yc = Y.getElementDoubleAbs(ity.index) - y;

        angleDerivative.setCoordinate(Xc, Yc);
        try {
            // find quadrant to use
            double pa = Math.atan2(Yc, Xc);
            if (pa < 0)
                pa += twopi;
            pa -= alpha;
            final double end;
            final double halfpi = 0.5 * Math.PI;
            pa /= halfpi;
            end = Math.ceil(pa) * halfpi;
            final double angle = solver.solve(100, angleDerivative, end - halfpi, end);
            init[i++] = angle;
        } catch (TooManyEvaluationsException e) {
            throw new IllegalArgumentException("Problem with solver converging as iterations exceed limit");
        } catch (MathIllegalArgumentException e) {
            // cannot happen
        }
    }
    return new InitialGuess(init);
}

From source file:com.alvermont.terraj.planet.project.ConicalProjection.java

/**
 * Carry out the projection//from  w  ww  . j  av a2  s.c  om
 */
public void project() {
    setcolours();

    final int width = getParameters().getProjectionParameters().getWidth();
    final int height = getParameters().getProjectionParameters().getHeight();

    final double lat = getParameters().getProjectionParameters().getLatitudeRadians();
    final double lon = getParameters().getProjectionParameters().getLongitudeRadians();

    final double scale = getParameters().getProjectionParameters().getScale();

    final double hgrid = getParameters().getProjectionParameters().getHgrid();
    final double vgrid = getParameters().getProjectionParameters().getVgrid();

    final boolean doShade = getParameters().getProjectionParameters().isDoShade();

    cacheParameters();

    colours = new short[width][height];
    shades = new short[width][height];

    final double sla = Math.sin(lat);
    final double cla = Math.cos(lat);
    final double slo = Math.sin(lon);
    final double clo = Math.cos(lon);

    depth = (3 * ((int) (log2(scale * height)))) + 6;

    double k1;
    double c;
    double y2;
    double x;
    double y;
    double zz;
    double x1;
    double y1;
    double z1;
    double theta1;
    double theta2;
    double ymin;
    double ymax;
    double cos2;
    int i;
    int j;

    ymin = 2.0;
    ymax = -2.0;

    if (lat > 0) {
        k1 = 1.0 / Math.sin(lat);
        c = k1 * k1;

        y2 = Math.sqrt((c * (1.0 - Math.sin(lat / k1))) / (1.0 + Math.sin(lat / k1)));

        progress.progressStart(height, "Generating Terrain");

        for (j = 0; j < height; ++j) {
            progress.progressStep(j);

            for (i = 0; i < width; ++i) {
                x = ((2.0 * i) - width) / height / scale;
                y = (((2.0 * j) - height) / height / scale) + y2;
                zz = (x * x) + (y * y);

                if (zz == 0.0) {
                    theta1 = 0.0;
                } else {
                    theta1 = k1 * Math.atan2(x, y);
                }

                if ((theta1 < -Math.PI) || (theta1 > Math.PI)) {
                    colours[i][j] = backgroundColour;

                    if (doShade) {
                        shades[i][j] = 255;
                    }
                } else {
                    /* theta1 is longitude */
                    theta1 += (lon - (0.5 * Math.PI));
                    theta2 = k1 * Math.asin((zz - c) / (zz + c));

                    /* theta2 is latitude */
                    if ((theta2 > (0.5 * Math.PI)) || (theta2 < (-0.5 * Math.PI))) {
                        colours[i][j] = backgroundColour;

                        if (doShade) {
                            shades[i][j] = 255;
                        }
                    } else {
                        cos2 = Math.cos(theta2);
                        y = Math.sin(theta2);

                        if (y < ymin) {
                            ymin = y;
                        }

                        if (y > ymax) {
                            ymax = y;
                        }

                        colours[i][j] = (short) planet0(Math.cos(theta1) * cos2, y, -Math.sin(theta1) * cos2);

                        if (doShade) {
                            shades[i][j] = shade;
                        }
                    }
                }
            }
        }

        progress.progressComplete("Terrain Generated");

        if (hgrid != 0.0) {
            /* draw horizontal gridlines */
            for (theta1 = 0.0; theta1 > -90.0; theta1 -= hgrid)
                ;

            for (theta1 = theta1; theta1 < 90.0; theta1 += hgrid) {
                y = Math.sin(Math.toRadians(theta1));

                if ((ymin <= y) && (y <= ymax)) {
                    zz = Math.sqrt((c * (1.0 + Math.sin(Math.toRadians(theta1) / k1)))
                            / (1.0 - Math.sin(Math.toRadians(theta1) / k1)));

                    for (theta2 = -Math.PI + lon; theta2 < (Math.PI + lon); theta2 += (0.5 / width / scale)) {
                        z1 = theta2 - lon;
                        x1 = zz * Math.sin(z1 / k1);
                        y1 = zz * Math.cos(z1 / k1);

                        i = (int) (0.5 * ((height * scale * x1) + width));
                        j = (int) (0.5 * ((height * scale * (y1 - y2)) + height));

                        if ((0 <= i) && (i < width) && (0 <= j) && (j < height)) {
                            colours[i][j] = BLACK;
                        }
                    }
                }
            }
        }

        if (vgrid != 0.0) {
            /* draw vertical gridlines */
            for (theta1 = -0.5 * Math.PI; theta1 < (0.5 * Math.PI); theta1 += (0.5 / width / scale)) {
                y = Math.sin(theta1);

                if ((ymin <= y) && (y <= ymax)) {
                    zz = Math.sqrt((c * (1.0 + Math.sin(theta1 / k1))) / (1.0 - Math.sin(theta1 / k1)));

                    for (theta2 = 0.0; theta2 > (-180.0 + Math.toDegrees(lon)); theta2 -= vgrid)
                        ;

                    for (theta2 = theta2; theta2 < (180.0 + Math.toDegrees(lon)); theta2 += vgrid) {
                        z1 = Math.toRadians(theta2) - lon;
                        x1 = zz * Math.sin(z1 / k1);
                        y1 = zz * Math.cos(z1 / k1);

                        i = (int) (0.5 * ((height * scale * x1) + width));
                        j = (int) (0.5 * ((height * scale * (y1 - y2)) + height));

                        if ((0 <= i) && (i < width) && (0 <= j) && (j < height)) {
                            colours[i][j] = BLACK;
                        }
                    }
                }
            }
        }
    } else {
        k1 = 1.0 / Math.sin(lat);
        c = k1 * k1;
        y2 = Math.sqrt((c * (1.0 - Math.sin(lat / k1))) / (1.0 + Math.sin(lat / k1)));

        progress.progressStart(height, "Generating Terrain");

        for (j = 0; j < height; ++j) {
            progress.progressStep(j);

            for (i = 0; i < width; ++i) {
                x = ((2.0 * i) - width) / height / scale;
                y = (((2.0 * j) - height) / height / scale) - y2;
                zz = (x * x) + (y * y);

                if (zz == 0.0) {
                    theta1 = 0.0;
                } else {
                    theta1 = -k1 * Math.atan2(x, -y);
                }

                if ((theta1 < -Math.PI) || (theta1 > Math.PI)) {
                    colours[i][j] = backgroundColour;

                    if (doShade) {
                        shades[i][j] = 255;
                    }
                } else {
                    /* theta1 is longitude */
                    theta1 += (lon - (0.5 * Math.PI));
                    theta2 = k1 * Math.asin((zz - c) / (zz + c));

                    /* theta2 is latitude */
                    if ((theta2 > (0.5 * Math.PI)) || (theta2 < (-0.5 * Math.PI))) {
                        colours[i][j] = backgroundColour;

                        if (doShade) {
                            shades[i][j] = 255;
                        }
                    } else {
                        cos2 = Math.cos(theta2);
                        y = Math.sin(theta2);

                        if (y < ymin) {
                            ymin = y;
                        }

                        if (y > ymax) {
                            ymax = y;
                        }

                        colours[i][j] = (short) planet0(Math.cos(theta1) * cos2, y, -Math.sin(theta1) * cos2);

                        if (doShade) {
                            shades[i][j] = shade;
                        }
                    }
                }
            }
        }

        progress.progressComplete("Terrain Generated");

        if (hgrid != 0.0) {
            /* draw horizontal gridlines */
            for (theta1 = 0.0; theta1 > -90.0; theta1 -= hgrid)
                ;

            for (theta1 = theta1; theta1 < 90.0; theta1 += hgrid) {
                y = Math.sin(Math.toRadians(theta1));

                if ((ymin <= y) && (y <= ymax)) {
                    zz = Math.sqrt((c * (1.0 + Math.sin(Math.toRadians(theta1) / k1)))
                            / (1.0 - Math.sin(Math.toRadians(theta1) / k1)));

                    for (theta2 = -Math.PI + lon; theta2 < (Math.PI + lon); theta2 += (0.5 / width / scale)) {
                        z1 = theta2 - lon;
                        x1 = -zz * Math.sin(z1 / k1);
                        y1 = -zz * Math.cos(z1 / k1);

                        i = (int) (0.5 * ((height * scale * x1) + width));
                        j = (int) (0.5 * ((height * scale * (y1 + y2)) + height));

                        if ((0 <= i) && (i < width) && (0 <= j) && (j < height)) {
                            colours[i][j] = BLACK;
                        }
                    }
                }
            }
        }

        if (vgrid != 0.0) {
            /* draw vertical gridlines */
            for (theta1 = -0.5 * Math.PI; theta1 < (0.5 * Math.PI); theta1 += (0.5 / width / scale)) {
                y = Math.sin(theta1);

                if ((ymin <= y) && (y <= ymax)) {
                    zz = Math.sqrt((c * (1.0 + Math.sin(theta1 / k1))) / (1.0 - Math.sin(theta1 / k1)));

                    for (theta2 = 0.0; theta2 > (-180.0 + Math.toDegrees(lon)); theta2 -= vgrid)
                        ;

                    for (theta2 = theta2; theta2 < (180.0 + Math.toDegrees(lon)); theta2 += vgrid) {
                        z1 = Math.toRadians(theta2) - lon;
                        x1 = -zz * Math.sin(z1 / k1);
                        y1 = -zz * Math.cos(z1 / k1);

                        i = (int) (0.5 * ((height * scale * x1) + width));
                        j = (int) (0.5 * ((height * scale * (y1 + y2)) + height));

                        if ((0 <= i) && (i < width) && (0 <= j) && (j < height)) {
                            colours[i][j] = BLACK;
                        }
                    }
                }
            }
        }
    }

    if (doShade) {
        smoothshades();
    }

    doOutlining();
}