Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Rectangle inscribed in another rectangle: solutions for all cases.

  1. Jun 21, 2011 #1
    Ciao to everybody,

    I've encountered frequently, in forums and news groups, questions about a rectangle inscribed in another rectangle, but nowhere a general discussion on ways to solve related problems (i.e., given three of the five quantities involved - 2 sides of outer rectangle, 2 sides of inner rectangle, and one of the two complementary angles between them - compute remaining two, or state that the problem has no solution).

    Probably the problem is felt too trivial to encourage somebody to afford difficulties - really NOT trivial - that are encountered in some of these cases.

    I did it, and attach below my solutions and some Fortran 95 subroutines and programs that allow for finding unknowns in each case - or for stating that the problem, with input data given, has no solution.

    I attach them as text files because proper formatting is relevant for text and essential to code.
    (Note: endlines unix style - under Windows use Notepad2 or Scite, not Notepad.)

    Hope this will help somebody in future.

    Of course, observations, corrections, and suggestions for improvements, are welcome.

    Code has been written by myself and I release it in the public domain.

    june 2011
    Tommaso Russo, Trieste(Italy)

    Rectangle inscribed in another rectangle: solutions for all cases.

    View attachment InscribedRectangles-discussion.txt

    Subroutines and test programs (Fortran 95):

    View attachment InscribedRectangles-fortran95code.txt

  2. jcsd
  3. Aug 21, 2012 #2
    So after reading through the three cases mentioned in the TXT file posted, I am interested in the hardest one of course, which is case 3: Given A,B,Y, find X (and phi).

    In the summary, i get lost as to how Phi is obtained. I am actually desparate to know the answer to the initial question: If given the outside rectangle size, and only one dimension of the inscribed rectangle, how do I get the angle (first) and length of the inner rectangle (which would come easily after finding the angle)?
  4. Aug 21, 2012 #3
    again here is the picture used throughout the discusion

    Attached Files:

  5. Aug 21, 2012 #4


    User Avatar
    Science Advisor
    Homework Helper
    Gold Member
    2016 Award

    I get a quartic in terms of X/W, Y/W for the tan of the angle. What do you get?
  6. Aug 22, 2012 #5
    I guess that one thing you have not grasped is that, given A,B,Y, the solution for phi is not necessarily unique. You can obtain one to three values for phi that allow you constructing a rectangle XY thath turns out to be perfectly inscribed into AB. Of course, to each value found for phi corresponds a value for X, and all you have to do is to choose the value of phi for which X is maximum.

    Don't suicide, you got it! :-)

    The graphical method described in 3a) does exactly this: giving to gnuplot the following sequence of commands,
    Code (Text):

      A = 1        # replace with your input value
      Y = .8       # replace with your input value
      Binput = 1   # replace with your input value
      phi(x)=x     # phi = x, i.e. x-axis *is* angle phi
      X(x) = (A-Y*cos(phi(x)))/sin(phi(x))
      B(x) = Y*sin(phi(x)) + X(x)*cos(phi(x))
      a1(x)= Y*cos(phi(x))
      b1(x) = Y*sin(phi(x))
      set angles degrees
      set xrange [0:90]    # phi must be acute
      set yrange [0:2*Binput]
      set samples 10000    # enough for a good resolution,
          #  low enough to make zooms in less than 1 second
      plot B(x), Binput, X(x), a1(x), b1(x)
    you find one to three good values for phi (that here is x, on x axis), simply looking at intersections between B(x) and Binput(x): you can read them with precision of some digits, zooming in a narrow rectangle around intersection points. For each found value of phi, the corresponding value of X stays on the curve X(x). Choose the best value for phi, that corresponds to largest X.

    For a better precision, you can use fortran subroutine find_angles_numerically (described in method 3c) that finds intersection points of the same graph by bisection (output variable ns contains number of found solutions), and then compute corresponding values for X as (A - Y*cos(phi)) / sin(phi).

    Using method 3b, instead, you find first X with routine find_max_X_side_analytically, that solves a quartic equation that returns four complex values, and chooses the largest of the real positive ones. As a byproduct, a1 is also obtained , and phi is computed as arcos(a1/Y).

    I used a similar one, but unfortunately, we have both used X and Y with a different meaning, and numbered differently segments that make up the sides. Correspondences are:

    your X → my A
    Y → B
    x1 → a1
    x2 → a2
    y2 → b1
    y1 → b2
    w → X
    h → Y
    theta → 90°-phi
    Last edited: Aug 22, 2012
  7. Aug 22, 2012 #6
    The problem is me. I am just a guy in the shop making various size panels that has a cross strap from corner to corner. I was trying to detail in Autocad the length of the strap for production purposes. That said, I guess it is obviuos that I am not a math major.

    Is it possible to input the formula(s) into Excel? If I can get that far, I can use a LISP routine in Autocad to pop in the strap.
  8. Aug 22, 2012 #7
    No problem. You can use Pythagorean Theorem without knowing any of its many proofs.

    What you need is exactly subroutine find_angles_numerically; and other subroutines it calls. In short, all fortran subprograms written for case 3c).

    find_angles_numerically(A,Binput,Y, phiout,ns), accepts in input A,B,Y and returns ns (= number of solutions, 1 to 4) and a vector phiout containing solutions for phi (warning: in radians, not degrees.)

    So, you have no more a mathematical problem, just a technical one: how to use some fortran 95 subprogram for your purpose?

    Unfortunately find_angles_numerically is not a simple formula, but an iterative algorithm that finds solutions by closer and closer approximations. I don't know if Excel programming language is powerful enough to translate into it a fortran subprogram: may be. LISP for sure is.

    However, translations into another language generally is not a good idea. find_angles_numerically, and functions it calls, have been proved for correctness, and tested thorougly to catch possible typos. In translating it, it is easy to introduce subtle bugs. If you choose this way, I suggest you to use some tool for automatic translation, like this:
    (Don't ask me where to download such a program, I don't know, and beware: translated programs usually require some hand made adjustment.)

    But, if you can call a LISP routine from Autocad, 99% you can also call a LISP routine that in turn calls a fortran subprogram, acting as a "wrapper". Probably this is the easiest, safest and fastest way. Probably you'll need some assistance from a skilled programmer knowing your programming environment, to join together LISP and fortran programs, but this has to be done once forever.

    Good luck
    Last edited: Aug 22, 2012
  9. Aug 22, 2012 #8


    User Avatar
    Science Advisor
    Homework Helper
    Gold Member
    2016 Award

    Then a quartic equation probably doesn't help you either. Fwiw, it's
    (X-Yt)2(1+t2) = W2(1-t2)2
    where X and Y are the dimensions of the outer rectangle, W is one side of the inner rectangle and t is tan(phi).
    (I'm intrigued that Tommaso says there are three solutions for phi. From a quartic you'd expect four. Maybe I've missed a cancellation.)
  10. Aug 22, 2012 #9
    It's simple: the fourth solution for phi is always out of the allowed range 0°-90°, and corresponds to a negative side for the inner rectangle.

    If you give to gnuplot the command sequence given above, but replacing set xrange [0:90] with set xrange [-360:360], you'll understand immediately why (when the given side of the inner rectangle is less than at least one side of the outer one, i.e. the problem is solvable) phi has always at least two real solutions (one between 0° and 90° and the other between 180° and 270°). For what concerns the remaining two, they can be complex conjugate, real and coinciding, real and distinct, depending on the height of the local maximum of B(phi) between 0° and 90°. (Look at curve B(x) on the plot and its intersections with the straight line Binput.)


    More precisely (I've just realized that) if phi1 is the first solution and X1 is the corresponding side for inner rectangle, then phi4 = phi1 + 180°, and X4 = -X1. Algebraically, phi4 and X4 are solutions, but geometrically they have the same meaning of phi1 and X1.
    Last edited: Aug 22, 2012
  11. Aug 22, 2012 #10


    User Avatar
    Science Advisor
    Homework Helper
    Gold Member
    2016 Award

    Yes, sorry, it doesn't surprise me that some of the four solutions are either invalid or reversed sign; what surprised me was that it would be only one of the four rather than a pair.
  12. Aug 23, 2012 #11
    Ah, OK. I understand well your surprise, because I too wondered why phi2+180° and phi3+180° are not solutions with a reverse sign for a side of inner rectangle: if real, Phi2 and phi3 are true solutions, I give an example below. phi2+180° and phi3+180° are solutions of same equations, if you revert the sign of one side of outer rectangle :-)

    if outer rectangle is a square of side 1, and you want inscribe in it a rectangle with one side 0.8, you can draw two 0.8 segments orthogonal to a diagonal of outer square (and the second inner side turns out to be 0.6142...) but also insert in it an 0.8 × 0.8 square and rotate it clockwise or counterclockwise until its vertexes touch outer sides. Solutions for phi are truly 3: decreasing the given inner side, they get closer and closer until they fuse in a solution of multiplicity 3 for sqrt(1/2) (B(phi) has there a flex at 45°).
  13. Aug 23, 2012 #12
    You should be able to solve this using excel. In particular the Goal seek functionality should be able to solve this by only entering simple formulas for how the relationships that sides and angles have.
  14. Aug 25, 2012 #13


    User Avatar
    Science Advisor
    Homework Helper
    Gold Member
    2016 Award

    Finally found a case where all four solutions are meaningful: W = 9, X = 10, Y = 11.
    The four solutions for the angle are near 6 degrees, 44 degrees, 61 degrees and 69 degrees.
  15. Aug 26, 2012 #14
    Sorry, but near 44° there is no geometrically valid solution. I suppose you have found 43.767° as arctan of one of the solutions of your quartic in t. But that solution for t is also tan(180°+43.767°), and is that value that solves original equations, but with a negative value for the inner unknown side. Remember that I use A,B for sides of outer rectangle, and X,Y for sides of the inner one: for phi=43.767° there in no value of the inner unknown side X that can fit in original equations. In facts,

    10 = 9 cos(43.767°) + X sin(43.767°)
    gives X = 5.061, while

    11 = 9 sin(43.767°) + X cos(43.767°)
    gives X = 6.611.

    Assuming that outer sides were interchanged above,

    11 = 9 cos(43.767°) + X sin(43.767°)
    gives X = 6.506, while

    10 = 9 sin(43.767°) + X cos(43.767°)
    gives X = 5.227.

    I say that geometrically meaningful solution can be at most three because I proven it: proof is on last page of the text I attached to my first post:


    However, the proof can be grasped intuitively, at least in case Y ≤ A ( or Y ≤ B, interchanging outer sides) looking at the following picture:


    Given a (semi)infinite rectangle of base A, we can insert in it the given side Y so that its endpoins fall on A and one of the two half-lines (no matter which, for symmetry). For example, at positions Y1 and Y2. To each position corresponds a different angle phi between A and Y, that varies obviously between 0° an 90°, and, if phi is not 0, drawing obvious orthogonal lines (dashed in the picture), we can build the only inner and outer rectangles that could be inscribed, the former in the latter, with the given phi. The value needed for B is the distance from A of the farthest inner vertex (in the example, B1 and B2).

    Of course, for phi→0, B(phi) tends to infinity, B(90°) = Y, and for phi→90 B tends to Y+ (from right:. i.e., B(90°-dphi) - B(90°) > 0).

    This is enough to state that, in its graph, every horizontal line B=constant (= Brequested) intersects it in an even number of points (counting their multiplicity). This number cannot be 5, otherwise we would have a quartic with 5 solutions or more.

    (And, of course, I was wrong when I said in an addendum "I realized that, if phi1 is the first solution ..., then phi4 = phi1 + 180°". This holds true only if A and B are interchangeable, i.e. outer rectangle is a square.)

    Attached Files:

    Last edited: Aug 26, 2012
  16. Aug 27, 2012 #15


    User Avatar
    Science Advisor
    Homework Helper
    Gold Member
    2016 Award

    Ah, ok. Thanks.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Discussions: Rectangle inscribed in another rectangle: solutions for all cases.
  1. Diagonal of rectangle (Replies: 2)

  2. Rectangle in a circle (Replies: 5)