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.csa.rstb.gpf.OrientationAngleCorrectionOp.java

/**
 * Compute polarization orientation angle.
 *
 * @param t23Re Real part of the t23 element of coherency matrix
 * @param t22   The t22 element of the coherency matrix
 * @param t33   The t33 element of the coherency matrix
 * @return The polarization orientation angle in radian
 *//*from ww  w .j a  v a  2 s .c  o  m*/
private static double estimateOrientationAngle(final double t23Re, final double t22, final double t33) {
    if (t33 == 0.0) {
        return 0.0;
    }

    double theta = 0.25 * (Math.atan2(2 * t23Re, t33 - t22) + Math.PI);
    if (theta > PI4) {
        theta -= PI2;
    }
    return theta;
}

From source file:com.seekret.data.flickr.FlickrRequestHandler.java

/**
 * This method generates satelites around a given spot. Attention, using
 * this method for requests to flickr, we can get problems with write/read
 * operations and problems with the spot size ( > 1MB) concerning memcache.
 * //w  ww.j av a  2  s .  c om
 * @param spot
 *            given Spot.
 * @param allPoints
 *            Map of Points with spotradius.
 */
@SuppressWarnings("unused")
private void createSatelitesArroundGivenSpot(Spot spot, HashMap<LatLng, Double> allPoints) {
    for (int degree = 0; degree < 316; degree += 45) {
        double d = spot.getSpotRadiusInKm() + (spot.getSpotRadiusInKm() / 2.0);
        System.out.println("Distance = " + d);
        double dist = d / 6371.0;
        double brng = Math.toRadians(degree);
        double lat1 = Math.toRadians(spot.getLatitude());
        double lon1 = Math.toRadians(spot.getLongitude());

        double lat2 = Math
                .asin(Math.sin(lat1) * Math.cos(dist) + Math.cos(lat1) * Math.sin(dist) * Math.cos(brng));
        double a = Math.atan2(Math.sin(brng) * Math.sin(dist) * Math.cos(lat1),
                Math.cos(dist) - Math.sin(lat1) * Math.sin(lat2));
        System.out.println("a = " + a);
        double lon2 = lon1 + a;

        lon2 = (lon2 + 3 * Math.PI) % (2 * Math.PI) - Math.PI;

        double latitude2 = Math.toDegrees(lat2);
        double longitude2 = Math.toDegrees(lon2);
        System.out.println("Latitude = " + latitude2 + "\nLongitude = " + longitude2);

        LatLng origin = new LatLng(spot.getLatitude(), spot.getLongitude());
        LatLng newPoint = new LatLng(latitude2, longitude2);

        log.log(Level.INFO,
                "DISTANCE TO CENTER " + (LatLngTool.distance(origin, newPoint, LengthUnit.KILOMETER)));
        allPoints.put(newPoint, Double.valueOf(spot.getSpotRadiusInKm() / 2.0));
    }
}

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

