1
\$\begingroup\$

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.

coordinates that are on screen

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

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

\$\endgroup\$
  • \$\begingroup\$ The ellipse is 2D too, right? If not, what plane is it in? \$\endgroup\$ – Josh Aug 10 '16 at 18:44
  • 1
    \$\begingroup\$ Yes, the ellipse is 2D. \$\endgroup\$ – Rocky Breslow Aug 10 '16 at 18:48
  • \$\begingroup\$ 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? \$\endgroup\$ – amitp Aug 10 '16 at 19:17
  • \$\begingroup\$ @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. \$\endgroup\$ – Pieter Geerkens Aug 10 '16 at 22:24
  • \$\begingroup\$ simple: change coordinates so the ellipse is at 0,0 and round; limit vector length; change coordinates back. \$\endgroup\$ – Sarge Borsch Aug 11 '16 at 10:25
2
\$\begingroup\$

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).

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 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 \$\endgroup\$ – Rocky Breslow Aug 10 '16 at 23:48
  • 1
    \$\begingroup\$ 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 \$\endgroup\$ – CobaltHex Aug 11 '16 at 1:50
  • \$\begingroup\$ @RockyBreslow: That's just going to be the intersection of the line segment from origin to point and the equation of the ellipse. \$\endgroup\$ – Pieter Geerkens Aug 11 '16 at 4:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.