Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion spatialmath/geom2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]
)

Expand Down
32 changes: 32 additions & 0 deletions tests/test_geom2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down