public static BufferedImage compensateLensDistortion2(BufferedImage img, double k) {
    if (img == null) {
        return null;
    }/*  w ww.  j  a v a  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;

    double rdmax = Math.sqrt((w - x0) * (w - x0) + (h - y0) * (h - y0));
    double rumax = rdmax * (1 + k * rdmax * rdmax);
    //System.out.println("rdmax="+rdmax);
    //System.out.println("rumax="+rumax);
    double thetamax = Math.atan2(h - y0, w - x0);

    int xmax = (int) Math.round(rumax * Math.cos(thetamax)) * 2;
    int ymax = (int) Math.round(rumax * Math.sin(thetamax)) * 2;
    //System.out.println("xmax="+xmax);
    //System.out.println("ymax="+ymax);

    BufferedImage out = new BufferedImage(xmax, ymax, img.getType());

    int newx0 = (int) Math.floor(xmax / 2) + 1;
    int newy0 = (int) Math.floor(ymax / 2) + 1;

    int dx = (int) ((xmax - w) / 2) - 1;
    int dy = (int) ((ymax - h) / 2) - 1;

    for (int x = 0; x < xmax; x++) {
        for (int y = 0; y < ymax; y++) {
            ru = Math.sqrt((x - newx0) * (x - newx0) + (y - newy0) * (y - newy0));
            theta = Math.atan2(y - newy0, x - newx0);
            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:PathLength.java

/**
 * Returns the slope of the path at the specified length.
 * @param length The length along the path
 * @return the angle in radians, in the range [-{@link Math#PI},
 *         {@link Math#PI}]./*  w  w  w .ja  va 2s .  co  m*/
 */
public float angleAtLength(float length) {
    int upperIndex = findUpperIndex(length);
    if (upperIndex == -1) {
        // Length is off the end of the path.
        return 0f;
    }

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

    if (upperIndex == 0) {
        // Length was probably zero, so return the angle between the first
        // and second segments.
        upperIndex = 1;
    }

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

    // Compute the slope.
    return (float) Math.atan2(upper.getY() - lower.getY(), upper.getX() - lower.getX());
}

From source file:com.projecttango.examples.java.floorplanreconstruction.FloorPlanReconstructionActivity.java

/**
 * Calculates the rotation around Y (yaw) from the given quaternion.
 *//*from   w  w w. ja  v  a 2 s .  c  om*/
private static float yRotationFromQuaternion(float x, float y, float z, float w) {
    return (float) Math.atan2(2 * (w * y - x * z), w * (w + x) - y * (z + y));
}

From source file:edu.umich.robot.gp.Gamepad.java

private void update(final Controller controller) {
    double left = 0;
    double right = 0;
    double angularVelocity = 0;
    double linearVelocity = 0;

    switch (gpInputScheme) {
    case JOY_MOTOR:
        // this should not be linear, it is difficult to precisely
        // control
        double fwd = -1 * ry; // +1 = forward, -1 = back
        double lr = -1 * rx; // +1 = left, -1 = right

        left = fwd - lr;/*  ww  w.j a  v a  2  s . co m*/
        right = fwd + lr;

        double max = Math.max(Math.abs(left), Math.abs(right));
        if (max > 1) {
            left /= max;
            right /= max;
        }

        if (slow) {
            left *= 0.5;
            right *= 0.5;
        }
        controller.fireGamepadControlEvent(new DriveThrottleEvent(left, right));
        break;

    case TANK:
        left = ly * -1;
        right = ry * -1;

        if (slow) {
            left *= 0.5;
            right *= 0.5;
        }
        controller.fireGamepadControlEvent(new DriveThrottleEvent(left, right));
        break;

    case JOY_VELOCITIES:
        angularVelocity = rx * -1;
        linearVelocity = ry * -1;
        controller.fireGamepadControlEvent(new DriveLinearEvent(linearVelocity));
        controller.fireGamepadControlEvent(new DriveAngularEvent(angularVelocity));
        break;

    case GAS_AND_WHEEL:
        linearVelocity = ly * -1;
        if (LinAlg.magnitude(new double[] { rx, ry }) < 0.5) {
            lastEvent = new DriveLinearEvent(linearVelocity);
            controller.fireGamepadControlEvent(new DriveAngularEvent(0));
        } else {
            double heading = Math.atan2(ry * -1, rx);
            lastEvent = new DriveHeadingAndLinearEvent(heading, linearVelocity);
        }
        controller.fireGamepadControlEvent(lastEvent);
        break;
    }
}

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

/**
 * // Given starting point GLON1,GLAT1, head1 = initial heading,and distance
 * // in meters, calculate destination GLON2,GLAT2, and head2=initial heading
 * // from destination to starting point
 * <p>/*from w w w. j  av  a  2 s. c  o m*/
 * // Input:
 * // lon1:   longitude
 * // lat1:   latitude
 * // dist:   distance in m
 * // head1:   azimuth in degree measured in the diretion North east south west
 * <p>
 * // Output:
 * // GLON2:   longitude
 * // GLAT2:   latitude
 * // head2:   azimuth in degree measured in the direction North east south west
 * //         from (GLON2,GLAT2) to (GLON1, GLAT1)
 *
 * @param lon1
 * @param lat1
 * @param dist
 * @param head1
 * @return
 */
public static LatLonHeading vincenty_direct(double lon1, double lat1, final double dist, final double head1) {

    final LatLonHeading pos = new LatLonHeading();

    lat1 *= Constants.DTOR;
    lon1 *= Constants.DTOR;
    final double FAZ = head1 * Constants.DTOR;

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

    // equatorial radius
    final double R = 1.0 - F;
    double TU = R * FastMath.tan(lat1);
    final double SF = FastMath.sin(FAZ);
    final double CF = FastMath.cos(FAZ);
    double BAZ = 0.0;
    if (CF != 0.0)
        BAZ = Math.atan2(TU, CF) * 2.0;
    final double CU = 1.0 / Math.sqrt(TU * TU + 1.0);
    final double SU = TU * CU;
    final double SA = CU * SF;
    final double C2A = -SA * SA + 1.0;
    double X = Math.sqrt((1.0 / R / R - 1.0) * C2A + 1.0) + 1.0;
    X = (X - 2.0) / X;
    double C = 1.0 - X;
    C = (X * X / 4.0 + 1) / C;
    double D = (0.375 * X * X - 1.0) * X;
    TU = dist / R / WGS84.a / C;
    double Y = TU;

    double SY, CY, CZ, E;
    do {
        SY = FastMath.sin(Y);
        CY = FastMath.cos(Y);
        CZ = FastMath.cos(BAZ + Y);
        E = CZ * CZ * 2.0 - 1.0;
        C = Y;
        X = E * CY;
        Y = E + E - 1.0;
        Y = (((SY * SY * 4.0 - 3.0) * Y * CZ * D / 6.0 + X) * D / 4.0 - CZ) * SY * D + TU;
    } while (Math.abs(Y - C) > EPS);

    BAZ = CU * CY * CF - SU * SY;
    C = R * Math.sqrt(SA * SA + BAZ * BAZ);
    D = SU * CY + CU * SY * CF;
    pos.lat = Math.atan2(D, C);
    C = CU * CY - SU * SY * CF;
    X = Math.atan2(SY * SF, C);
    C = ((-3.0 * C2A + 4.0) * F + 4.0) * C2A * F / 16.0;
    D = ((E * CY * C + CZ) * SY * C + Y) * SA;
    pos.lon = lon1 + X - (1.0 - C) * D * F;
    BAZ = Math.atan2(SA, BAZ) + Constants.PI;

    pos.lon *= Constants.RTOD;
    pos.lat *= Constants.RTOD;
    pos.heading = BAZ * Constants.RTOD;

    while (pos.heading < 0)
        pos.heading += 360;

    return pos;
}

From source file:com.pseudosurface.levels.template.SimulatorActivity.java

@Override
public boolean onTouchEvent(MotionEvent event) {

    float x = event.getX();
    float y = event.getY();

    // Remember some things for zooming
    PointF start = new PointF();
    PointF mid = new PointF();
    switch (event.getAction() & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
        start.set(event.getX(), event.getY());
        //          Log.d(TAG, "mode=DRAG");

        break;//from w  w  w .j  a va 2s . c  om
    case MotionEvent.ACTION_POINTER_DOWN:
        oldDist = spacing(event);
        if (oldDist > 10f) {
            midPoint(mid, event);
            mode = ZOOM;
            //             Log.d(TAG, "mode=ZOOM");
        }
        break;
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_POINTER_UP:
        mode = NONE;
        oldDist = -1;
        //          Log.d(TAG, "mode=NONE");
        break;
    case MotionEvent.ACTION_MOVE:

        if (mode == ZOOM) {
            float newDist = spacing(event);
            if (newDist > simulation.minZoom && oldDist != -1) {
                simulation.zoom -= (newDist - oldDist) * .05;
            }
            oldDist = newDist;
        } else {
            float dz = x - mPreviousX;
            float dy = y - mPreviousY;

            float r = (float) Math.sqrt(dz * dz + dy * dy);

            double theta = Math.atan2(dy, dz) + Math.PI + SimulatorActivity.angleLR;

            try {
                simulation.world.objects[simulation.world.getPlayerIndex()].omega[0] += .05f * r
                        * Math.cos(theta);
                simulation.world.objects[simulation.world.getPlayerIndex()].omega[2] += .05f * r
                        * Math.sin(theta);
            } catch (Exception e) {
            }

            //MainActivity.angleLR += dx*.01;
            //simulation.zoom += dy*.01;
        }
        break;
    }

    mPreviousX = x;
    mPreviousY = y;

    gestureDetector.onTouchEvent(event);

    return true; // indicate event was handled
}

From source file:org.csa.rstb.polarimetric.gpf.OrientationAngleCorrectionOp.java

/**
 * Compute polarization orientation angle.
 *
 * @param t23Re Real part of the t23 element of coherency matrix
 * @param t22   The t22 element of the coherency matrix
 * @param t33   The t33 element of the coherency matrix
 * @return The polarization orientation angle in radian
 *//*from  w ww . j  a  v a2  s  .c o  m*/
private static double estimateOrientationAngle(final double t23Re, final double t22, final double t33) {
    if (t33 == 0.0) {
        return 0.0;
    }

    double theta = 0.25 * (Math.atan2(2 * t23Re, t33 - t22) + Constants.PI);
    if (theta > PI4) {
        theta -= PI2;
    }
    return theta;
}

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

/**
 // Given starting point GLON1,GLAT1, head1 = initial heading,and distance
 // in meters, calculate destination GLON2,GLAT2, and head2=initial heading
 // from destination to starting point/*from   w w  w  .ja  v a 2 s .  co  m*/
        
 // Input:
 // lon1:   longitude
 // lat1:   latitude
 // dist:   distance in m
 // head1:   azimuth in degree measured in the diretion North east south west
        
 // Output:
 // GLON2:   longitude
 // GLAT2:   latitude
 // head2:   azimuth in degree measured in the direction North east south west
 //         from (GLON2,GLAT2) to (GLON1, GLAT1)
 * @param lon1
 * @param lat1
 * @param dist
 * @param head1
 * @return
 */
public static LatLonHeading vincenty_direct(double lon1, double lat1, final double dist, final double head1) {

    final LatLonHeading pos = new LatLonHeading();

    lat1 *= org.esa.beam.util.math.MathUtils.DTOR;
    lon1 *= org.esa.beam.util.math.MathUtils.DTOR;
    final double FAZ = head1 * org.esa.beam.util.math.MathUtils.DTOR;

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

    // equatorial radius
    final double R = 1.0 - F;
    double TU = R * Math.tan(lat1);
    final double SF = Math.sin(FAZ);
    final double CF = Math.cos(FAZ);
    double BAZ = 0.0;
    if (CF != 0.0)
        BAZ = Math.atan2(TU, CF) * 2.0;
    final double CU = 1.0 / Math.sqrt(TU * TU + 1.0);
    final double SU = TU * CU;
    final double SA = CU * SF;
    final double C2A = -SA * SA + 1.0;
    double X = Math.sqrt((1.0 / R / R - 1.0) * C2A + 1.0) + 1.0;
    X = (X - 2.0) / X;
    double C = 1.0 - X;
    C = (X * X / 4.0 + 1) / C;
    double D = (0.375 * X * X - 1.0) * X;
    TU = dist / R / WGS84.a / C;
    double Y = TU;

    double SY, CY, CZ, E;
    do {
        SY = Math.sin(Y);
        CY = Math.cos(Y);
        CZ = Math.cos(BAZ + Y);
        E = CZ * CZ * 2.0 - 1.0;
        C = Y;
        X = E * CY;
        Y = E + E - 1.0;
        Y = (((SY * SY * 4.0 - 3.0) * Y * CZ * D / 6.0 + X) * D / 4.0 - CZ) * SY * D + TU;
    } while (Math.abs(Y - C) > EPS);

    BAZ = CU * CY * CF - SU * SY;
    C = R * Math.sqrt(SA * SA + BAZ * BAZ);
    D = SU * CY + CU * SY * CF;
    pos.lat = Math.atan2(D, C);
    C = CU * CY - SU * SY * CF;
    X = Math.atan2(SY * SF, C);
    C = ((-3.0 * C2A + 4.0) * F + 4.0) * C2A * F / 16.0;
    D = ((E * CY * C + CZ) * SY * C + Y) * SA;
    pos.lon = lon1 + X - (1.0 - C) * D * F;
    BAZ = Math.atan2(SA, BAZ) + Math.PI;

    pos.lon *= org.esa.beam.util.math.MathUtils.RTOD;
    pos.lat *= org.esa.beam.util.math.MathUtils.RTOD;
    pos.heading = BAZ * org.esa.beam.util.math.MathUtils.RTOD;

    while (pos.heading < 0)
        pos.heading += 360;

    return pos;
}