# clamp a 2D coordinate to fit within an ellipse

I need to clamp a 2D coordinate to fit within an ellipse.

Call of Duty: Modern Warfare 2 does something similar where capture points are translated from a 3D vector in the world to a 2D screen coordinate and then the 2D coordinates are clamped within an ellipse.

When the capture points are in view they're within the bounds of the ellipse.

When they're behind you they are clamped to be within the bounds of the ellipse.

Given a 2D coordinate that could be off screen, etc, what is the math behind clamping it within an ellipse?

• The ellipse is 2D too, right? If not, what plane is it in? – Josh Aug 10 '16 at 18:44
• Yes, the ellipse is 2D. – Rocky Breslow Aug 10 '16 at 18:48
• Can you scale the points and the ellipse to make the ellipse into a circle, clamp to the circle (relatively easy), then scale the points back? – amitp Aug 10 '16 at 19:17
• @amitp: Why? Just use the well known equation for an ellipse at the origin and just use a simple convolution by the transformation that takes it to the origin. – Pieter Geerkens Aug 10 '16 at 22:24
• simple: change coordinates so the ellipse is at 0,0 and round; limit vector length; change coordinates back. – Sarge Borsch Aug 11 '16 at 10:25

Here's a general method to clamp a point $$\A\$$ to the ellipse of center $$\C\$$ and half-dimensions $$\d\$$ (i.e. the ellipse is $$\2d_x\$$ wide and $$\2d_y\$$ high):

• $$\B = \frac{(A - C)}{d}\$$ gives you the coordinates of $$\A\$$ in the frame in which the ellipse is the unit circle. The fraction is memberwise division ($$\ \frac{a}{b} = (\frac{a_x}{b_x} ; \frac{a_y}{b_y})\$$)

• $$\B' = \frac{B}{|B|}\$$ normalizes $$\B\$$: $$\B'\$$ is now on the unit circle.

• $$\B'' = C + B' × d\$$, where $$\×\$$ is the memberwise product. This brings our clamped point back into the original frame : $$\B''\$$ is $$\A\$$, clamped onto the ellipse.

This method can be greatly simplified if the ellipse is centered ($$\C\$$ disappears), and/or if your frame has the same aspect ratio as your ellipse ($$\d_x = d_y\$$, so the scaling isn't necessary).

The equation for an ellipse centered at the origin is just:

$$\bigg( \frac{x ^ 2}{Rx ^ 2}\bigg) + \bigg(\frac{y ^ 2}{Ry ^ 2} \bigg) = 1$$

where:

• Rx is the X-dimension radius; and

• Ry is the y-dimension radius

Combining this with a transformation that maps the relevant ellipse to the origin is straightforward.

Then just replace the = sign with a <= to get a test for (inclusive) containment in the ellipse.

• right, I've gotten that far but all that lets us know is if the point is inclusive or exclusive in the ellipse, not where the closest point on the ellipse – Rocky Breslow Aug 10 '16 at 23:48
• extrude it: take the normal vector of the point to the center of the ellipse and then extrude it to the intersection point on the edge of the ellipse – CobaltHex Aug 11 '16 at 1:50
• @RockyBreslow: That's just going to be the intersection of the line segment from origin to point and the equation of the ellipse. – Pieter Geerkens Aug 11 '16 at 4:34