diff --git a/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/mercator/EllipticalMercator.java b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/mercator/EllipticalMercator.java new file mode 100644 index 0000000000..e1c41f9518 --- /dev/null +++ b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/mercator/EllipticalMercator.java @@ -0,0 +1,22 @@ +package com.baeldung.algorithms.mercator; + +class EllipticalMercator extends Mercator { + + @Override + double yAxisProjection(double input) { + + input = Math.min(Math.max(input, -89.5), 89.5); + double earthDimensionalRateNormalized = 1.0 - Math.pow(RADIUS_MINOR / RADIUS_MAJOR, 2); + + double inputOnEarthProj = Math.sqrt(earthDimensionalRateNormalized) * Math.sin( Math.toRadians(input)); + + inputOnEarthProj = Math.pow(((1.0 - inputOnEarthProj)/(1.0+inputOnEarthProj)), 0.5 * Math.sqrt(earthDimensionalRateNormalized)); + double inputOnEarthProjNormalized = Math.tan(0.5 * ((Math.PI*0.5) - Math.toRadians(input)))/inputOnEarthProj; + return (-1) * RADIUS_MAJOR * Math.log(inputOnEarthProjNormalized); + } + + @Override + double xAxisProjection(double input) { + return RADIUS_MAJOR * Math.toRadians(input); + } +} diff --git a/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/mercator/Mercator.java b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/mercator/Mercator.java new file mode 100644 index 0000000000..b289b1839d --- /dev/null +++ b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/mercator/Mercator.java @@ -0,0 +1,10 @@ +package com.baeldung.algorithms.mercator; + +abstract class Mercator { + final static double RADIUS_MAJOR = 6378137.0; + final static double RADIUS_MINOR = 6356752.3142; + + abstract double yAxisProjection(double input); + + abstract double xAxisProjection(double input); +} diff --git a/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/mercator/SphericalMercator.java b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/mercator/SphericalMercator.java new file mode 100644 index 0000000000..1be976d82e --- /dev/null +++ b/algorithms-miscellaneous-2/src/main/java/com/baeldung/algorithms/mercator/SphericalMercator.java @@ -0,0 +1,14 @@ +package com.baeldung.algorithms.mercator; + +public class SphericalMercator extends Mercator { + + @Override + double xAxisProjection(double input) { + return Math.toRadians(input) * RADIUS_MAJOR; + } + + @Override + double yAxisProjection(double input) { + return Math.log(Math.tan(Math.PI / 4 + Math.toRadians(input) / 2)) * RADIUS_MAJOR; + } +} diff --git a/algorithms-miscellaneous-2/src/test/java/com/baeldung/algorithms/mercator/EllipticalMercatorUnitTest.java b/algorithms-miscellaneous-2/src/test/java/com/baeldung/algorithms/mercator/EllipticalMercatorUnitTest.java new file mode 100644 index 0000000000..96b644c46c --- /dev/null +++ b/algorithms-miscellaneous-2/src/test/java/com/baeldung/algorithms/mercator/EllipticalMercatorUnitTest.java @@ -0,0 +1,22 @@ +package com.baeldung.algorithms.mercator; + +import org.junit.Assert; +import org.junit.Test; + + +public class EllipticalMercatorUnitTest { + + @Test + public void giventThatTheInputIs22_whenXAxisProjectionIsCalled_thenTheResultIsTheCorrectOne() { + Mercator mercator = new EllipticalMercator(); + double result = mercator.xAxisProjection(22); + Assert.assertEquals(result, 2449028.7974520186, 0.0); + } + + @Test + public void giventThatTheInputIs44_whenYAxisProjectionIsCalled_thenTheResultIsTheCorrectOne() { + Mercator mercator = new EllipticalMercator(); + double result = mercator.yAxisProjection(44); + Assert.assertEquals(result, 5435749.887511954, 0.0); + } +} diff --git a/algorithms-miscellaneous-2/src/test/java/com/baeldung/algorithms/mercator/SphericalMercatorUnitTest.java b/algorithms-miscellaneous-2/src/test/java/com/baeldung/algorithms/mercator/SphericalMercatorUnitTest.java new file mode 100644 index 0000000000..348c6ad3e4 --- /dev/null +++ b/algorithms-miscellaneous-2/src/test/java/com/baeldung/algorithms/mercator/SphericalMercatorUnitTest.java @@ -0,0 +1,21 @@ +package com.baeldung.algorithms.mercator; + +import org.junit.Assert; +import org.junit.Test; + +public class SphericalMercatorUnitTest { + + @Test + public void giventThatTheInputIs22_whenXAxisProjectionIsCalled_thenTheResultIsTheCorrectOne() { + Mercator mercator = new SphericalMercator(); + double result = mercator.xAxisProjection(22); + Assert.assertEquals(result, 2449028.7974520186, 0.0); + } + + @Test + public void giventThatTheInputIs44_whenYAxisProjectionIsCalled_thenTheResultIsTheCorrectOne() { + Mercator mercator = new SphericalMercator(); + double result = mercator.yAxisProjection(44); + Assert.assertEquals(result, 5465442.183322753, 0.0); + } +}