Results 1 to 4 of 4

Thread: [RESOLVED] calculate largest rectangle that fits in ellipse

  1. #1

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Resolved [RESOLVED] calculate largest rectangle that fits in ellipse

    how can i calculate the largest rectangle that fits in an ellipse + the 4 points that describe that rectangle?
    see image... the green rectangle is what i'm looking for:

    Name:  30-09-2012 16.46.51.jpg
Views: 1227
Size:  9.0 KB

  2. #2
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: calculate largest rectangle that fits in ellipse

    The ellipse in your case is given by the equation

    x^2 / A^2 + y^2 / B^2 = 1

    where 2A is the width and 2B is the height of the ellipse. You wish to find a point (p, q) on the ellipse such that the area pq is maximized. Solve the above equation for y to get an expression valid for y >= 0:

    y^2 / B^2 = 1 - x^2 / A^2
    y = B sqrt(1 - x^2 / A^2)

    The area pq is then just p*B sqrt(1 - p^2 / A^2). This is maximized when the derivative is zero, so differentiate with respect to p:

    (pq)' = B sqrt(1 - p^2 / A^2) + B p*1/2*(1 - p^2 / A^2)^(-1/2) * (-2p/A^2) = 0
    => (1 - p^2 / A^2) - p^2 / A^2 = 0
    => 1 = 2p^2 / A^2 and p^2 / A^2 = 1/2
    => p = A sqrt(2)/2
    => q = B sqrt(1 - 1/2) = B sqrt(2)/2

    The four corners are then just (+/- p, +/- q). The maximal area is 4pq = 2AB. The general case of a rotated, translated ellipse can be reduced to this case: take the center of the ellipse C, the axis X with constant A, the axis Y with constant B, and just move A*sqrt(2)/2 along the X axis or B*sqrt(2)/2 along the Y axis to get to your points. Computing these axes is another problem and depends on the form in which the ellipse is given. For the quadratic form, it's just completing the square.


    Alternative derivation:
    Consider a circle of radius 1. The area is maximized when p=q from symmetry and intuition. This occurs for points (p, q) = (sqrt(2)/2, sqrt(2)/2). Imagine scaling the vertical axis by B. Two rectangles pre-scaling have their area increased by a factor of B by the scaling, so the maximal-area rectangle is the same before and after scaling. Do the same with the horizontal axis, multiplying by A. The resulting points (p, q) are just (A sqrt(2)/2, B sqrt(2)/2), as I derived more rigorously from calculus above.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  3. #3

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: calculate largest rectangle that fits in ellipse

    i resolved it by calculating height * (4/3) and width * (4/3)
    it's a close enough approximation for my purposes

    thanks for answering

  4. #4
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: [RESOLVED] calculate largest rectangle that fits in ellipse

    Sure, though as my derivation shows, using height * sqrt(2) and width * sqrt(2) is exact. Yours is a decent approximation since

    sqrt(2) = 1.4142...
    4/3 = 1.3333...

    are reasonably close.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width