以最佳可能的二维尺寸对一维数组进行排序? [英] Sort a one dimensional array in the best possible two dimensional size?
问题描述
Hey Codeproject,
我有一个我正在渲染的图像列表,现在阵列的长度可能会不时变化,但是按顺序不仅要使用新的Bitmap(Width * Images.Length,Height)我想以矩形的最佳尺寸对所有这些图像进行排序。
有谁知道如何做到这一点?
目前我的代码是:
Hey Codeproject,
I have a list of images that I'm rendering, now the length of the array may vary from time to time, but in order to not only use new Bitmap(Width * Images.Length, Height) I'd like to sort all these images in the best possible size for a rectangle.
Does anyone have any idea on how to do this?
Currently my code is:
Bitmap CombinedImages = new Bitmap(MaxWidth * Images.Count, MaxHeight);
for (int i = 0; i < Images.Count; i++)
{
g.DrawImage(Images[i], MaxWidth * i, 0, Images[i].Width, Images[i].Height);
}
我宁愿有两个for循环所以我可以将它们全部放在一个正方形而不是仅仅单行。
我尝试了什么:
我'尝试使用模块化功能:
I'd much rather have a two for loops so I can put them all in a square instead of just a single line.
What I have tried:
I've tried using the modular function:
int bestColumn = Images.Count;
while(Images.Count % 2 != 0)
{
bestColumn += 1;
}
int Columns = BestColumn / 2;
int Rows = BestColumn / 2;
我不知道下一步该做什么。
I'm not sure what to do next.
推荐答案
这很复杂!从这里开始:包装问题 - 维基百科,免费的百科全书 [ ^ ]并遵循一些相关链接。
It's quite complicated! Start here: Packing problems - Wikipedia, the free encyclopedia[^] and follow a few of the relevent links.
现在找到一个有效的解决方案。
Found a working solution for now.
int bestColumn = (int)Math.Ceiling(Math.Sqrt(Images.Count)) * 2;
int column = bestColumn / 2;
for (int i =0; i< column; i++)
{
for (int j = 0; j < column; j++)
{
if(j * column + i >= 0 && j * column + i < Images.Count)
{
这篇关于以最佳可能的二维尺寸对一维数组进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!