我试图通过设置x
并找到y
,然后通过设置x
来简单地按y
{{1}}进行扩展。如何用Python表达这个公式(为了便于阅读)。我试图将这个盒子放在一个更大的盒子里面,这样内盒子总能放在更大的盒子里面。
答案 0 :(得分:6)
new_y = (float(new_x) / x) * y
或
new_x = (float(new_y) / y) * x
答案 1 :(得分:6)
注意:我不是真的做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
答案 2 :(得分:0)
>>> 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)
>>>