I_really_love_MSVC I_really_love_MSVC - 2 months ago 12
C# Question

How to find a point where a line intersects an ellipse in 2D (C#)

I need to find a point where a line (its origin is ellipse' center) intersects an ellipse in 2D... I can easily find a point on a circle, because I know an angle F and the circle' radius (R):

x = x0 + R * cosF
y = y0 + R * sinF


However I just can't figure how am I supposed to deal with an ellipse... I know it's dimensions (A & B), but what is the way of finding parameter T?!

x = x0 + A * cosT
y = y0 + B * sinT


From what I understand the parameter T (T angle) is not far from the F angle (approximately +-15 degrees in some cases), but I just can't figure how to calculate it!!!

If there is a kind hearted soul, please help me with this problem...

Answer

The standard equation of an ellipse, stationed at 0,0, is:

1 = (x)^2 / (a)    +  (y)^2 / (b)

Where a is 1/2 the diameter on the horizontal axis, and b is 1/2 the diameter on the vertical axis.

you have a line, assuming an equation:

y = (m)(x - x0) + y0

So, let us plug-and-play!

1 = (x)^2 / (a)   +   (m(x - x0) + y0)^2 / (b)

1 = x^2 / a  +  (mx + (y0 - mx0))^2 / b

1 = x^2 / a  +  (m^2 * x^2 + 2mx*(y0 - mx0) + (y0 - mx0)^2) / b

1 = x^2 / a  + (m^2 x^2) / b + (2mx*(y0 - mx0) + (y0^2 - 2y0mx0 + m^2*x0^2)) / b

1 = ((x^2 * b) / (a * b)) + ((m^2 * x^2 * a) / (a * b)) + (2mxy0 - 2m^2xx0)/b + (y0^2 - 2y0mx0 + m^2*x0^2)/b

1 = ((bx^2 + am^2x^2)/(ab)) + (x*(2my0 - 2m^2x0))/b + (y0^2 - 2y0mx0 + m^2*x0^2)/b

0 = x^2*((b + a*m^2)/(ab)) + x*((2my0 - 2m^2x0)/b) + (((y0^2 - 2y0mx0 + m^2*x0^2)/b) - 1)

That last equation follows the form of a standard quadratic equation.

So just use the quadratic formula, with:

((b + a*m^2)/(ab))
((2my0 - 2m^2x0)/b)
and 
(((y0^2 - 2y0mx0 + m^2*x0^2)/b) - 1)

to get the X values at the intersections; Then, plug in those values into your original line equation to get the Y values.

Good luck!