我正在尝试为 2d bin 打包制作 FNF 算法,当我调用该方法时,我得到了错误的结果。请帮忙,我找不到问题。
private void FFF()
{
int xmax = 0;
int movex = 0;
int movey = 0;
int maxH = RectBin.Height;
int maxW = RectBin.Width;
List<Rectangle> rectsToDraw = new List<Rectangle>();
for (int i = 0; i < Shapes.Count; i++)
{
int height = Shapes[i].Height;
int width = Shapes[i].Width;
if ((movey + height) <= maxH)
{
rectsToDraw.Add(new Rectangle(movex, movey, width, height));
movey = movey + height;
if (xmax < movex + width)
{
xmax = movex + width;
}
}
else if ((xmax + width) <= maxW)
{
movex = xmax;
rectsToDraw.Add(new Rectangle(movex, 0, width, height));
movey = height;
xmax = movex + width;
}
else
{
Debug.Write("Message1");
break;
}
}
for (int j = 0; j < rectsToDraw.Count; j++)
{
Debug.Write(rectsToDraw[0]);
}
r2d = rectsToDraw;
}
rectBin 是一个公共矩形 (0,0, 300, 190),Shapes[] 是一个公共矩形列表。我在这里使用之前对形状进行排序:
private void button9_Click(object sender, EventArgs e)
{
Shapes.Sort((x,y) => ((y.Width.CompareTo(x.Width))));
}
当我在调试控制台中启动 FNF 时,我得到重复的列表项和 message1(形状中的第一个,不应该重复),当我应该得到 14 个项目时。
为了清楚起见,我不是在寻找算法,只是有人告诉我我在哪里犯了错误,以便 oblici 中的第一项被复制 4 次到 rectsToDraw 并且循环结束......
输入:{X=0,Y=0,Width=231,Height=66}{X=0,Y=0,Width=167,Height=61}{X=0,Y=0,Width=151,Height =47}{X=0,Y=0,Width=130,Height=40}{X=0,Y=0,Width=119,Height=39}{X=0,Y=0,Width=115,高度=52}{X=0,Y=0,宽度=72,高度=53}{X=0,Y=0,宽度=58,高度=46}{X=0,Y=0,宽度=47 ,Height=32}{X=0,Y=0,Width=41,Height=47}{X=0,Y=0,Width=38,Height=47}{X=0,Y=0,Width= 33,高度=45}{X=0,Y=0,宽度=22,高度=39}{X=0,Y=0,宽度=0,高度=0}
输出:{X=0,Y=0,Width=231,Height=66}{X=0,Y=0,Width=231,Height=66}{X=0,Y=0,Width=167,Height =61}{X=0,Y=0,Width=231,Height=66}{X=0,Y=0,Width=167,Height=61}{X=0,Y=0,Width=151,高度=47}{X=0,Y=0,宽度=231,高度=66}{X=0,Y=0,宽度=167,高度=61}{X=0,Y=0,宽度=151 ,Height=47}{X=0,Y=0,Width=130,Height=40}{X=0,Y=0,Width=231,Height=66}{X=0,Y=0,Width= 167,高度=61}{X=0,Y=0,宽度=151,高度=47}{X=0,Y=0,宽度=130,高度=40}{X=0,Y=0,宽度=119,高度=39}{X=0,Y=0,宽度=231,高度=66}{X=0,Y=0,宽度=167,高度=61}{X=0,Y=0,宽度=151,高度=47}{X=0,Y=0,宽度=130,高度=40}{X=0,Y=0,宽度=119,高度=39}{X=0,Y=0 ,Width=115,Height=52}{X=0,Y=0,Width=231,Height=66}{X=0,Y=0,Width=167,Height=61}{X=0,Y= 0,Width=151,Height=47}{X=0,Y=0,Width=130,Height=40}{X=0,Y=0,Width=119,Height=39}{X=0,Y =0,Width=115,Height=52}{X=0,Y=0,Width=72,Height=53}{X=0,Y=0,Width=231,Height=66}{X=0, Y=0,宽度=167,高度=61}{X=0,Y=0,宽度=151,高度=47}{X=0,Y=0,宽度=130,高度=40}{X=0,Y=0 ,Width=119,Height=39}{X=0,Y=0,Width=115,Height=52}{X=0,Y=0,Width=72,Height=53}{X=0,Y= 0,Width=58,Height=46}{X=0,Y=0,Width=231,Height=66}{X=0,Y=0,Width=167,Height=61}{X=0,Y =0,Width=151,Height=47}{X=0,Y=0,Width=130,Height=40}{X=0,Y=0,Width=119,Height=39}{X=0, Y=0,Width=115,Height=52}{X=0,Y=0,Width=72,Height=53}{X=0,Y=0,Width=58,Height=46}{X=0 ,Y=0,Width=47,Height=32}{X=0,Y=0,Width=231,Height=66}{X=0,Y=0,Width=167,Height=61}{X= 0,Y=0,宽度=151,高度=47}{X=0,Y=0,宽度=130,高度=40}{X=0,Y=0,宽度=119,高度=39}{X =0,Y=0,宽度=115,高度=52}{X=0,Y=0,宽度=72,高度=53}{X=0,Y=0,宽度=58,高度=46}{ X=0,Y=0,Width=47,Height=32}{X=0,Y=0,Width=41,Height=47}{X=0,Y=0,Width=231,Height=66} {X=0,Y=0,Width=167,Height=61}{X=0,Y=0,Width=151,Height=47}{X=0,Y=0,Width=130,Height=40 }{X=0,Y=0,Width=119,Height=39}{X=0,Y=0,Width=115,Height=52}{X=0,Y=0,Width=72,Height=53}{X=0 ,Y=0,Width=58,Height=46}{X=0,Y=0,Width=47,Height=32}{X=0,Y=0,Width=41,Height=47}{X= 0,Y=0,宽度=38,高度=47}{X=0,Y=0,宽度=231,高度=66}{X=0,Y=0,宽度=167,高度=61}{X =0,Y=0,宽度=151,高度=47}{X=0,Y=0,宽度=130,高度=40}{X=0,Y=0,宽度=119,高度=39}{ X=0,Y=0,Width=115,Height=52}{X=0,Y=0,Width=72,Height=53}{X=0,Y=0,Width=58,Height=46} {X=0,Y=0,Width=47,Height=32}{X=0,Y=0,Width=41,Height=47}{X=0,Y=0,Width=38,Height=47 }{X=0,Y=0,Width=33,Height=45}{X=0,Y=0,Width=231,Height=66}{X=0,Y=0,Width=167,Height= 61}{X=0,Y=0,Width=151,Height=47}{X=0,Y=0,Width=130,Height=40}{X=0,Y=0,Width=119,Height =39}{X=0,Y=0,宽度=115,高度=52}{X=0,Y=0,宽度=72,高度=53}{X=0,Y=0,宽度=58,高度=46}{X=0,Y=0,宽度=47,高度=32}{X=0,Y=0,宽度=41,高度=47}{X=0,Y=0,宽度=38 ,高度=47}{X=0,Y=0,宽度=33,高度=45}{X=0,Y=0,宽度=22,高度=39}{X=0,Y=0,宽度=231,高度=66}{X=0,Y=0 ,Width=167,Height=61}{X=0,Y=0,Width=151,Height=47}{X=0,Y=0,Width=130,Height=40}{X=0,Y= 0,Width=119,Height=39}{X=0,Y=0,Width=115,Height=52}{X=0,Y=0,Width=72,Height=53}{X=0,Y =0,Width=58,Height=46}{X=0,Y=0,Width=47,Height=32}{X=0,Y=0,Width=41,Height=47}{X=0, Y=0,Width=38,Height=47}{X=0,Y=0,Width=33,Height=45}{X=0,Y=0,Width=22,Height=39}{X=0 ,Y=0,宽度=0,高度=0}高度=39}{X=0,Y=0,宽度=0,高度=0}高度=39}{X=0,Y=0,宽度=0,高度=0}