将N#的正方形调整为尽可能大的尺寸,同时仍将X尺寸乘以Y尺寸. (缩略图!) [英] Resizing N # of squares to be as big as possible while still fitting into box of X by Y dimensions. (Thumbnails!)

查看:70
本文介绍了将N#的正方形调整为尽可能大的尺寸,同时仍将X尺寸乘以Y尺寸. (缩略图!)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有N个方格. 我有一个矩形框. 我希望所有正方形都适合盒子. 我希望正方形尽可能大.

I have N squares. I have a Rectangular box. I want all the squares to fit in the box. I want the squares to be as large as possible.

如何计算正方形的最大尺寸,使它们都适合盒子?

How do I calculate the largest size for the squares such that they all fit in the box?

这是缩略图库中的缩略图.

This is for thumbnails in a thumbnail gallery.

int function thumbnailSize(
    iItems, // The number of items to fit.
    iWidth, // The width of the container.
    iHeight, // The height of the container.
    iMin // The smallest an item can be.
)
{
    // if there are no items we don't care how big they are!    
    if (iItems = 0) return 0;

    // Max size is whichever dimension is smaller, height or width.
    iDimension = (iWidth min iHeight);

    // Add .49 so that we always round up, even if the square root
    // is something like 1.2.  If the square root is whole (1, 4, etc..)
    // then it won't round up.
    iSquare = (round(sqrt(iItems) + 0.49));

    // If we arrange our items in a square pattern we have the same
    // number of rows and columns, so we can just divide by the number
    // iSquare, because iSquare = iRows = iColumns.
    iSize = (iDimension / iSquare);

    // Don't use a size smaller than the minimum.
    iSize = (iSize max iMin);

    return iSize;
 }

此代码目前可以正常运行.其背后的想法是采用矩形容器的最小尺寸,并假设该容器是该尺寸的正方形,然后假定我们具有相等数量的行和列,正好足以在其中容纳iItems正方形.

This code currently works OK. The idea behind it is to take the smallest dimension of the rectangular container, pretend the container is a square of that dimension, and then assume we have an equal number of rows and columns, just enough to fit iItems squares inside.

如果容器大多是方形的,则此功能非常有用.但是,如果您有一个长矩形,则缩略图会比可能的要小.例如,如果我的矩形是100 x 300,并且我有三个缩略图,则它应返回100,而应返回33.

This function works great if the container is mostly squarish. If you have a long rectangle, though, the thumbnails come out smaller than they could be. For instance, if my rectangle is 100 x 300, and I have three thumbnails, it should return 100, but instead returns 33.

推荐答案

可能不是最佳选择(如果可以,我还没有尝试过),但是我认为比当前的方法更好:

Probably not optimal (if it works which I haven't tried), but I think better than you current approach :

w:矩形的宽度

h:矩形的高度

n:图片数量

a = w * h:矩形的面积.

a = w*h : area of the rectangle.

ia =理想情况下图像的最大面积.

ia = a/n max area of an image in the ideal case.

il = sqrt(ia)在理想情况下的图像最大长度.

il = sqrt(ia) max length of an image in the ideal case.

nw = round_up(w/il):需要堆叠的图像数量.

nw = round_up(w/il): number of images you need to stack on top of each other.

nh = round_up(h/il):需要彼此堆叠的图像数量.

nh = round_up(h/il): number of images you need to stack next to each other.

l = min(w/nw,w/nh):要使用的图像长度.

l = min(w/nw, w/nh) : length of the images to use.

这篇关于将N#的正方形调整为尽可能大的尺寸,同时仍将X尺寸乘以Y尺寸. (缩略图!)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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