我想简单地将给定的 x
乘以 y
的盒子放大,可以通过设置 x
并找到 y
或反之亦然来实现。为了可读性,请问这个公式在 Python 中应该如何表达。我正在尝试将此框适配到一个更大的框中,以便内部框始终适合于较大的框中。
我想简单地将给定的 x
乘以 y
的盒子放大,可以通过设置 x
并找到 y
或反之亦然来实现。为了可读性,请问这个公式在 Python 中应该如何表达。我正在尝试将此框适配到一个更大的框中,以便内部框始终适合于较大的框中。
new_y = (float(new_x) / x) * y
or
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)
>>>