Repository navigation
[Utils] Clean up coordinates classes and tests #112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
9aff2bd
de1df58
e53c687
b5ff98b
221e3ab
072c1d3
c58ad6b
6639b6f
8d01173
cb8fc2b
b12ba09
66825e0
b7769df
fde8285
cc574ba
3a23a66
4d177bc
8d2dea3
37ce457
5b24122
3055f72
ccf32b0
74c81b8
d063df8
0e4794e
37c8c70
a55fe1c
6829370
728b2c1
d68be39
a3d1a1a
a4076a6
51b36e0
1b6f999
7936281
32958ad
2bc261c
1c207ae
5e84fa5
fe294d2
086ea47
515e488
f9bdd9a
f6ec3ea
820eb7d
1a875cf
b69ca74
78b20e1
0097541
8aa9a1b
70c3f13
3693e95
db49203
2d858fa
fa34391
27309b6
5fdbce5
82c9e5b
2b310cb
e84b7b0
12ef012
3abcb5e
202ce9c
cabe6af
60dda38
ebb88f2
bc90392
ac2c656
1d79501
4fb28bf
69c531b
67eba59
c5cb7f4
9e37530
7fbb745
a1c34d6
51e8d76
e702f14
5ca37ef
59cab92
8104ac6
040cd52
64abe47
2b20b5d
7732efe
d4d26d8
d786635
1327336
b3d8d2d
1a377ad
7108370
82bf5a9
236d837
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| using UnityEngine; | ||
|
|
||
| // Utility functions for 2D coordinates. | ||
| // In Cartesian coordinates, the x-axis points right, and the y-axis points up. The coordinates are | ||
| // given by (x, y). | ||
| // In polar coordinates, the angle is measured in degrees from the x-axis counterclockwise to the | ||
| // y-axis. The coordinates are given by (r, theta). | ||
| public static class Coordinates2 { | ||
| public static Vector2 ConvertCartesianToPolar(in Vector2 cartesian) { | ||
| float r = cartesian.magnitude; | ||
| float theta = Mathf.Atan2(cartesian.y, cartesian.x) * Mathf.Rad2Deg; | ||
| return new Vector2(r, theta); | ||
| } | ||
| public static Vector2 ConvertCartesianToPolar(float x, float y) { | ||
| return ConvertCartesianToPolar(new Vector2(x, y)); | ||
| } | ||
|
|
||
| public static Vector2 ConvertPolarToCartesian(in Vector2 polar) { | ||
| float x = polar.x * Mathf.Cos(polar.y * Mathf.Deg2Rad); | ||
| float y = polar.x * Mathf.Sin(polar.y * Mathf.Deg2Rad); | ||
| return new Vector2(x, y); | ||
| } | ||
| public static Vector2 ConvertPolarToCartesian(float r, float theta) { | ||
| return ConvertPolarToCartesian(new Vector2(r, theta)); | ||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| using NUnit.Framework; | ||
| using UnityEngine; | ||
| using UnityEngine.TestTools.Utils; | ||
|
|
||
| public class Coordinates2Tests { | ||
| private const float _epsilon = 1e-4f; | ||
|
|
||
| [Test] | ||
| public void ConvertCartesianToPolar_Origin_ReturnsZeroDistance() { | ||
| var cartesian = Vector2.zero; | ||
| Vector2 polar = Coordinates2.ConvertCartesianToPolar(cartesian); | ||
| Assert.AreEqual(0, polar.x, _epsilon); | ||
| Assert.IsFalse(float.IsNaN(polar.y)); | ||
| } | ||
|
tryuan99 marked this conversation as resolved.
Resolved
|
||
|
|
||
| [Test] | ||
| public void ConvertCartesianToPolar_OnXAxis_ReturnsZeroTheta() { | ||
| var cartesian = new Vector2(5, 0); | ||
| var expectedPolar = new Vector2(5.0f, 0.0f); | ||
| Vector2 actualPolar = Coordinates2.ConvertCartesianToPolar(cartesian); | ||
| Assert.That(actualPolar, Is.EqualTo(expectedPolar).Using(Vector2EqualityComparer.Instance)); | ||
| } | ||
|
|
||
| [Test] | ||
| public void ConvertCartesianToPolar_OnYAxis_ReturnsNinetyTheta() { | ||
| var cartesian = new Vector2(0, 5); | ||
| var expectedPolar = new Vector2(5.0f, 90.0f); | ||
| Vector2 actualPolar = Coordinates2.ConvertCartesianToPolar(cartesian); | ||
| Assert.That(actualPolar, Is.EqualTo(expectedPolar).Using(Vector2EqualityComparer.Instance)); | ||
| } | ||
|
|
||
| [Test] | ||
| [TestCase(0f)] | ||
| [TestCase(45f)] | ||
| [TestCase(90f)] | ||
| [TestCase(180f)] | ||
| [TestCase(360f)] | ||
| public void ConvertPolarToCartesian_ZeroDistance_ReturnsOrigin(float theta) { | ||
| var polar = new Vector2(0, theta); | ||
| var expectedCartesian = Vector2.zero; | ||
| Vector2 actualCartesian = Coordinates2.ConvertPolarToCartesian(polar); | ||
| Assert.That(actualCartesian, | ||
| Is.EqualTo(expectedCartesian).Using(Vector2EqualityComparer.Instance)); | ||
| } | ||
|
tryuan99 marked this conversation as resolved.
Resolved
|
||
|
|
||
| [Test] | ||
| public void ConvertPolarToCartesian_ZeroTheta_ReturnsOnXAxis() { | ||
| var polar = new Vector2(10, 0); | ||
| var expectedCartesian = new Vector2(10.0f, 0.0f); | ||
| Vector2 actualCartesian = Coordinates2.ConvertPolarToCartesian(polar); | ||
| Assert.That(actualCartesian, | ||
| Is.EqualTo(expectedCartesian).Using(Vector2EqualityComparer.Instance)); | ||
| } | ||
|
|
||
| [Test] | ||
| public void ConvertPolarToCartesian_NinetyTheta_ReturnsOnYAxis() { | ||
| Vector2 polar = new Vector2(10, 90); | ||
| Vector2 expectedCartesian = new Vector2(0.0f, 10.0f); | ||
| Vector2 actualCartesian = Coordinates2.ConvertPolarToCartesian(polar); | ||
| Assert.That(actualCartesian, | ||
| Is.EqualTo(expectedCartesian).Using(Vector2EqualityComparer.Instance)); | ||
| } | ||
|
|
||
| [Test] | ||
| public void ConvertPolarToCartesian_ThirtyTheta() { | ||
| var polar = new Vector2(20, 30); | ||
| var expectedCartesian = new Vector2(10 * Mathf.Sqrt(3), 10); | ||
| Vector2 actualCartesian = Coordinates2.ConvertPolarToCartesian(polar); | ||
| Assert.That(actualCartesian, | ||
| Is.EqualTo(expectedCartesian).Using(Vector2EqualityComparer.Instance)); | ||
| } | ||
|
|
||
| [Test] | ||
| public void ConvertPolarToCartesian_FortyFiveTheta() { | ||
| var polar = new Vector2(10 * Mathf.Sqrt(2), 45); | ||
| var expectedCartesian = new Vector2(10.0f, 10.0f); | ||
| Vector2 actualCartesian = Coordinates2.ConvertPolarToCartesian(polar); | ||
| Assert.That(actualCartesian, | ||
| Is.EqualTo(expectedCartesian).Using(Vector2EqualityComparer.Instance)); | ||
| } | ||
|
|
||
| [Test] | ||
| public void RoundTrip_CartesianToPolarToCartesian_ReturnsOriginal() { | ||
| var originalCartesian = new Vector2(3.5f, 7.2f); | ||
| Vector2 polar = Coordinates2.ConvertCartesianToPolar(originalCartesian); | ||
| Vector2 resultCartesian = Coordinates2.ConvertPolarToCartesian(polar); | ||
| Assert.That(resultCartesian, | ||
| Is.EqualTo(originalCartesian).Using(Vector2EqualityComparer.Instance)); | ||
| } | ||
| } | ||
|
tryuan99 marked this conversation as resolved.
Resolved
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| using NUnit.Framework; | ||
| using UnityEngine; | ||
| using UnityEngine.TestTools.Utils; | ||
|
|
||
| public class Coordinates3Tests { | ||
| private const float _epsilon = 1e-4f; | ||
|
|
||
| [Test] | ||
| public void ConvertCartesianToSpherical_Origin_ReturnsZeroDistance() { | ||
| var cartesian = Vector3.zero; | ||
| Vector3 spherical = Coordinates3.ConvertCartesianToSpherical(cartesian); | ||
| Assert.AreEqual(0, spherical.x, _epsilon); | ||
| Assert.IsFalse(float.IsNaN(spherical.y)); | ||
| Assert.IsFalse(float.IsNaN(spherical.z)); | ||
| } | ||
|
tryuan99 marked this conversation as resolved.
Resolved
|
||
|
|
||
| [Test] | ||
| public void ConvertCartesianToSpherical_ReturnsCorrectly() { | ||
| var cartesian = new Vector3(2, -1, 3); | ||
| var expectedSpherical = new Vector3(Mathf.Sqrt(14), 33.690068f, -15.501360f); | ||
| Vector3 actualSpherical = Coordinates3.ConvertCartesianToSpherical(cartesian); | ||
| Assert.That(actualSpherical, | ||
| Is.EqualTo(expectedSpherical).Using(Vector3EqualityComparer.Instance)); | ||
| } | ||
|
|
||
| [Test] | ||
| [TestCase(0f, 0f)] | ||
| [TestCase(45f, 0f)] | ||
| [TestCase(90f, 0f)] | ||
| [TestCase(180f, 0f)] | ||
| [TestCase(360f, 0f)] | ||
| [TestCase(45f, 45f)] | ||
| [TestCase(45f, 90f)] | ||
| [TestCase(45f, 180f)] | ||
| [TestCase(45f, 360f)] | ||
| public void ConvertSphericalToCartesian_ZeroDistance_ReturnsOrigin(float theta, float phi) { | ||
| var spherical = new Vector3(0, theta, phi); | ||
| var expectedCartesian = Vector3.zero; | ||
| Vector3 actualCartesian = Coordinates3.ConvertSphericalToCartesian(spherical); | ||
| Assert.That(actualCartesian, | ||
| Is.EqualTo(expectedCartesian).Using(Vector3EqualityComparer.Instance)); | ||
| } | ||
|
|
||
| [Test] | ||
| public void ConvertSphericalToCartesian_ReturnsCorrectly() { | ||
| var spherical = new Vector3(10, 60, 30); | ||
| var expectedCartesian = new Vector3(7.5f, 5.0f, 4.3301270189221945f); | ||
| Vector3 actualCartesian = Coordinates3.ConvertSphericalToCartesian(spherical); | ||
| Assert.That(actualCartesian, | ||
| Is.EqualTo(expectedCartesian).Using(Vector3EqualityComparer.Instance)); | ||
| } | ||
|
|
||
| [TestCase(1.5f, 2.5f, 3.5f)] | ||
| [TestCase(-1, 2, 3)] | ||
| [TestCase(-12, 0, 0)] | ||
| [TestCase(0, 5, 0)] | ||
| [TestCase(0, 0, -10)] | ||
| public void RoundTrip_CartesianToSphericalToCartesian_ReturnsOriginal(float x, float y, float z) { | ||
| var originalCartesian = new Vector3(x, y, z); | ||
| Vector3 spherical = Coordinates3.ConvertCartesianToSpherical(originalCartesian); | ||
| Vector3 resultCartesian = Coordinates3.ConvertSphericalToCartesian(spherical); | ||
| Assert.That(resultCartesian, | ||
| Is.EqualTo(originalCartesian).Using(Vector3EqualityComparer.Instance)); | ||
| } | ||
|
|
||
| [Test] | ||
| public void ConvertCartesianToCylindrical_Origin_ReturnsZeroDistanceZeroHeight() { | ||
| var cartesian = Vector3.zero; | ||
| Vector3 cylindrical = Coordinates3.ConvertCartesianToCylindrical(cartesian); | ||
| Assert.AreEqual(0, cylindrical.x, _epsilon); | ||
| Assert.IsFalse(float.IsNaN(cylindrical.y)); | ||
| Assert.AreEqual(0, cylindrical.z, _epsilon); | ||
| } | ||
|
tryuan99 marked this conversation as resolved.
Resolved
|
||
|
|
||
| [Test] | ||
| public void ConvertCartesianToCylindrical_ReturnsCorrectly() { | ||
| var cartesian = new Vector3(2, -1, 3); | ||
| var expectedCylindrical = new Vector3(Mathf.Sqrt(13), 33.6900675f, -1); | ||
| Vector3 actualCylindrical = Coordinates3.ConvertCartesianToCylindrical(cartesian); | ||
| Assert.That(actualCylindrical, | ||
| Is.EqualTo(expectedCylindrical).Using(Vector3EqualityComparer.Instance)); | ||
| } | ||
|
|
||
| [Test] | ||
| [TestCase(0f)] | ||
| [TestCase(45f)] | ||
| [TestCase(90f)] | ||
| [TestCase(180f)] | ||
| [TestCase(360f)] | ||
| public void ConvertCylindricalToCartesian_ZeroDistance_ZeroHeight_ReturnsOrigin(float theta) { | ||
| var cylindrical = new Vector3(0, theta, 0); | ||
| var expectedCartesian = Vector3.zero; | ||
| Vector3 actualCartesian = Coordinates3.ConvertCylindricalToCartesian(cylindrical); | ||
| Assert.That(actualCartesian, | ||
| Is.EqualTo(expectedCartesian).Using(Vector3EqualityComparer.Instance)); | ||
| } | ||
|
|
||
| [Test] | ||
| public void ConvertCylindricalToCartesian_ReturnsCorrectly() { | ||
| var cylindrical = new Vector3(10, 60, 2); | ||
| var expectedCartesian = new Vector3(8.660254f, 2.0f, 5.0f); | ||
| Vector3 actualCartesian = Coordinates3.ConvertCylindricalToCartesian(cylindrical); | ||
| Assert.That(actualCartesian, | ||
| Is.EqualTo(expectedCartesian).Using(Vector3EqualityComparer.Instance)); | ||
| } | ||
|
|
||
| [TestCase(1.5f, 2.5f, 3.5f)] | ||
| [TestCase(-1, 2, 3)] | ||
| [TestCase(-12, 0, 0)] | ||
| [TestCase(0, 5, 0)] | ||
| [TestCase(0, 0, -10)] | ||
| public void RoundTrip_CartesianToCylindricalToCartesian_ReturnsOriginal(float x, float y, | ||
| float z) { | ||
| var originalCartesian = new Vector3(x, y, z); | ||
| Vector3 cylindrical = Coordinates3.ConvertCartesianToCylindrical(originalCartesian); | ||
| Vector3 resultCartesian = Coordinates3.ConvertCylindricalToCartesian(cylindrical); | ||
| Assert.That(resultCartesian, | ||
| Is.EqualTo(originalCartesian).Using(Vector3EqualityComparer.Instance)); | ||
| } | ||
| } | ||
|
tryuan99 marked this conversation as resolved.
Resolved
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.