Example usage for java.lang Math toRadians

List of usage examples for java.lang Math toRadians

Introduction

In this page you can find the example usage for java.lang Math toRadians.

Prototype

public static double toRadians(double angdeg) 

Source Link

Document

Converts an angle measured in degrees to an approximately equivalent angle measured in radians.

Usage

From source file:Main.java

/**
 * From http://stackoverflow.com/a/19498994/423980
 * @return  distance between 2 points, stored as 2 pair location;
 *//*from w ww .  java  2  s. com*/
public static double distanceFrom(double lat1, double lng1, double lat2, double lng2) {
    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));
    double dist = EARTH_RADIOUS * c;
    return Double.valueOf(dist * METER_CONVERSION).floatValue();
}

From source file:Main.java

/**
 * Returns bounding box centered on location
 * /*  www.java2 s . c  o m*/
 * @param loc
 * @param radius, in meters
 * @return long lat long lat
 */
public static double[] getBoundingBoxCoords(Location loc, Double radius) {
    double dY = 360 * radius / EARTH_RADIUS;
    double dX = dY * Math.cos(Math.toRadians(loc.getLatitude()));
    return new double[] { loc.getLongitude() - dX, loc.getLatitude() - dY, loc.getLongitude() + dX,
            loc.getLatitude() + dY };
}

From source file:Main.java

/**
 * Returns the bearing from one point to another.
 * @param latFrom The latitude of the point from
 * @param lonFrom The longitude of the point from
 * @param latTo The latitude of the point to
 * @param lonTo The longitude of the point to
 * @return the bearing from one point to another
 *///from ww  w  . j  a  v a 2s  .  c o  m
private static double bearingTo(double latFrom, double lonFrom, double latTo, double lonTo) {
    double latitude1 = Math.toRadians(latFrom);
    double latitude2 = Math.toRadians(latTo);
    double longDiff = Math.toRadians(lonTo - lonFrom);
    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:Main.java

/**
 * Calculates the position around a center point, depending on the distance
 * from the center, and the angle of the position around the center.
 *
 * @param center//from  w  w w.j  a v a  2  s . co m
 * @param dist
 * @param angle  in degrees, converted to radians internally
 * @return
 */
public static PointF getPosition(PointF center, float dist, float angle) {

    PointF p = new PointF((float) (center.x + dist * Math.cos(Math.toRadians(angle))),
            (float) (center.y + dist * Math.sin(Math.toRadians(angle))));
    return p;
}

From source file:Main.java

/**
 * Computes the distance in meters between two points on Earth.
 *
 * @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 Distance between the two points in meters.
 *///from  w  w w  .  ja v  a 2  s. c o  m
public static double distance(double lat1, double lon1, double lat2, double lon2) {
    double lat1Rad = Math.toRadians(lat1);
    double lat2Rad = Math.toRadians(lat2);
    double deltaLonRad = Math.toRadians(lon2 - lon1);

    return Math.acos(Math.sin(lat1Rad) * Math.sin(lat2Rad)
            + Math.cos(lat1Rad) * Math.cos(lat2Rad) * Math.cos(deltaLonRad)) * EARTH_RADIUS_KM * 1000;
}

From source file:Main.java

/**
 * Computes the distance in kilometers between two points on Earth.
 *
 * @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 Distance between the two points in kilometers.
 *///w  w w .  ja v a2 s . com
public static double distanceKm(double lat1, double lon1, double lat2, double lon2) {
    double lat1Rad = Math.toRadians(lat1);
    double lat2Rad = Math.toRadians(lat2);
    double deltaLonRad = Math.toRadians(lon2 - lon1);

    return Math.acos(Math.sin(lat1Rad) * Math.sin(lat2Rad)
            + Math.cos(lat1Rad) * Math.cos(lat2Rad) * Math.cos(deltaLonRad)) * EARTH_RADIUS_KM;
}

From source file:Main.java

static float computeCircleY(float r, float degrees) {
    return (float) (r * Math.sin(Math.toRadians(degrees)));
}

From source file:Main.java

static float computeCircleX(float r, float degrees) {
    return (float) (r * Math.cos(Math.toRadians(degrees)));
}

From source file:Main.java

public static Point rotatePoint(Point point, Point centerPoint, float rotate) {
    float x = point.x;
    float y = point.y;
    float sinA = (float) Math.sin(Math.toRadians(rotate));
    float cosA = (float) Math.cos(Math.toRadians(rotate));
    float newX = centerPoint.x + (x - centerPoint.x) * cosA - (y - centerPoint.y) * sinA;
    float newY = centerPoint.y + (y - centerPoint.y) * cosA + (x - centerPoint.x) * sinA;
    return new Point((int) newX, (int) newY);
}

From source file:Main.java

/**
 * Special crop of bitmap rotated by not stright angle, in this case the original crop bitmap contains parts
 * beyond the required crop area, this method crops the already cropped and rotated bitmap to the final
 * rectangle.<br>//from   w  w w. j  a v  a2s  . c o m
 * Note: rotating by 0, 90, 180 or 270 degrees doesn't require extra cropping.
 */
private static Bitmap cropForRotatedImage(Bitmap bitmap, float[] points, Rect rect, int degreesRotated,
        boolean fixAspectRatio, int aspectRatioX, int aspectRatioY) {
    if (degreesRotated % 90 != 0) {

        int adjLeft = 0, adjTop = 0, width = 0, height = 0;
        double rads = Math.toRadians(degreesRotated);
        int compareTo = degreesRotated < 90 || (degreesRotated > 180 && degreesRotated < 270) ? rect.left
                : rect.right;
        for (int i = 0; i < points.length; i += 2) {
            if (points[i] >= compareTo - 1 && points[i] <= compareTo + 1) {
                adjLeft = (int) Math.abs(Math.sin(rads) * (rect.bottom - points[i + 1]));
                adjTop = (int) Math.abs(Math.cos(rads) * (points[i + 1] - rect.top));
                width = (int) Math.abs((points[i + 1] - rect.top) / Math.sin(rads));
                height = (int) Math.abs((rect.bottom - points[i + 1]) / Math.cos(rads));
                break;
            }
        }

        rect.set(adjLeft, adjTop, adjLeft + width, adjTop + height);
        if (fixAspectRatio) {
            fixRectForAspectRatio(rect, aspectRatioX, aspectRatioY);
        }

        Bitmap bitmapTmp = bitmap;
        bitmap = Bitmap.createBitmap(bitmap, rect.left, rect.top, rect.width(), rect.height());
        if (bitmapTmp != bitmap) {
            bitmapTmp.recycle();
        }
    }
    return bitmap;
}