我试图通过设置和查找来简单地获取一个给定的x
by框y
并将其放大,反之亦然。这个公式将如何用 Python 表示(为了便于阅读)。我试图把这个盒子装在一个更大的盒子里,这样内盒总是能装在更大的盒子里。x
y
问问题
4322 次
3 回答
6
new_y = (float(new_x) / x) * y
或者
new_x = (float(new_y) / y) * x
于 2011-05-16T21:12:38.150 回答
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
于 2011-05-16T21:16:23.977 回答
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)
>>>
于 2011-05-16T21:14:49.727 回答