Skip to content

Commit 1a835fe

Browse files
authored
[wpimath] Fix singularities in Ellipse2d::Nearest() (#7851)
The problem was ill-conditioned if either semiaxis had zero length.
1 parent 0ad595c commit 1a835fe

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

wpimath/src/main/native/cpp/geometry/Ellipse2d.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@ Translation2d Ellipse2d::Nearest(const Translation2d& point) const {
3434
slp::pow(y - rotPoint.Y().value(), 2));
3535

3636
// (x − x_c)²/a² + (y − y_c)²/b² = 1
37-
problem.SubjectTo(slp::pow(x - m_center.X().value(), 2) /
38-
(m_xSemiAxis.value() * m_xSemiAxis.value()) +
39-
slp::pow(y - m_center.Y().value(), 2) /
40-
(m_ySemiAxis.value() * m_ySemiAxis.value()) ==
41-
1);
37+
// b²(x − x_c)² + a²(y − y_c)² = a²b²
38+
double a2 = m_xSemiAxis.value() * m_xSemiAxis.value();
39+
double b2 = m_ySemiAxis.value() * m_ySemiAxis.value();
40+
problem.SubjectTo(b2 * slp::pow(x - m_center.X().value(), 2) +
41+
a2 * slp::pow(y - m_center.Y().value(), 2) ==
42+
a2 * b2);
4243

4344
problem.Solve();
4445

0 commit comments

Comments
 (0)