Skip to content

Commit e6cb96f

Browse files
Dev: Added CoordinateConverter for Cartesian and Polar coordinate conversions (#6628)
Added CoordinateConverter utility class for Cartesian and Polar coordinate conversions
1 parent a0b6c52 commit e6cb96f

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.thealgorithms.conversions;
2+
3+
/**
4+
* A utility class to convert between Cartesian and Polar coordinate systems.
5+
*
6+
* <p>This class provides methods to perform the following conversions:
7+
* <ul>
8+
* <li>Cartesian to Polar coordinates</li>
9+
* <li>Polar to Cartesian coordinates</li>
10+
* </ul>
11+
*
12+
* <p>The class is final and cannot be instantiated.
13+
*/
14+
public final class CoordinateConverter {
15+
16+
private CoordinateConverter() {
17+
// Prevent instantiation
18+
}
19+
20+
/**
21+
* Converts Cartesian coordinates to Polar coordinates.
22+
*
23+
* @param x the x-coordinate in the Cartesian system; must be a finite number
24+
* @param y the y-coordinate in the Cartesian system; must be a finite number
25+
* @return an array where the first element is the radius (r) and the second element is the angle (theta) in degrees
26+
* @throws IllegalArgumentException if x or y is not a finite number
27+
*/
28+
public static double[] cartesianToPolar(double x, double y) {
29+
if (!Double.isFinite(x) || !Double.isFinite(y)) {
30+
throw new IllegalArgumentException("x and y must be finite numbers.");
31+
}
32+
double r = Math.sqrt(x * x + y * y);
33+
double theta = Math.toDegrees(Math.atan2(y, x));
34+
return new double[] {r, theta};
35+
}
36+
37+
/**
38+
* Converts Polar coordinates to Cartesian coordinates.
39+
*
40+
* @param r the radius in the Polar system; must be non-negative
41+
* @param thetaDegrees the angle (theta) in degrees in the Polar system; must be a finite number
42+
* @return an array where the first element is the x-coordinate and the second element is the y-coordinate in the Cartesian system
43+
* @throws IllegalArgumentException if r is negative or thetaDegrees is not a finite number
44+
*/
45+
public static double[] polarToCartesian(double r, double thetaDegrees) {
46+
if (r < 0) {
47+
throw new IllegalArgumentException("Radius (r) must be non-negative.");
48+
}
49+
if (!Double.isFinite(thetaDegrees)) {
50+
throw new IllegalArgumentException("Theta (angle) must be a finite number.");
51+
}
52+
double theta = Math.toRadians(thetaDegrees);
53+
double x = r * Math.cos(theta);
54+
double y = r * Math.sin(theta);
55+
return new double[] {x, y};
56+
}
57+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.thealgorithms.conversions;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.CsvSource;
8+
9+
public class CoordinateConverterTest {
10+
11+
@ParameterizedTest
12+
@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"})
13+
void testCartesianToPolar(double x, double y, double expectedR, double expectedTheta) {
14+
assertArrayEquals(new double[] {expectedR, expectedTheta}, CoordinateConverter.cartesianToPolar(x, y), 1e-9);
15+
}
16+
17+
@ParameterizedTest
18+
@CsvSource({"1, 0, 1, 0", "1, 90, 0, 1", "1, 180, -1, 0", "1, -90, 0, -1", "5, 53.13010235415599, 3, 4"})
19+
void testPolarToCartesian(double r, double theta, double expectedX, double expectedY) {
20+
assertArrayEquals(new double[] {expectedX, expectedY}, CoordinateConverter.polarToCartesian(r, theta), 1e-9);
21+
}
22+
23+
@ParameterizedTest
24+
@CsvSource({"NaN, 1", "1, NaN", "Infinity, 1", "1, Infinity", "-Infinity, 1", "1, -Infinity"})
25+
void testCartesianToPolarInvalidInputs(double x, double y) {
26+
assertThrows(IllegalArgumentException.class, () -> CoordinateConverter.cartesianToPolar(x, y));
27+
}
28+
29+
@ParameterizedTest
30+
@CsvSource({"-1, 0", "1, NaN", "1, Infinity", "1, -Infinity"})
31+
void testPolarToCartesianInvalidInputs(double r, double theta) {
32+
assertThrows(IllegalArgumentException.class, () -> CoordinateConverter.polarToCartesian(r, theta));
33+
}
34+
}

0 commit comments

Comments
 (0)