Check if ellipse lies inside rectangle

conic sectionsellipsoidselliptic-equationsrectangles

I have Ellipse center Cx, Cy and radius (major radius Rx and minor radius Ry) with an angle of α (or α = rotation).

Rectangle cordinates are (x1,y1), (x2,y2), (x3,y3) and (x4,y4).

The Ellipse can be completely outside rectangle without touching the rectangle

How can i check if the ellipse is within a rectangle completely?

Check this link

Best Answer

First of all locate the foci $F_1$, $F_2$ of the ellipse (they lie on the major axis, at a distance $\sqrt{R_x^2-R_y^2}$ from the center).

Compute then the minimum sum of the distances from the foci to a point on a side of the rectangle. To this end, just reflect $F_1$ about a side of the rectangle to $F_1'$ and compute length $F_1'F_2$; do that for all four sides and pick the minimum value $m$.

If $m\le 2R_x$, then either the ellipse intersects the border of the rectangle (second case in your picture) or the rectangle is completely inside the ellipse (to see if this is the case, just check that the sum of the distances from the foci to any vertex of the rectangle is $\le 2R_x$).

Otherwise, the ellipse is either completely outside or inside the rectangle: to check which case it is, just find if the center of the ellipse is inside the rectangle or not.