List of usage examples for java.lang Math atan2
@HotSpotIntrinsicCandidate public static double atan2(double y, double x)
From source file:Main.java
/** * Computes the bearing in degrees between two points on Earth. * //from ww w .j av a 2 s. c om * @param lat1 Latitude of the first point * @param lon1 Longitude of the first point * @param lat2 Latitude of the second point * @param lon2 Longitude of the second point * @return Bearing between the two points in degrees. A value of 0 means due * north. */ public static double bearing(double lat1, double lon1, double lat2, double lon2) { double lat1Rad = Math.toRadians(lat1); double lat2Rad = Math.toRadians(lat2); double deltaLonRad = Math.toRadians(lon2 - lon1); double y = Math.sin(deltaLonRad) * Math.cos(lat2Rad); double x = Math.cos(lat1Rad) * Math.sin(lat2Rad) - Math.sin(lat1Rad) * Math.cos(lat2Rad) * Math.cos(deltaLonRad); return radToBearing(Math.atan2(y, x)); }
From source file:Main.java
/** * Waypoint projection using haversine formula * http://en.wikipedia.org/wiki/Haversine_formula See discussion here for * further information: http://www.movable-type.co.uk/scripts/latlong.html *//* w w w. java 2 s . c o m*/ public static double[] project(double distance, double bearing, double startLat, double startLon) { double distanceRad = distance / EARTH_RADIUS_KM; double bearingRad = Math.toRadians(bearing); double startLatRad = Math.toRadians(startLat); double startLonRad = Math.toRadians(startLon); double endLat = Math.asin(Math.sin(startLatRad) * Math.cos(distanceRad) + Math.cos(startLatRad) * Math.sin(distanceRad) * Math.cos(bearingRad)); double endLon = startLonRad + Math.atan2(Math.sin(bearingRad) * Math.sin(distanceRad) * Math.cos(startLatRad), Math.cos(distanceRad) - Math.sin(startLatRad) * Math.sin(endLat)); // Adjust projections crossing the 180th meridian: double endLonDeg = Math.toDegrees(endLon); if (endLonDeg > 180 || endLonDeg < -180) { endLonDeg = endLonDeg % 360; // Just in case we circle the earth // more than once. if (endLonDeg > 180) { endLonDeg = endLonDeg - 360; } else if (endLonDeg < -180) { endLonDeg = endLonDeg + 360; } } double[] endCoords = new double[] { Math.toDegrees(endLat), endLonDeg }; return endCoords; }
From source file:Main.java
/** * Returns the distance between two given locations in meters. * * @param loc1 First location object/*from www . ja v a 2 s . c o m*/ * @param loc2 Second location object * @return distance between Loc1 & Loc2 in meters. */ public static float getDistance(Location loc1, Location loc2) { double lat1 = loc1.getLatitude(); double lng1 = loc1.getLongitude(); double lat2 = loc2.getLatitude(); double lng2 = loc2.getLongitude(); double earthRad = 6371; //kilometers double dLatitude = Math.toRadians(lat2 - lat1); double dLongitude = Math.toRadians(lng2 - lng1); double a = Math.sin(dLatitude / 2) * Math.sin(dLatitude / 2) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * Math.sin(dLongitude / 2) * Math.sin(dLongitude / 2); double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); float dist = (float) (earthRad * c); dist = dist * MILES_TO_METER_CONVERSION; return dist; }
From source file:com.opengamma.analytics.math.ComplexMathUtils.java
public static double arg(final ComplexNumber z) { ArgumentChecker.notNull(z, "z"); return Math.atan2(z.getImaginary(), z.getReal()); }
From source file:Main.java
/** * Function: arcToCurves//from w w w . j a va 2 s. c o m * * Converts the given arc to a series of curves. */ public static double[] arcToCurves(double x0, double y0, double r1, double r2, double angle, double largeArcFlag, double sweepFlag, double x, double y) { x -= x0; y -= y0; if (r1 == 0 || r2 == 0) { return new double[0]; } double fS = sweepFlag; double psai = angle; r1 = Math.abs(r1); r2 = Math.abs(r2); double ctx = -x / 2; double cty = -y / 2; double cpsi = Math.cos(psai * Math.PI / 180); double spsi = Math.sin(psai * Math.PI / 180); double rxd = cpsi * ctx + spsi * cty; double ryd = -1 * spsi * ctx + cpsi * cty; double rxdd = rxd * rxd; double rydd = ryd * ryd; double r1x = r1 * r1; double r2y = r2 * r2; double lamda = rxdd / r1x + rydd / r2y; double sds; if (lamda > 1) { r1 = Math.sqrt(lamda) * r1; r2 = Math.sqrt(lamda) * r2; sds = 0; } else { double seif = 1; if (largeArcFlag == fS) { seif = -1; } sds = seif * Math.sqrt((r1x * r2y - r1x * rydd - r2y * rxdd) / (r1x * rydd + r2y * rxdd)); } double txd = sds * r1 * ryd / r2; double tyd = -1 * sds * r2 * rxd / r1; double tx = cpsi * txd - spsi * tyd + x / 2; double ty = spsi * txd + cpsi * tyd + y / 2; double rad = Math.atan2((ryd - tyd) / r2, (rxd - txd) / r1) - Math.atan2(0, 1); double s1 = (rad >= 0) ? rad : 2 * Math.PI + rad; rad = Math.atan2((-ryd - tyd) / r2, (-rxd - txd) / r1) - Math.atan2((ryd - tyd) / r2, (rxd - txd) / r1); double dr = (rad >= 0) ? rad : 2 * Math.PI + rad; if (fS == 0 && dr > 0) { dr -= 2 * Math.PI; } else if (fS != 0 && dr < 0) { dr += 2 * Math.PI; } double sse = dr * 2 / Math.PI; int seg = (int) Math.ceil(sse < 0 ? -1 * sse : sse); double segr = dr / seg; double t = 8 / 3 * Math.sin(segr / 4) * Math.sin(segr / 4) / Math.sin(segr / 2); double cpsir1 = cpsi * r1; double cpsir2 = cpsi * r2; double spsir1 = spsi * r1; double spsir2 = spsi * r2; double mc = Math.cos(s1); double ms = Math.sin(s1); double x2 = -t * (cpsir1 * ms + spsir2 * mc); double y2 = -t * (spsir1 * ms - cpsir2 * mc); double x3 = 0; double y3 = 0; double[] result = new double[seg * 6]; for (int n = 0; n < seg; ++n) { s1 += segr; mc = Math.cos(s1); ms = Math.sin(s1); x3 = cpsir1 * mc - spsir2 * ms + tx; y3 = spsir1 * mc + cpsir2 * ms + ty; double dx = -t * (cpsir1 * ms + spsir2 * mc); double dy = -t * (spsir1 * ms - cpsir2 * mc); // CurveTo updates x0, y0 so need to restore it int index = n * 6; result[index] = x2 + x0; result[index + 1] = y2 + y0; result[index + 2] = x3 - dx + x0; result[index + 3] = y3 - dy + y0; result[index + 4] = x3 + x0; result[index + 5] = y3 + y0; x2 = x3 + dx; y2 = y3 + dy; } return result; }
From source file:Main.java
/** * Gets the relative bearing from one geographical coordinate to another. * * @param latitude1 the latitude of the source point * @param longitude1 the longitude of the source point * @param latitude2 the latitude of the destination point * @param longitude2 the longitude of the destination point * @return the relative bearing from point 1 to point 2, in degrees. The result is guaranteed * to fall in the range 0-360/*from w w w . j av a 2 s.c o m*/ */ public static float getBearing(double latitude1, double longitude1, double latitude2, double longitude2) { latitude1 = Math.toRadians(latitude1); longitude1 = Math.toRadians(longitude1); latitude2 = Math.toRadians(latitude2); longitude2 = Math.toRadians(longitude2); double dLon = longitude2 - longitude1; double y = Math.sin(dLon) * Math.cos(latitude2); double x = Math.cos(latitude1) * Math.sin(latitude2) - Math.sin(latitude1) * Math.cos(latitude2) * Math.cos(dLon); double bearing = Math.atan2(y, x); return mod((float) Math.toDegrees(bearing), 360.0f); }
From source file:Main.java
public static double calculateDistance(double lat1, double lng1, double lat2, double lng2) { int MAXITERS = 20; // Convert lat/long to radians lat1 *= Math.PI / 180.0;/*from w w w.j a v a 2 s . co m*/ lat2 *= Math.PI / 180.0; lng1 *= Math.PI / 180.0; lng2 *= Math.PI / 180.0; double a = 6378137.0; // WGS84 major axis double b = 6356752.3142; // WGS84 semi-major axis double f = (a - b) / a; double aSqMinusBSqOverBSq = (a * a - b * b) / (b * b); double L = lng2 - lng1; double A = 0.0; double U1 = Math.atan((1.0 - f) * Math.tan(lat1)); double U2 = Math.atan((1.0 - f) * Math.tan(lat2)); double cosU1 = Math.cos(U1); double cosU2 = Math.cos(U2); double sinU1 = Math.sin(U1); double sinU2 = Math.sin(U2); double cosU1cosU2 = cosU1 * cosU2; double sinU1sinU2 = sinU1 * sinU2; double sigma = 0.0; double deltaSigma = 0.0; double cosSqAlpha = 0.0; double cos2SM = 0.0; double cosSigma = 0.0; double sinSigma = 0.0; double cosLambda = 0.0; double sinLambda = 0.0; double lambda = L; // initial guess for (int iter = 0; iter < MAXITERS; iter++) { double lambdaOrig = lambda; cosLambda = Math.cos(lambda); sinLambda = Math.sin(lambda); double t1 = cosU2 * sinLambda; double t2 = cosU1 * sinU2 - sinU1 * cosU2 * cosLambda; double sinSqSigma = t1 * t1 + t2 * t2; // (14) sinSigma = Math.sqrt(sinSqSigma); cosSigma = sinU1sinU2 + cosU1cosU2 * cosLambda; // (15) sigma = Math.atan2(sinSigma, cosSigma); // (16) double sinAlpha = (sinSigma == 0) ? 0.0 : cosU1cosU2 * sinLambda / sinSigma; // (17) cosSqAlpha = 1.0 - sinAlpha * sinAlpha; cos2SM = (cosSqAlpha == 0) ? 0.0 : cosSigma - 2.0 * sinU1sinU2 / cosSqAlpha; // (18) double uSquared = cosSqAlpha * aSqMinusBSqOverBSq; // defn A = 1 + (uSquared / 16384.0) * // (3) (4096.0 + uSquared * (-768 + uSquared * (320.0 - 175.0 * uSquared))); double B = (uSquared / 1024.0) * // (4) (256.0 + uSquared * (-128.0 + uSquared * (74.0 - 47.0 * uSquared))); double C = (f / 16.0) * cosSqAlpha * (4.0 + f * (4.0 - 3.0 * cosSqAlpha)); // (10) double cos2SMSq = cos2SM * cos2SM; deltaSigma = B * sinSigma * // (6) (cos2SM + (B / 4.0) * (cosSigma * (-1.0 + 2.0 * cos2SMSq) - (B / 6.0) * cos2SM * (-3.0 + 4.0 * sinSigma * sinSigma) * (-3.0 + 4.0 * cos2SMSq))); lambda = L + (1.0 - C) * f * sinAlpha * (sigma + C * sinSigma * (cos2SM + C * cosSigma * (-1.0 + 2.0 * cos2SM * cos2SM))); // (11) double delta = (lambda - lambdaOrig) / lambda; if (Math.abs(delta) < 1.0e-12) { break; } } float distance = (float) (b * A * (sigma - deltaSigma)); return distance; }
From source file:Main.java
/** * Gets the great circle distance in kilometers between two geographical points, using * the <a href="http://en.wikipedia.org/wiki/Haversine_formula">haversine formula</a>. * * @param latitude1 the latitude of the first point * @param longitude1 the longitude of the first point * @param latitude2 the latitude of the second point * @param longitude2 the longitude of the second point * @return the distance, in kilometers, between the two points *///from www . j a v a 2s .c om public static float getDistance(double latitude1, double longitude1, double latitude2, double longitude2) { double dLat = Math.toRadians(latitude2 - latitude1); double dLon = Math.toRadians(longitude2 - longitude1); double lat1 = Math.toRadians(latitude1); double lat2 = Math.toRadians(latitude2); double sqrtHaversineLat = Math.sin(dLat / 2); double sqrtHaversineLon = Math.sin(dLon / 2); double a = sqrtHaversineLat * sqrtHaversineLat + sqrtHaversineLon * sqrtHaversineLon * Math.cos(lat1) * Math.cos(lat2); double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); return (float) (EARTH_RADIUS_KM * c); }
From source file:org.apache.usergrid.client.QueryTestCase.java
public static float distFrom(float lat1, float lng1, float lat2, float lng2) { double earthRadius = 6371000; //meters 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)); return (float) (earthRadius * c); }
From source file:org.gearvrf.keyboard.util.Util.java
public static float getXRotationAngle(GVRSceneObject rotatingObject, GVRSceneObject targetObject) { float angle = (float) Math.toDegrees(Math.atan2( targetObject.getTransform().getPositionY() - rotatingObject.getTransform().getPositionY(), targetObject.getTransform().getPositionZ() - rotatingObject.getTransform().getPositionZ())); if (rotatingObject.getTransform().getPositionZ() < 0) { if (rotatingObject.getTransform().getPositionY() > 10) { angle = angle + 90;/*from ww w. ja v a 2s . c om*/ } else if (rotatingObject.getTransform().getPositionY() < 0) { angle = angle - 90; } } else { angle = angle - 180; } return angle; }