Gistrec

cpp-geometry-library java test

Jun 20th, 2021
676
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. import static org.junit.Assert.assertEquals;
  6.  
  7. import static java.lang.Math.*;
  8.  
  9. /** A point represented by (latitude, longitude). */
  10. class LatLng {
  11.     public final double latitude;
  12.     public final double longitude;
  13.  
  14.     /**
  15.      * Constructor.
  16.      * @param latitude, range [-90, 90]
  17.      * @param longitude, range [-180, 180]
  18.      */
  19.     public LatLng(double latitude, double longitude) {
  20.         this.latitude = latitude;
  21.         this.longitude = longitude;
  22.     }
  23.  
  24.     @Override
  25.     public String toString() {
  26.         return "(" + latitude + "," + longitude + ")";
  27.     }
  28. }
  29.  
  30. /* Name of the class has to be "Main" only if the class is public. */
  31. class Ideone
  32. {
  33.     static final double EARTH_RADIUS = 6371009;
  34.  
  35.     /**
  36.      * Returns the LatLng resulting from moving a distance from an origin
  37.      * in the specified heading (expressed in degrees clockwise from north).
  38.      *
  39.      * @param from     The LatLng from which to start.
  40.      * @param distance The distance to travel.
  41.      * @param heading  The heading in degrees clockwise from north.
  42.      */
  43.     public static LatLng computeOffset(LatLng from, double distance, double heading) {
  44.         distance /= EARTH_RADIUS;
  45.         heading = toRadians(heading);
  46.         // http://williams.best.vwh.net/avform.htm#LL
  47.         double fromLat = toRadians(from.latitude);
  48.         double fromLng = toRadians(from.longitude);
  49.         double cosDistance = cos(distance);
  50.         double sinDistance = sin(distance);
  51.         double sinFromLat = sin(fromLat);
  52.         double cosFromLat = cos(fromLat);
  53.         double sinLat = cosDistance * sinFromLat + sinDistance * cosFromLat * cos(heading);
  54.         double dLng = atan2(
  55.                 sinDistance * cosFromLat * sin(heading),
  56.                 cosDistance - sinFromLat * sinLat);
  57.         return new LatLng(toDegrees(asin(sinLat)), toDegrees(fromLng + dLng));
  58.     }
  59.  
  60.     private static final LatLng up = new LatLng(90, 0);
  61.     private static final LatLng down = new LatLng(-90, 0);
  62.     private static final LatLng front = new LatLng(0, 0);
  63.     private static final LatLng right = new LatLng(0, 90);
  64.     private static final LatLng back = new LatLng(0, -180);
  65.     private static final LatLng left = new LatLng(0, -90);
  66.  
  67.     /**
  68.      * Tests for approximate equality.
  69.      */
  70.     private static void expectLatLngApproxEquals(LatLng actual, LatLng expected) {
  71.         double cosLat = Math.cos(Math.toRadians(actual.latitude));
  72.         if (Math.abs(actual.latitude - expected.latitude) > 1e-6 ||
  73.             Math.abs(cosLat * actual.longitude - cosLat * expected.longitude) > 1e-6)
  74.         {
  75.             System.out.print("[No] " + actual.toString() + " => " + expected.toString() + "\n");
  76.             System.out.print("[  ] " + actual.latitude + " => " + expected.latitude + "\n");
  77.             System.out.print("[  ] " + (cosLat * actual.longitude) + " => " + (cosLat * expected.longitude) + "\n");
  78.         }else {
  79.             System.out.print("[Ok] " + actual.toString() + " => " + expected.toString() + "\n");
  80.         }
  81.     }
  82.  
  83.     public static void main (String[] args) throws java.lang.Exception
  84.     {
  85.         // expectLatLngApproxEquals(front, computeOffset(front, 0, 0));
  86.         // expectLatLngApproxEquals(up, computeOffset(front, Math.PI * EARTH_RADIUS / 2, 0));
  87.         // expectLatLngApproxEquals(down, computeOffset(front, Math.PI * EARTH_RADIUS / 2, 180));
  88.         // expectLatLngApproxEquals(left, computeOffset(front, Math.PI * EARTH_RADIUS / 2, -90));
  89.         // expectLatLngApproxEquals(right, computeOffset(front, Math.PI * EARTH_RADIUS / 2, 90));
  90.         expectLatLngApproxEquals(back, computeOffset(front, Math.PI * EARTH_RADIUS, 0));
  91.         expectLatLngApproxEquals(back, computeOffset(front, Math.PI * EARTH_RADIUS, 90));
  92.     }
  93. }
RAW Paste Data