Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
freem
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Openai/6959ed27-accc-800e-8e87-21aa81e93c07
(section)
Add languages
Page
Discussion
English
Read
Edit
Edit source
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
Edit source
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Assistant: Here’s a solid, numerically-stable C++ C++17 function for the distance from a 2D point to the closest point on an axis-… === Here’s a solid, numerically-stable C++ (C++17) function for the distance from a 2D point to the closest point on an axis-aligned ellipse: * Ellipse: centered at origin, semi-axes a (x-radius) and b (y-radius) * Point: (x, y) * Returns: closest point (cx, cy) on the ellipse + Euclidean distance <syntaxhighlight lang="cpp">#include <cmath> #include <algorithm> #include <limits> struct PointEllipseResult { double distance; // Euclidean distance from (x,y) to ellipse double cx, cy; // closest point on ellipse double t; // root parameter (useful for debugging) int iterations; // iterations used }; // Distance from point (x,y) to axis-aligned ellipse (x/a)^2 + (y/b)^2 = 1 // Ellipse is centered at origin, a>0, b>0. PointEllipseResult distancePointToEllipse(double x, double y, double a, double b) { PointEllipseResult out{}; out.t = 0.0; out.iterations = 0; // Basic validation if (!(a > 0.0) || !(b > 0.0) || !std::isfinite(x) || !std::isfinite(y) || !std::isfinite(a) || !std::isfinite(b)) { out.distance = std::numeric_limits<double>::quiet_NaN(); out.cx = out.cy = out.distance; return out; } const double alpha = a * a; const double beta = b * b; // Work in first quadrant (symmetry), restore signs at end const double sx = (x < 0.0) ? -1.0 : 1.0; const double sy = (y < 0.0) ? -1.0 : 1.0; const double px = std::fabs(x); const double py = std::fabs(y); // Handle origin explicitly (closest point is along the minor axis) if (px == 0.0 && py == 0.0) { if (a <= b) { out.cx = sx * a; out.cy = 0.0; out.distance = a; } else { out.cx = 0.0; out.cy = sy * b; out.distance = b; } out.t = (a <= b) ? 0.0 : 0.0; out.iterations = 0; return out; } // Handle axis cases with a simple closed form (more robust than iteration) if (py == 0.0) { out.cx = sx * a; out.cy = 0.0; out.distance = std::fabs(px - a); out.t = a * px - alpha; // optional, matches the t-equation solution out.iterations = 0; return out; } if (px == 0.0) { out.cx = 0.0; out.cy = sy * b; out.distance = std::fabs(py - b); out.t = b * py - beta; // optional out.iterations = 0; return out; } // Decide if point is outside or inside ellipse const double insideTest = (px '' px) / alpha + (py '' py) / beta; const bool outside = (insideTest > 1.0); auto F = [&](double t) -> double { const double da = t + alpha; const double db = t + beta; const double termA = alpha '' px '' px / (da * da); const double termB = beta '' py '' py / (db * db); return termA + termB - 1.0; }; auto Fp = [&](double t) -> double { const double da = t + alpha; const double db = t + beta; const double termA = -2.0 '' alpha '' px '' px / (da '' da * da); const double termB = -2.0 '' beta '' py '' py / (db '' db * db); return termA + termB; // strictly negative }; // Bracket the root for t double tLow, tHigh; if (outside) { // Root in [0, +inf) tLow = 0.0; tHigh = 1.0; // Expand until F(tHigh) < 0 for (int i = 0; i < 80 && F(tHigh) > 0.0; ++i) { tHigh *= 2.0; if (!std::isfinite(tHigh)) break; } } else { // Root in (-min(alpha,beta), 0] const double m = std::min(alpha, beta); const double eps = std::numeric_limits<double>::epsilon() * m; tLow = -m + eps; // avoid singular tHigh = 0.0; } // Initial guess double t = outside ? 0.0 : 0.5 * (tLow + tHigh); // Newton + bisection safeguard const int maxIter = 64; const double tol = 1e-14; for (int iter = 0; iter < maxIter; ++iter) { out.iterations = iter + 1; const double f = F(t); if (std::fabs(f) <= tol) break; // Maintain bracket knowing F is strictly decreasing in t if (f > 0.0) tLow = t; else tHigh = t; const double fp = Fp(t); double tCand = t; if (std::isfinite(fp) && fp != 0.0) { tCand = t - f / fp; // Newton } // If Newton goes out of bracket or is NaN/inf, fall back to bisection if (!(tCand > tLow && tCand < tHigh) || !std::isfinite(tCand)) { tCand = 0.5 * (tLow + tHigh); } t = tCand; } // Closest point in first quadrant const double cx_q = (alpha * px) / (t + alpha); const double cy_q = (beta * py) / (t + beta); // Restore signs out.cx = sx * cx_q; out.cy = sy * cy_q; out.t = t; // Distance out.distance = std::hypot(out.cx - x, out.cy - y); return out; } </syntaxhighlight> Note (if you need translated/rotated ellipses): If your ellipse is centered at C and rotated by angle θ, transform the point into ellipse-local coordinates with p' = Rᵀ (p - C), call the function above, then rotate/translate the closest point back with p_closest = C + R * p'_closest. If you tell me your ellipse representation (center + rotation, or general quadratic form), I’ll drop in that wrapper too.
Summary:
Please note that all contributions to freem are considered to be released under the Creative Commons Attribution-ShareAlike 4.0 (see
Freem:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)