Cartesian coordinates from degree angle in Java

The challenge

Write a simple function that takes polar coordinates (an angle in degrees and a radius) and returns the equivalent cartesian coordinates (rounded to 10 places).

For example:

coordinates(90,1) => (0.0, 1.0)

coordinates(45, 1) => (0.7071067812, 0.7071067812)

The solution in Java code

Option 1:

import java.awt.geom.Point2D; class CoordinatesInspector { Point2D.Double coordinates(Double degrees, Double radius) { return new Point2D.Double( Math.round(radius * Math.cos(Math.toRadians(degrees)) * 10e9) / 10e9, Math.round(radius * Math.sin(Math.toRadians(degrees)) * 10e9) / 10e9); } }
Code language: Java (java)

Option 2:

import java.awt.geom.Point2D; import java.math.*; public class CoordinatesInspector { public Point2D coordinates(Double degrees, Double radius) { double rd = Math.toRadians(degrees); double x = radius * Math.cos(rd); double y = radius * Math.sin(rd); BigDecimal xb = new BigDecimal(x).setScale(10, RoundingMode.HALF_UP); BigDecimal yb = new BigDecimal(y).setScale(10, RoundingMode.HALF_UP); return new Point2D.Double(xb.doubleValue(),yb.doubleValue()); } }
Code language: Java (java)

Option 3:

import java.awt.geom.Point2D; public class CoordinatesInspector { public static Point2D coordinates(Double degrees, Double radius) { var radians = Math.toRadians(degrees); return new Point2D.Double( Double.parseDouble(String.format("%.10f", radius * Math.cos(radians))), Double.parseDouble(String.format("%.10f", radius * Math.sin(radians))) ); } }
Code language: Java (java)

Test cases to validate our solution

import java.awt.geom.Point2D; import org.junit.Test; import static org.junit.Assert.assertEquals; public class CoordinatesTest { @Test public void test_1() { Point2D result = new CoordinatesInspector().coordinates(90.0,1.0); assertEquals("Incorrect coordinates", new Point2D.Double(0.0,1.0), result); } }
Code language: Java (java)

Additional test cases

import java.awt.geom.Point2D; import static org.junit.Assert.*; import org.junit.Test; public class CoordinatesTest { @Test public void test_1() { Point2D result = new CoordinatesInspector().coordinates(90.0,1.0); assertEquals("Incorrect coordinates", new Point2D.Double(0.0,1.0), result); } @Test public void test_2() { Point2D result = new CoordinatesInspector().coordinates(90.0,2.0); assertEquals("Incorrect coordinates", new Point2D.Double(0.0,2.0), result); } @Test public void test_3() { Point2D result = new CoordinatesInspector().coordinates(0.0,1.0); assertEquals("Incorrect coordinates", new Point2D.Double(1.0,0.0), result); } @Test public void test_4() { Point2D result = new CoordinatesInspector().coordinates(45.0,1.0); assertEquals("Incorrect coordinates", new Point2D.Double(0.7071067812,0.7071067812), result); } @Test public void test_5() { Point2D result = new CoordinatesInspector().coordinates(1090.0,10000.0); assertEquals("Incorrect coordinates", new Point2D.Double(9848.0775301221,1736.4817766693), result); } @Test public void test_6() { Point2D result = new CoordinatesInspector().coordinates(-270.0,1.0); assertEquals("Incorrect coordinates", new Point2D.Double(0.0,1.0), result); } }
Code language: Java (java)
Tags:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments