Added unit tests for Mercator projection class.

- changed its methods to static
- add some documentation for its methods

#story[928]
This commit is contained in:
Haoming Yin
2017-05-15 13:23:04 +12:00
parent 3fd8b1b855
commit 4b1a4aae87
3 changed files with 64 additions and 27 deletions
+16 -16
View File
@@ -9,36 +9,36 @@ package seng302.models.map;
*/
public class Boundary {
private double north, east, south, west;
private double northLat, eastLng, southLat, westLng;
public Boundary(double north, double east, double south, double west) {
this.north = north;
this.east = east;
this.south = south;
this.west = west;
public Boundary(double northLat, double eastLng, double southLat, double westLng) {
this.northLat = northLat;
this.eastLng = eastLng;
this.southLat = southLat;
this.westLng = westLng;
}
public double getCentreLat() {
return (north + south) / 2;
return (northLat + southLat) / 2;
}
public double getCentreLng() {
return (east + west) / 2;
return (eastLng + westLng) / 2;
}
public double getNorth() {
return north;
public double getNorthLat() {
return northLat;
}
public double getEast() {
return east;
public double getEastLng() {
return eastLng;
}
public double getSouth() {
return south;
public double getSouthLat() {
return southLat;
}
public double getWest() {
return west;
public double getWestLng() {
return westLng;
}
}
@@ -2,21 +2,16 @@ package seng302.models.map;
public class MercatorProjection {
private double MERCATOR_RANGE = 256;
private double pixelsPerLngDegree, pixelsPerLngRadian;
public MercatorProjection() {
pixelsPerLngDegree = MERCATOR_RANGE / 360.0;
pixelsPerLngRadian = MERCATOR_RANGE / (2 * Math.PI);
}
private static final double MERCATOR_RANGE = 256;
private static final double pixelsPerLngDegree = MERCATOR_RANGE / 360.0;
private static final double pixelsPerLngRadian = MERCATOR_RANGE / (2 * Math.PI);
/**
* A help function keeps the value in bound between -0.9999 and 0.9999.
* @param value in bound value
* @return the value in bound
*/
private double bound(double value) {
private static double bound(double value) {
return Math.min(Math.max(value, -0.9999), 0.9999);
}
@@ -25,7 +20,7 @@ public class MercatorProjection {
* @param geo MapGeo (lat, lng) location to be projected
* @return the projection GeoPoint (x, y) on planar
*/
public MapPoint toMapPoint(MapGeo geo) {
public static MapPoint toMapPoint(MapGeo geo) {
MapPoint point = new MapPoint(0, 0);
MapPoint origin = new MapPoint(MERCATOR_RANGE / 2.0, MERCATOR_RANGE / 2.0);
point.setX(origin.getX() + geo.getLng() * pixelsPerLngDegree);
@@ -42,7 +37,7 @@ public class MercatorProjection {
* @param point MapPoint (x, y) to be converted back
* @return the original Geo location converted from the given projection point
*/
public MapGeo toMapGeo(MapPoint point) {
public static MapGeo toMapGeo(MapPoint point) {
MapPoint origin = new MapPoint(MERCATOR_RANGE / 2.0, MERCATOR_RANGE / 2.0);
double lng = (point.getX() - origin.getX()) / pixelsPerLngDegree;
double latRadians = (point.getY() - origin.getY()) / (-pixelsPerLngRadian);