在另一个矩形中找到最大的16:9矩形

时间:2012-08-13 14:07:03

标签: lua rectangles

我正在研究这个Lua脚本,我需要能够在另一个没有特定宽高比的矩形中找到最大的16:9矩形。所以你能告诉我怎么做吗?你不必写Lua - 伪代码也可以。

谢谢!

我已经尝试过并且可以确认这对于较低比率的外部作用是不可行的。

if wOut > hOut then 
wIn = wOut
hIn = (wIn / 16) *9
else 
hIn = hOut
wIn = (hIn / 9) * 16 
end

2 个答案:

答案 0 :(得分:1)

heightCount = originalHeight / 9;
widthCount = originalWidth / 16;

if (heightCount == 0 || widthCount == 0)
    throw "No 16/9 rectangle";

recCount = min(heightCount, widthCount);

targetHeight = recCount * 9;
targetWidth = recCount * 16;

到目前为止,任何具有left = 0 ..(originalWidth - targetWidth)和top = 0 ..(originalHeight - targetHeight)和width = targetWidth和height = targetHeight的矩形都应满足您的要求。

答案 1 :(得分:0)

嗯,你的新矩形可以描述为:

h = w / (16/9)
w = h * (16/9)

您的新矩形应该基于外部矩形的宽度,所以:

h = w0 / (16/9)
w = w0

根据Lua如何使用数字,您可能需要确保它使用真正的除法而不是整数除法 - 上次我看起来是2001年,我的记忆力比咖啡变冷更快,但我似乎记住所有数字都是漂浮的......

相关问题