计算长宽比的算法是什么? [英] What's the algorithm to calculate aspect ratio?

查看:385
本文介绍了计算长宽比的算法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我计划将其与JavaScript一起使用,以裁剪图像以适合整个窗口。

I plan to use it with JavaScript to crop an image to fit the entire window.

编辑:我将使用第三方组件,仅接受以下格式的长宽比: 4:3 16:9

Edit: I'll be using a 3rd party component that only accepts the aspect ratio in the format like: 4:3, 16:9.

推荐答案

我收集到您正在寻找可用的宽高比 integer:integer 16:9 这样的解决方案,而不是 float:1 这样的解决方案,例如 1.77778:1

I gather you're looking for an usable aspect ratio integer:integer solution like 16:9 rather than a float:1 solution like 1.77778:1.

如果是这样,您需要做的是找到最大公约数(GCD)并将其除以。 GCD是将两个数字均分的最高数字。因此,6和10的GCD为2,44和99的GCD为11。

If so, what you need to do is find the greatest common divisor (GCD) and divide both values by that. The GCD is the highest number that evenly divides both numbers. So the GCD for 6 and 10 is 2, the GCD for 44 and 99 is 11.

例如,一个1024x768显示器的GCD为256。值得到的结果是4x3或4:3。

For example, a 1024x768 monitor has a GCD of 256. When you divide both values by that you get 4x3 or 4:3.

A(递归)GCD算法:

A (recursive) GCD algorithm:

function gcd (a,b):
    if b == 0:
        return a
    return gcd (b, a mod b)

在C:

static int gcd (int a, int b) {
    return (b == 0) ? a : gcd (b, a%b);
}

int main(void) {
    printf ("gcd(1024,768) = %d\n",gcd(1024,768));
}

这是一些完整的HTML / Javascript,显示了一种检测屏幕尺寸的方法并据此计算宽高比。这在FF3中有效,我不确定其他浏览器对 screen.width screen.height 有什么支持。 / p>

And here's some complete HTML/Javascript which shows one way to detect the screen size and calculate the aspect ratio from that. This works in FF3, I'm unsure what support other browsers have for screen.width and screen.height.

<html><body>
    <script type="text/javascript">
        function gcd (a, b) {
            return (b == 0) ? a : gcd (b, a%b);
        }
        var w = screen.width;
        var h = screen.height;
        var r = gcd (w, h);
        document.write ("<pre>");
        document.write ("Dimensions = ", w, " x ", h, "<br>");
        document.write ("Gcd        = ", r, "<br>");
        document.write ("Aspect     = ", w/r, ":", h/r);
        document.write ("</pre>");
    </script>
</body></html>

它输出(在我怪异的宽屏显示器上):

It outputs (on my weird wide-screen monitor):

Dimensions = 1680 x 1050
Gcd        = 210
Aspect     = 8:5

我在上面测试过的其他产品

Others that I tested this on:

Dimensions = 1280 x 1024
Gcd        = 256
Aspect     = 5:4

Dimensions = 1152 x 960
Gcd        = 192
Aspect     = 6:5

Dimensions = 1280 x 960
Gcd        = 320
Aspect     = 4:3

Dimensions = 1920 x 1080
Gcd        = 120
Aspect     = 16:9

我希望自己家里有最后一个,但不,这是一台工作机不幸的是。

I wish I had that last one at home but, no, it's a work machine unfortunately.

如果您发现图形调整大小工具不支持长宽比,该怎么办。我怀疑最好的选择是添加字母装订线(就像您在旧电视上观看宽屏电影时在顶部和底部看到的那样)。我将它们添加到顶部/底部或侧面(以导致信箱装箱数量最少的一个为准),直到图像满足要求为止。

What you do if you find out the aspect ratio is not supported by your graphic resize tool is another matter. I suspect the best bet there would be to add letter-boxing lines (like the ones you get at the top and bottom of your old TV when you're watching a wide-screen movie on it). I'd add them at the top/bottom or the sides (whichever one results in the least number of letter-boxing lines) until the image meets the requirements.

一个您可能要考虑的是一张从16:9更改为5:4的图片的质量-我仍然记得在引入信箱技术之前,我在电视上看过的那年高个子,瘦瘦的牛仔。您最好在每个宽高比上拥有一张不同的图像,而只是将实际尺寸调整为正确的尺寸,然后再通过网络发送。

One thing you may want to consider is the quality of a picture that's been changed from 16:9 to 5:4 - I still remember the incredibly tall, thin cowboys I used to watch in my youth on television before letter-boxing was introduced. You may be better off having one different image per aspect ratio and just resize the correct one for the actual screen dimensions before sending it down the wire.

这篇关于计算长宽比的算法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