Dev: Added CoordinateConverter for Cartesian and Polar coordinate conversions (#6628)

Added CoordinateConverter utility class for Cartesian and Polar coordinate conversions
This commit is contained in:
Banula Kumarage
2025-10-05 16:56:13 +05:30
committed by GitHub
parent a0b6c52790
commit e6cb96f390
2 changed files with 91 additions and 0 deletions

View File

@@ -0,0 +1,57 @@
package com.thealgorithms.conversions;
/**
* A utility class to convert between Cartesian and Polar coordinate systems.
*
* <p>This class provides methods to perform the following conversions:
* <ul>
* <li>Cartesian to Polar coordinates</li>
* <li>Polar to Cartesian coordinates</li>
* </ul>
*
* <p>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};
}
}

View File

@@ -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));
}
}