mathgraphicsgeometry

How do you calculate the axis-aligned bounding box of an ellipse?


If the major axis of the ellipse is vertical or horizontal, it's easy to calculate the bounding box, but what about when the ellipse is rotated?

The only way I can think of so far is to calculate all the points around the perimeter and find the max/min x and y values. It seems like there should be a simpler way.

If there's a function (in the mathematical sense) that describes an ellipse at an arbitrary angle, then I could use its derivative to find points where the slope is zero or undefined, but I can't seem to find one.

Edit: to clarify, I need the axis-aligned bounding box, i.e. it should not be rotated with the ellipse, but stay aligned with the x axis so transforming the bounding box won't work.


Solution

  • You could try using the parametrized equations for an ellipse rotated at an arbitrary angle:

    x = h + a*cos(t)*cos(phi) - b*sin(t)*sin(phi)  [1]
    y = k + b*sin(t)*cos(phi) + a*cos(t)*sin(phi)  [2]
    

    ...where ellipse has centre (h,k) semimajor axis a and semiminor axis b, and is rotated through angle phi.

    You can then differentiate and solve for gradient = 0:

    0 = dx/dt = -a*sin(t)*cos(phi) - b*cos(t)*sin(phi)
    

    =>

    tan(t) = -b*tan(phi)/a   [3]
    

    Which should give you many solutions for t (two of which you are interested in), plug that back into [1] to get your max and min x.

    Repeat for [2]:

    0 = dy/dt = b*cos(t)*cos(phi) - a*sin(t)*sin(phi)
    

    =>

    tan(t) = b*cot(phi)/a  [4]
    

    Lets try an example:

    Consider an ellipse at (0,0) with a=2, b=1, rotated by PI/4:

    [1] =>

    x = 2*cos(t)*cos(PI/4) - sin(t)*sin(PI/4)
    

    [3] =>

    tan(t) = -tan(PI/4)/2 = -1/2
    

    =>

    t = -0.4636 + n*PI
    

    We are interested in t = -0.4636 and t = -3.6052

    So we get:

    x = 2*cos(-0.4636)*cos(PI/4) - sin(-0.4636)*sin(PI/4) = 1.5811
    

    and

    x = 2*cos(-3.6052)*cos(PI/4) - sin(-3.6052)*sin(PI/4) = -1.5811