0
\$\begingroup\$

I have a vector (white line), a centre and a radius along each of the semi-major and semi-minor axes of an ellipse (green line). I need to find the point where the white line intersects the green.

I have tried to work it out using Atan2, Sin and Cos but that doesn't quite work (blue line). It works when parallel to an axis but doesn't otherwise, I have a feeling this is because I am trying to perform the calculation on an ellipse where the horizontal and vertical radii are different.

Vector2 delta = (screenPos - canvasCentre).normalized;
float angle = Mathf.Atan2(delta.y, delta.x);
Vector2 position = canvasCentre + new Vector2(
                Mathf.Cos(angle) * radius.x,
                Mathf.Sin(angle) * radius.y);

enter image description here

\$\endgroup\$
1
\$\begingroup\$

First, we invert the squash/stretch of the ellipse, so we're working in "unit circle space"

Vector2 delta = (screenPos - canvasCentre);

delta.x /= radius.x;
delta.y /= radius.y;

Now we can normalize the vector to snap it onto the unit circle, then re-introduce our squash/stretch to map the result back onto our original ellipse:

delta = normalize(delta);
delta.x *= radius.x;
delta.y *= radius.y;

Vector2 position = canvasCentre + delta;
\$\endgroup\$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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