From 24c3a6312d8fdf21aec55d97ea56c6558a360bb2 Mon Sep 17 00:00:00 2001 From: Kaushik Gunasekaren Date: Thu, 12 Nov 2026 10:49:05 -0700 Subject: [PATCH 1/2] Include ellipse level offset in exported polynomial --- spatialmath/geom2d.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spatialmath/geom2d.py b/spatialmath/geom2d.py index ee42c08f..f2b5b4f3 100755 --- a/spatialmath/geom2d.py +++ b/spatialmath/geom2d.py @@ -1025,7 +1025,7 @@ def polynomial(self): 2 * c, -2 * a * x_0 - 2 * c * y_0, -2 * b * y_0 - 2 * c * x_0, - a * x_0**2 + b * y_0**2 + 2 * c * x_0 * y_0, + a * x_0**2 + b * y_0**2 + 2 * c * x_0 * y_0 - 1, ] ) From b84a8a15a2eee6eecd22c5af78da59033b32787b Mon Sep 17 00:00:00 2001 From: Kaushik Gunasekaren Date: Thu, 12 Nov 2026 10:49:25 -0700 Subject: [PATCH 2/2] Test ellipse polynomial values on perimeter and interior points --- tests/test_geom2d.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tests/test_geom2d.py b/tests/test_geom2d.py index 49aa1d8b..3be6d805 100755 --- a/tests/test_geom2d.py +++ b/tests/test_geom2d.py @@ -142,6 +142,38 @@ def test_intersect_segment(self): class EllipseTest(unittest.TestCase): + def test_exported_polynomial_matches_ellipse_equation(self): + for radii in ((1, 2), (2, 3)): + for centre in ((0, 0), (3, -4)): + for theta in (0, 0.4, -0.7): + with self.subTest(radii=radii, centre=centre, theta=theta): + ellipse = Ellipse(radii=radii, centre=centre, theta=theta) + coefficients = ellipse.polynomial + angles = np.linspace(0, 2 * np.pi, 17) + rotation = np.array( + [ + [np.cos(theta), -np.sin(theta)], + [np.sin(theta), np.cos(theta)], + ] + ) + points = rotation @ ( + np.array(radii)[:, None] + * np.array([np.cos(angles), np.sin(angles)]) + ) + points += np.array(centre)[:, None] + x, y = points + terms = np.array([x * x, y * y, x * y, x, y, np.ones_like(x)]) + nt.assert_allclose(coefficients @ terms, 0, atol=1e-12) + x, y = centre + self.assertAlmostEqual( + coefficients @ [x * x, y * y, x * y, x, y, 1], -1 + ) + outside = 2 * (points[:, 0] - centre) + centre + x, y = outside + self.assertAlmostEqual( + coefficients @ [x * x, y * y, x * y, x, y, 1], 3 + ) + def test_constructor(self): E = np.array([[1, 1], [1, 3]]) e = Ellipse(E=E)