我正在尝试使用jQuery解决矩形问题。基本上,我有3个方框,固定在一个更大的盒子里。
我知道较大的盒子的最大宽度为500px。我不知道1,2或3的尺寸是多少 - 但我知道它们的纵横比可以改变,并且它们必须按比例放入盒子中。
我如何始终确保无论什么形状--3个矩形在500px盒子内总是按比例调整大小?
答案 0 :(得分:5)
在求解方程组之后,我相信以下是正确的。
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和a3是已知的。解决w1,然后解决w2和w3。使用a1,a2和a3来确定h1,h2和h3。
我相信你的算法应该是:
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)
以下是我遵循的步骤
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)