From 3bf9d581148dd42256c0e78418c9f901bccc63de Mon Sep 17 00:00:00 2001 From: BanulaKumarage Date: Sun, 5 Oct 2025 00:12:44 +0530 Subject: [PATCH] Added CoordinateConverter utility class for Cartesian and Polar coordinate conversions --- .../conversions/CoordinateConverter.java | 57 +++++++++++++++++++ .../conversions/CoordinateConverterTest.java | 34 +++++++++++ 2 files changed, 91 insertions(+) create mode 100644 src/main/java/com/thealgorithms/conversions/CoordinateConverter.java create mode 100644 src/test/java/com/thealgorithms/conversions/CoordinateConverterTest.java diff --git a/src/main/java/com/thealgorithms/conversions/CoordinateConverter.java b/src/main/java/com/thealgorithms/conversions/CoordinateConverter.java new file mode 100644 index 000000000000..2766a3a1cf89 --- /dev/null +++ b/src/main/java/com/thealgorithms/conversions/CoordinateConverter.java @@ -0,0 +1,57 @@ +package com.thealgorithms.conversions; + +/** + * A utility class to convert between Cartesian and Polar coordinate systems. + * + *

This class provides methods to perform the following conversions: + *

+ * + *

The class is final and cannot be instantiated. + */ +public final class CoordinateConverter { + + private CoordinateConverter() { + // Prevent instantiation + } + + /** + * Converts Cartesian coordinates to Polar coordinates. + * + * @param x the x-coordinate in the Cartesian system; must be a finite number + * @param y the y-coordinate in the Cartesian system; must be a finite number + * @return an array where the first element is the radius (r) and the second element is the angle (theta) in degrees + * @throws IllegalArgumentException if x or y is not a finite number + */ + public static double[] cartesianToPolar(double x, double y) { + if (!Double.isFinite(x) || !Double.isFinite(y)) { + throw new IllegalArgumentException("x and y must be finite numbers."); + } + double r = Math.sqrt(x * x + y * y); + double theta = Math.toDegrees(Math.atan2(y, x)); + return new double[] {r, theta}; + } + + /** + * Converts Polar coordinates to Cartesian coordinates. + * + * @param r the radius in the Polar system; must be non-negative + * @param thetaDegrees the angle (theta) in degrees in the Polar system; must be a finite number + * @return an array where the first element is the x-coordinate and the second element is the y-coordinate in the Cartesian system + * @throws IllegalArgumentException if r is negative or thetaDegrees is not a finite number + */ + public static double[] polarToCartesian(double r, double thetaDegrees) { + if (r < 0) { + throw new IllegalArgumentException("Radius (r) must be non-negative."); + } + if (!Double.isFinite(thetaDegrees)) { + throw new IllegalArgumentException("Theta (angle) must be a finite number."); + } + double theta = Math.toRadians(thetaDegrees); + double x = r * Math.cos(theta); + double y = r * Math.sin(theta); + return new double[] {x, y}; + } +} diff --git a/src/test/java/com/thealgorithms/conversions/CoordinateConverterTest.java b/src/test/java/com/thealgorithms/conversions/CoordinateConverterTest.java new file mode 100644 index 000000000000..e41e9160478d --- /dev/null +++ b/src/test/java/com/thealgorithms/conversions/CoordinateConverterTest.java @@ -0,0 +1,34 @@ +package com.thealgorithms.conversions; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +public class CoordinateConverterTest { + + @ParameterizedTest + @CsvSource({"0, 0, 0, 0", "1, 0, 1, 0", "0, 1, 1, 90", "-1, 0, 1, 180", "0, -1, 1, -90", "3, 4, 5, 53.13010235415599"}) + void testCartesianToPolar(double x, double y, double expectedR, double expectedTheta) { + assertArrayEquals(new double[] {expectedR, expectedTheta}, CoordinateConverter.cartesianToPolar(x, y), 1e-9); + } + + @ParameterizedTest + @CsvSource({"1, 0, 1, 0", "1, 90, 0, 1", "1, 180, -1, 0", "1, -90, 0, -1", "5, 53.13010235415599, 3, 4"}) + void testPolarToCartesian(double r, double theta, double expectedX, double expectedY) { + assertArrayEquals(new double[] {expectedX, expectedY}, CoordinateConverter.polarToCartesian(r, theta), 1e-9); + } + + @ParameterizedTest + @CsvSource({"NaN, 1", "1, NaN", "Infinity, 1", "1, Infinity", "-Infinity, 1", "1, -Infinity"}) + void testCartesianToPolarInvalidInputs(double x, double y) { + assertThrows(IllegalArgumentException.class, () -> CoordinateConverter.cartesianToPolar(x, y)); + } + + @ParameterizedTest + @CsvSource({"-1, 0", "1, NaN", "1, Infinity", "1, -Infinity"}) + void testPolarToCartesianInvalidInputs(double r, double theta) { + assertThrows(IllegalArgumentException.class, () -> CoordinateConverter.polarToCartesian(r, theta)); + } +}