找到宽高比的最佳方法

时间:2011-04-08 19:47:02

标签: graphics haskell

我正在用Haskell编写一个简单的OpenGL游戏。每当用户调整窗口大小时,我都会得到它们的宽度和高度。我需要计算适合其窗口的最大宽度和高度,同时保持W / H比率为1.6。

这就是我写的。它有效,但我不认为这是在Haskell中做到这一点的最佳方式。有人可以建议一些替代方案:

fixedRatio = 1.6

keepRatio (w,h) = head [(floor w',floor h') | w' <- [w, h*fixedRatio], h' <- [h, w/fixedRatio], w' <= w, h' <= h, w'/h' == fixedRatio ]

2 个答案:

答案 0 :(得分:4)

我是以警卫(条件)来做的:

keepRatio (w,h) | w > expectedWidth = (floor expectedWidth, h)
                | otherwise         = (w, floor(fromIntegral w / fixedRatio))
                where expectedWidth = fromIntegral h * fixedRatio

答案 1 :(得分:1)

试试这个:

keepRatio (w,h) = min sizeByWidth sizeByHeight
   where sizeByWidth = (w, (w * 5) `div` 8)
         sizeByHeight = ((h*8) `div` 5, h)

这假设您只需要纵横比到最近的像素。

相关问题