# Check if a point is inside an ellipse which is inscribed in a rectangle

I have a rectangle with points $$A, B, C, D$$ which doesn't have to be axis-aligned. This rectangle contains an ellipse, whose vertices and co-vertices are touching the rectangle. Given a point $$P$$, how can I check whether this points is inside or outside of the ellipse?

• Do you know the coordinates of $A,B,C,D?$ If you can get the equation of the ellipse, this is easy. – saulspatz Jul 20 at 16:25
• What have you tried? Where did you come to your difficulties? – David G. Stork Jul 20 at 16:27
• yes, i want to find out if the point is inside of the ellipse with given points $A, B, C, D$ and $P$ – Orbit Jul 20 at 16:27
• Could you do it if the rectangle were centered at the origin, with sides parallel to the axes? – saulspatz Jul 20 at 16:30

You can easily find the foci of your ellips because you have the major and the minor axes of the ellipss.

All you have to do is to find the total distances from your point to the foci and compare with the major axis.

If the total is less than the major axis the point is inside the ellips otherwise it is not.

The first thing to do is to translate the picture so that the rectangle is centered at the origin. Compute the center $$(x_0,y_0)$$ of the rectangle, and subtract $$(x_0,y_0)$$ from each of $$A,B,C,D,P.$$ (I'll still call the new points $$A,B,C,D,P.$$) We know that an ellipse centered at the origin has an equation of the form$$ax^2+bxy+cy^2=1\tag{1}$$ and since we know four points on the ellipse, we can substitute three of them into $$(1)$$ and get three linear equation for the three unknowns $$a,b,c$$.

At this point, it's done. Substitute the coordinates of $$P$$ in the left-hand side of $$(1)$$. $$P$$ is inside, on, or outside the ellipse, according as the value obtained is less than, equal to, or greater than $$1$$.

Let $$\Delta_{ABP}$$, $$\Delta_{BCP}$$, $$\Delta_{CDP}$$, $$\Delta_{DAP}$$ be the signed area of the corresponding triangles (you can compute them using shoelace-formula). Their sum

$$\square = \Delta_{ABP} + \Delta_{BCP} + \Delta_{CDP} + \Delta_{DAC}$$ will be the signed area of rectangle ABCD. Define $$u = \frac{2}{\square}(\Delta_{ABP} - \Delta_{CDP}) \quad\text{ and }\quad v = \frac{2}{\square}(\Delta_{BCP} - \Delta_{DAP})$$ The condition that $$P$$ falls on/inside/outside the ellipse is $$u^2 + v^2 = 1$$, $$< 1$$ and $$> 1$$ respectively.

The basic idea behind this is we use a linear transform to map the rectangle/ellipse into a square/circle centered at origin. In this new coordinate system (let's call it $$(u,v)$$), the equation of the ellipse becomes $$u^2 + v^2 = 1$$. We then express $$(u,v)$$ in terms of the area of the triangles. Since the ratio of area of different shapes in invariant under linear transform, we can use the same ratio in any coordinate system to compute the coordinates $$(u,v)$$.

By the way, above formula also works when $$ABCD$$ is a parallelogram.

Here the characterisation of the ellipse $$\quad\dfrac{x^2}{a^2}+\dfrac{y^2}{b^2}\le 1\quad$$ should work nicely.

First calculate the center $$O=\frac{A+B+C+D}{4}$$

Then unit vectors are $$\vec{e_x}=\dfrac{\overrightarrow{AB}}{AB}$$ and $$\vec{e_y}=\dfrac{\overrightarrow{CB}}{CB}$$

Expressing all needed quantities : $$\quad a=\frac 12 AB\qquad b=\frac 12 CB\qquad x=\overrightarrow{OP}\cdot\vec{e_x}\qquad y=\overrightarrow{OP}\cdot\vec{e_y}$$

In summary: $$\dfrac{(\overrightarrow{OP}\cdot\overrightarrow{AB})^2}{AB^{\ 4}}+\dfrac{(\overrightarrow{OP}\cdot\overrightarrow{CB})^2}{CB^{\ 4}}\le \dfrac 14$$