Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.lang.*;
- import java.io.*;
- import static org.junit.Assert.assertEquals;
- import static java.lang.Math.*;
- /** A point represented by (latitude, longitude). */
- class LatLng {
- public final double latitude;
- public final double longitude;
- /**
- * Constructor.
- * @param latitude, range [-90, 90]
- * @param longitude, range [-180, 180]
- */
- public LatLng(double latitude, double longitude) {
- this.latitude = latitude;
- this.longitude = longitude;
- }
- @Override
- public String toString() {
- return "(" + latitude + "," + longitude + ")";
- }
- }
- /* Name of the class has to be "Main" only if the class is public. */
- class Ideone
- {
- static final double EARTH_RADIUS = 6371009;
- /**
- * Returns the LatLng resulting from moving a distance from an origin
- * in the specified heading (expressed in degrees clockwise from north).
- *
- * @param from The LatLng from which to start.
- * @param distance The distance to travel.
- * @param heading The heading in degrees clockwise from north.
- */
- public static LatLng computeOffset(LatLng from, double distance, double heading) {
- distance /= EARTH_RADIUS;
- heading = toRadians(heading);
- // http://williams.best.vwh.net/avform.htm#LL
- double fromLat = toRadians(from.latitude);
- double fromLng = toRadians(from.longitude);
- double cosDistance = cos(distance);
- double sinDistance = sin(distance);
- double sinFromLat = sin(fromLat);
- double cosFromLat = cos(fromLat);
- double sinLat = cosDistance * sinFromLat + sinDistance * cosFromLat * cos(heading);
- double dLng = atan2(
- sinDistance * cosFromLat * sin(heading),
- cosDistance - sinFromLat * sinLat);
- return new LatLng(toDegrees(asin(sinLat)), toDegrees(fromLng + dLng));
- }
- private static final LatLng up = new LatLng(90, 0);
- private static final LatLng down = new LatLng(-90, 0);
- private static final LatLng front = new LatLng(0, 0);
- private static final LatLng right = new LatLng(0, 90);
- private static final LatLng back = new LatLng(0, -180);
- private static final LatLng left = new LatLng(0, -90);
- /**
- * Tests for approximate equality.
- */
- private static void expectLatLngApproxEquals(LatLng actual, LatLng expected) {
- double cosLat = Math.cos(Math.toRadians(actual.latitude));
- if (Math.abs(actual.latitude - expected.latitude) > 1e-6 ||
- Math.abs(cosLat * actual.longitude - cosLat * expected.longitude) > 1e-6)
- {
- System.out.print("[No] " + actual.toString() + " => " + expected.toString() + "\n");
- System.out.print("[ ] " + actual.latitude + " => " + expected.latitude + "\n");
- System.out.print("[ ] " + (cosLat * actual.longitude) + " => " + (cosLat * expected.longitude) + "\n");
- }else {
- System.out.print("[Ok] " + actual.toString() + " => " + expected.toString() + "\n");
- }
- }
- public static void main (String[] args) throws java.lang.Exception
- {
- // expectLatLngApproxEquals(front, computeOffset(front, 0, 0));
- // expectLatLngApproxEquals(up, computeOffset(front, Math.PI * EARTH_RADIUS / 2, 0));
- // expectLatLngApproxEquals(down, computeOffset(front, Math.PI * EARTH_RADIUS / 2, 180));
- // expectLatLngApproxEquals(left, computeOffset(front, Math.PI * EARTH_RADIUS / 2, -90));
- // expectLatLngApproxEquals(right, computeOffset(front, Math.PI * EARTH_RADIUS / 2, 90));
- expectLatLngApproxEquals(back, computeOffset(front, Math.PI * EARTH_RADIUS, 0));
- expectLatLngApproxEquals(back, computeOffset(front, Math.PI * EARTH_RADIUS, 90));
- }
- }
Advertisement