Distance from a Point to an Ellipse, an Ellipsoid, Or a Hyperellipsoid
Total Page:16
File Type:pdf, Size:1020Kb
Distance from a Point to an Ellipse, an Ellipsoid, or a Hyperellipsoid David Eberly, Geometric Tools, Redmond WA 98052 https://www.geometrictools.com/ This work is licensed under the Creative Commons Attribution 4.0 International License. To view a copy of this license, visit http://creativecommons.org/licenses/by/4.0/ or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA. Created: June 28, 2013 Last Modified: September 11, 2020 Contents 1 Introduction 3 2 Distance from a Point to an Ellipse3 2.1 The Closest Point's Normal is Directed Toward the Query Point................3 2.2 The Case of a Circle.........................................4 2.3 The Query Point is the Origin....................................4 2.4 The Query Point is on the Vertical Axis..............................4 2.5 The Query Point is on the Horizontal Axis.............................5 2.6 The Query Point is Strictly in the First Quadrant........................5 2.7 A Summary of the Mathematical Algorithm............................6 2.8 Robust Root Finders.........................................7 2.8.1 Bisection Method.......................................7 2.8.2 Newton's Method.......................................8 2.8.3 Conversion to a Polynomial Equation........................... 10 2.9 A Robust Implementation...................................... 11 3 Distance from a Point to an Ellipsoid 13 3.1 The Closest Point's Normal is Directed Toward the Query Point................ 13 3.2 The Case of a Sphere......................................... 14 3.3 The Case of an Oblate Spheroid.................................. 14 3.4 The Case of a Prolate Spheroid................................... 14 1 3.5 The Query Point is the Origin.................................... 14 3.6 The Query Point has y2 > 0..................................... 14 3.7 The Query Point has y2 = 0..................................... 16 3.8 A Summary of the Mathematical Algorithm............................ 16 3.9 Robust Root Finding and Conversion to a Polynomial...................... 18 3.10 A Robust Implementation...................................... 18 4 Distance from a Point to a Hyperellipsoid 20 4.1 The Closest Point's Normal is Directed Toward the Query Point................ 20 4.2 The Key Observations........................................ 21 4.3 A Robust Implementation...................................... 22 2 1 Introduction This document describes an algorithm for computing the distance from a point to an ellipse (2D), from a point to an ellipsoid (3D), and from a point to a hyperellipsoid (any dimension). 2 Distance from a Point to an Ellipse A general ellipse in 2D is represented by a center point C, an orthonormal set of axis-direction vectors fU0; U1g, and associated extents ei with e0 ≥ e1 > 0. The ellipse points are P = C + x0U0 + x1U1 (1) where x 2 x 2 0 + 1 = 1 (2) e0 e1 If e0 = e1, then the ellipse is a circle with center C and radius e0. The orthonormality of the axis directions and Equation (1) imply xi = Ui · (P − C). Substituting this into Equation (2) we obtain (P − C)T M (P − C) = 1 (3) T where M = RDR , R is an orthogonal matrix whose columns are U0 and U1, and D is a diagonal matrix 2 2 whose diagonal entries are 1=e0 and 1=e1. The problem is to compute the distance from a point Q to the ellipse. It is sufficient to solve this problem in the coordinate system of the ellipse; that is, represent Q = C + y0U0 + y1U1. The distance from Q to the closest point P on the ellipse as defined by Equation (3) is the same as the distance from Y = (y0; y1) to the closest point X = (x0; x1) on the standard ellipse of Equation (2). We may additionally use symmetry to simplify the construction. It is sufficient to consider the case when (y0; y1) is in the first quadrant: y0 ≥ 0 and y1 ≥ 0. For example, if (y0; y1) is in the second quadrant where y0 < 0, and if (x0; x1) is the closest point which must be in the second quadrant, then (−y0; y1) is in the first quadrant and (−x0; x1) is the closest ellipse point which must be in the first quadrant. If we transform the query point into the first quadrant by sign changes on the components, we can construct the closest point in the first quadrant, and then undo the sign changes to obtain the result in the original quadrant. 2.1 The Closest Point's Normal is Directed Toward the Query Point A parameterization of the standard ellipse is X(θ) = (e0 cos θ; e1 sin θ) for θ 2 [0; 2π). The squared distance from Y to any point on the ellipse is F (θ) = jX(θ) − Yj2 (4) This is a nonnegative, periodic, and differentiable function; it must have a global minimum occurring at an angle for which the first-order derivative is zero, F 0(θ) = 2(X(θ) − Y) · X0(θ) = 0 (5) For the derivative to be zero, the vectors (X(θ) − Y) and X0(θ) must be perpendicular. The vector X0(θ) is tangent to the ellipse at X(θ). This implies that the vector from Y to the closest ellipse point X is normal to 3 2 2 the curve at X. Using the implicit form of the ellipse, namely, G(x0; x1) = (x0=e0) + (x1=e1) − 1 = 0, half the gradient of G(x0; x1) is a normal vector to the ellipse at (x0; x1), so (y0; y1)−(x0; x1) = trG(x0; x1)=2 = 2 2 t(x0=e0; x1=e1) for some scalar t, or t t y0 = x0 1 + 2 ; y1 = x1 1 + 2 (6) e0 e1 If (y0; y1) is outside the ellipse, it is necessary that t > 0. If (y0; y1) is inside the ellipse, it is necessary that t < 0. If (y0; y1) is already on the ellipse, then t = 0 and the distance is zero. 2.2 The Case of a Circle If e0 = e1, then the ellipse is a circle. The origin (0; 0) has infinitely many closest circle points (all of them), but clearly the distance from the origin to the circle is e0. The closest circle point to a point (y0; y1) 6= (0; 0) is (x0; x1) = e0(y0; y1)=j(y0; y1)j. Equation (6) is consistent with this, because it implies 2 2 (x0; x1) = (e0=(t + e0))(y0; y1) for some t; that is, (x0; x1) is parallel to (y0; y1) and must have length e0. It 2 p 2 2 is easily shown that t = −e0 + e0 y0 + y1. For the remainder of Section 2, we assume e0 > e1 and that all analysis is restricted to the first quadrant. 2.3 The Query Point is the Origin 2 2 Let y0 = 0 and y1 = 0. Equation (6) becomes 0 = x0(1 + t=e0) and 0 = x1(1 + t=e1). We have four cases to consider. If x0 = 0 and x1 = 0, the point is not on the ellipse; this case may be discarded. 2 If x0 = 0 and x1 6= 0, then t = −e1 and the only constraint on x1 is that (x0; x1) be a point on the ellipse, which means x1 = e1. The candidate closest point is (0; e1). 2 If x0 6= 0 and x1 = 0, then t = −e0 and the only constraint on x0 is that (x0; x1) be a point on the ellipse, which means x0 = e0. The candidate closest point is (e0; 0). 2 2 If x0 6= 0 and x1 6= 0, then t = −e0 and t = −e1, which is a contradiction because e0 6= e1; this case may be discarded. The only candidate ellipse points in the first quadrant closest to (0; 0) are (e0; 0) and (0; e1). Of these two, (0; e1) is closer. In summary: The closest ellipse point in the first quadrant to (0; 0) is the point (0; e1) with distance d = e1. 2.4 The Query Point is on the Vertical Axis 2 2 Let y0 = 0 and y1 > 0. Equation (6) becomes 0 = x0(1 + t=e0) and y1 = x1(1 + t=e1). We have two cases to consider. If x0 = 0, then for (x0; x1) to be on the ellipse, we need x1 = e1. The candidate closest point is (0; e1). 4 2 2 2 If x0 6= 0, then t = −e0 and y1 = x1(1 − e0=e1) ≤ 0, where the last inequality is a consequence of x1 ≥ 0 and e0 > e1. This contradicts the assumption that y1 > 0. The only candidate ellipse point is (0; e1). In summary: The closest in the first quadrant to (0; y1) for y1 > 0 is (0; e1) with distance d = jy1 − e1j. 2.5 The Query Point is on the Horizontal Axis 2 2 Let y0 > 0 and y1 = 0. Equation (6) becomes y0 = x0(1 + t=e0) and 0 = x1(1 + t=e1). We have two subcases to consider. If x1 = 0, then for (x0; x1) to be on the ellipse, we need x0 = e0. The candidate closest point is (e0; 0). The squared distance to this candidate is 2 2 d0 = (y0 − e0) (7) 2 2 2 2 2 2 If x1 6= 0, then t = −e1 and y0 = x0(1 − e1=e0). It follows that x0 = e0y0=(e0 − e1) ≤ e0. The last 2 2 inequality is a consequence of (x0; x1) being on the ellipse. The implication is that y0 ≤ (e0 − e1)=e0 < e0. Notice that this limits the construction to points (y0; 0) inside the ellipse. For points (y0; 0) with 2 2 y0 ≥ (e0 − e1)=e0, which includes points inside and outside the ellipse, the closest point is necessarily 2 2 p 2 (e0; 0). When y0 ≤ (e0−e1)=e0, for (x0; x1) to be on the ellipse, we may solve for x1 = e1 1 − (x0=e0) .