Search code examples
jqueryarea

jQuery Box Prob


I am trying to solve a rectangle problem using jQuery. Basically, I have 3 square boxes which fix inside a larger box.

I know the larger box has a max-width of 500px. I do not know what the sizes of 1, 2 or 3 will be - but I know that their aspect ratio's can change and that they must fit proportionally into the box.

How would I go about always ensuring that no matter what shape - the 3 rectangles are always proportionally sized inside the 500px box ?


Solution

  • After solving the system of equations, I believe the following is true.

    w  = Overall width (500)
    h  = Overall height
    w1 = Width of B1
    h1 = Height of B1
    w2 = Width of B2
    h2 = Height of B2
    w3 = Width of B3
    h3 = Height of B3
    a1 = Aspect ratio of B1 (width/height)
    a2 = Aspect ratio of B2 (width/height)
    a3 = Aspect ratio of B3 (width/height)
    
    w1 = (500/a2 + 500/a3) / (1/a1 + 1/a2 + 1/a3)
    

    500 px, a1, a2, and a3 are knowns. Solve for w1, then w2, and w3. Use a1,a2, and a3 to determine h1, h2, and h3.

    I believe your algorithm should be:

    1: Find w1
    w1 = (500/a2 + 500/a3) / (1/a1 + 1/a2 + 1/a3)
    
    2: Find w2 and w3
    w1+w2 = 500
    w1+w3 = 500
    
    3: Determine ideal h1, h2, and h3 via aspect ratio
    h1 = w1/a1
    h2 = w2/a2
    h3 = w3/a3
    
    4: Best-Fit h1, h2, and h3
    h = h1 = max(h2+h3, h1)
    h2 = h2 + ((h - (h2+h3))/2)
    h3 = h3 + ((h - (h2+h3))/2)
    

    Here are the steps I followed

    Eq1: w1/a1 = h1                              [aspect ratio]                
    Eq2: h1 = (h2 + h3)                          [see diagram]
    Eq3: h2 = w2/a2                              [aspect ratio]  
    Eq4: h3 = w3/a3                              [aspect ratio]  
    Eq5: w2 = 500 - w1                           [see diagram]
    Eq6: w3 = 500 - w1                           [see diagram]
    
    w1/a1 = h1                                   [Eq1]
    w1/a1 = (h2 + h3)                            [Eq2]
    w1/a1 = (w2/a2 + w3/a3)                      [Eq3, Eq4]
    w1/a1 = ((500 - w1)/a2 + (500 - w1)/a3)      [Eq5, Eq6]
    
    w1/a1 = 500/a2 - w1/a2 + 500/a3 - w1/a3
    w1/a1 + w1/a2 + w1/a3 = 500/a2 + 500/a3 
    w1 * (1/a1 + 1/a2 + 1/a3) = 500/a2 + 500/a3 
    w1 = (500/a2 + 500/a3) / (1/a1 + 1/a2 + 1/a3)