@Override public boolean equals(Object obj) { if (obj == null) return false; if (!(obj instanceof SphericCoordinate)) return false; SphericCoordinate otherCoordinate = (SphericCoordinate) obj; if (latitude == otherCoordinate.getLatitude() && longitude == otherCoordinate.getLongitude() && radius == otherCoordinate.getRadius()) return true; else return false; }
@BeforeClass public static void setUp() { /* Example provided by https://de.wikipedia.org/wiki/Orthodrome, released 07.09.2015 19:55 */ berlinSpheric = SphericCoordinate.getCoordinate(52.517, 13.4); tokioSpheric = SphericCoordinate.getCoordinate(35.7, 139.767); berlinCartesian = CartesianCoordinate.getCoordinate(3771.373, 898.468, 5055.605); tokioCartesian = CartesianCoordinate.getCoordinate(-3949.792, 3341.734, 3717.741); berlinSphericSameHashCode = SphericCoordinate.getCoordinate( ((AbstractCoordinate) berlinCartesian).getLatitude(), ((AbstractCoordinate) berlinCartesian).getLongitude(), ((AbstractCoordinate) berlinCartesian).getRadius()); berlinSpheric2 = SphericCoordinate.getCoordinate(52.517, 13.4); berlinCartesian2 = CartesianCoordinate.getCoordinate(3771.373, 898.468, 5055.605); }
/** * @Pattern ( name = "Template" participants = { "AbstactCoordinate", "CartesianCoordinate", * "SphericCoordinate" } ) */ @Override protected double getRadialDistance(Coordinate other) { if (other == null) { throw new IllegalArgumentException("Other coordinate must not be null"); } SphericCoordinate sphOther = convertToSpheric(other); double radLatitude = Math.toRadians(this.getLatitude()); double otherRadLatitude = Math.toRadians(sphOther.getLatitude()); double lonDistance = Math.toRadians(this.getLongitudinalDistance(sphOther)); return (Math.acos( Math.sin(radLatitude) * Math.sin(otherRadLatitude) + Math.cos(radLatitude) * Math.cos(otherRadLatitude) * Math.cos(lonDistance))); }
/** * Computes the absolute distance to the other coordinate's latitude * * @methodtype get * @methodproperty regular */ public double getLatitudinalDistance(SphericCoordinate other) { if (other == null) { throw new IllegalArgumentException("Other coordinate must not be null"); } return Math.abs(other.getLatitude() - this.getLatitude()); }
/** * Computes the absolute distance to the other coordinate's longitude * * @methodtype get * @methodproperty regular */ public double getLongitudinalDistance(SphericCoordinate other) { if (other == null) { throw new IllegalArgumentException("Other coordinate must not be null"); } double result; if (Math.signum(this.getLongitude()) != Math.signum(other.getLatitude())) { result = Math.abs(this.getLongitude()) + Math.abs(other.getLongitude()); if (result > 180.) { result = 360. - result; } } else { result = Math.abs(Math.abs(this.getLongitude()) - Math.abs(other.getLongitude())); } return result; }
/** * @methodtype conversion * @param Coordinate to convert * @return SphericCoordinate */ private static SphericCoordinate convertToSpheric(Coordinate other) { if (SphericCoordinate.class.isInstance(other)) { return (SphericCoordinate) other; } else if (CartesianCoordinate.class.isInstance(other)) { CartesianCoordinate carOther = (CartesianCoordinate) other; double r = Math.sqrt( Math.pow(carOther.getX(), 2) + Math.pow(carOther.getY(), 2) + Math.pow(carOther.getZ(), 2)); double latitude = Math.toDegrees(Math.asin(carOther.getZ() / r)); double longitude = Math.toDegrees(Math.atan2(carOther.getY(), carOther.getX())); SphericCoordinate toRet = getSphericCoordinate(latitude, longitude, r); toRet.assertClassInvariants(); return toRet; } else { throw new UnsupportedOperationException("Unknown Type of Coordinate"); } }
/** * @param other the other Coordinate-Object * @return distance between the latitude-values of both coordinates as double-value * @methodtype query */ public double getLatitudinalDistance(SphericCoordinate secondCoordinate) { // preconditions assertNotNull(secondCoordinate); double latitudinalDistance = this.latitude - secondCoordinate.getLatitude(); latitudinalDistance = Math.abs(latitudinalDistance); // check for shortest way if (latitudinalDistance > 90) { latitudinalDistance = 180 - latitudinalDistance; } // postconditions assertIsValidDistance(latitudinalDistance); return latitudinalDistance; }