Rotating ellipse equation, solve for $x$

98 Views Asked by At

I want to get the coordinates of the intersection of an ellipse and a line.

The line has the equation: $$y=\frac{p-y_1}{q-x_1}\,(x-x_1)+y_1. $$

The numbers $x_1, p, y_1, q$ represent the coordinates of two points: $A(x_1, y_1)$ and $S(p, q).$

The ellipse has the equation: $$\left( \frac{ (x - p) \cos(\alpha) - (y - q) \sin(\alpha) }{a} \right)^{\!2} + \left( \frac{ (x - p) \sin(\alpha) + (y - q) \cos(\alpha) }{b} \right)^{\!2} = 1 .$$

Numbers $p, q$ represent the center of the ellipse $S(p, q), a$ is half of the ellipses' width, $b$ is half of the ellipses' height and $\alpha$ is the angle of rotation.

Basically I am trying to test if a dot is intersecting the ellipse by first calculating the line of the dot and ellipses' center then calculating the intersection of the ellipse and the line and finally checking if the distance between the dot and center is lower or equal than the distance between ellipses' center and the intersection.

Now this is really simple to do on paper but I am doing this for a game I am programming and I don't have any library that can solve an equation so I need a formula for the $x$ or $y$ coordinate of the intersection.

2

There are 2 best solutions below

0
On BEST ANSWER

The point is inside or on the ellipse iff

$$\left( \frac{ (x_1 - p) \cos(\alpha) - (y_1 - q) \sin(\alpha) }{a} \right)^{\!2} + \left( \frac{ (x_1 - p) \sin(\alpha) + (y_1 - q) \cos(\alpha) }{b} \right)^{\!2}\le 1 .$$

Don't use a line !

0
On

Let $$\phi=\arctan{q-y_1\over p-x_1}.$$ If $r$ is the distance between the center of the ellipse and an intersection with line $AS$, then plugging $x-p=r\cos\phi$ and $y-q=r\sin\phi$ into the equation of the ellipse yields: $$ r^2\left({\cos^2(\phi+\alpha)\over a^2}+{\sin^2(\phi+\alpha)\over b^2}\right)=1, $$ and you can solve for $r$.