#### Python: How to calculate intersection point between lines and ellipses using only math equation

I want to calculate the intersection points between an ellipse and a line using python.
However how do I do it using only mathematical quadratic equation, when given both end points of the line and the center point and radii of the ellipse?

None of the solutions on stackoverflow describe the solution I’m looking for.
They use either numpy, scipy and other libraries or use triggernometry – none of them use a purely mathematical process.

Comparing the solution in this post for example:

You have the ellipse formula:

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

And the line formula:

y = m*x + c

The solution in linked post describes the quadratic formula derived from it on rearrangement:

A=a2m2+b2

B=2a2m(c-k)-2b2h

C=b2h2+a2(c-k)2-a2b2

I want to solve this equation using only math, solving for x, and afterwards solving for y using the line formula.

However when using above formula, the resulting intersection points are erroneous and do not lie on the ellipse or the line.

This is the code I tried (I excluded the part that visualizes the code because of moderator’s criticism who shut down my previous post):

``````from math import *
# use line formula to calculate m and c:
x1, y1, x2, y2 = *line_point_a, *line_point_b
m = (y1 - y2) / (x1 - x2) if not x1 - x2 == 0 else 0
c = y2 - (m * x2)

A = (rad_y ** 2) + ((rad_x ** 2) * (m ** 2))
B = (2 * (rad_x ** 2) * m * (c - center_y)) - 2 * (rad_y ** 2) * center_x
C = (rad_y ** 2) * (center_x ** 2) + (rad_x ** 2) * (c - center_y) * 2 - (rad_x ** 2) * (rad_y ** 2)