簡體   English   中英

如何按比例(尊重縱橫比)縮放矩形?

[英]How to proportionally (respecting aspect ratio) scale a rectangle?

我想簡單地給定的盒子xy和規模時,通過設置x和查找y ,反之亦然。 如何用Python表達這個公式(為了便於閱讀)。 我試圖將這個盒子放在一個更大的盒子里面,這樣內盒子總能放在更大的盒子里面。

new_y = (float(new_x) / x) * y

要么

new_x = (float(new_y) / y) * x

注意:我不是真的做Python,所以這是偽代碼。

您需要的是兩個框的相對寬高比,因為它確定哪個新軸必須與新框的大小相同:

r_old = old_w / old_h
r_new = new_w / new_h

if (r_old > r_new) then
   w = new_w              // width of mapped rect
   h = w / r_old          // height of mapped rect
   x = 0                  // x-coord of mapped rect
   y = (new_h - h) / 2    // y-coord of centered mapped rect
else
   h = new_h
   w = h * r_old
   y = 0
   x = (new_w - w) / 2
endif
>>> import fractions
>>> x, y = 10, 10
>>> fix_rat = fractions.Fraction(x, y)
>>> fix_rat
Fraction(1, 1)
>>> x = 8
>>> if fractions.Fraction(x, y) != fix_rat:
    y = x / fix_rat #well instead of y you should put the last one that has been changed
                    #but this is just an example


>>> y
Fraction(8, 1)
>>> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM