1

我正在尝试为 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,H​​eight =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,H​​eight=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,H​​eight=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,H​​eight=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,H​​eight=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,H​​eight=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}

4

2 回答 2

2

可能还有其他问题,但我认为这段代码:

for (int j = 0; j < rectsToDraw.Count; j++)
{
    Debug.Write(rectsToDraw[0]);
}

应该:

for (int j = 0; j < rectsToDraw.Count; j++)
{
    Debug.Write(rectsToDraw[j]);
}

(更改Debug.Write(rectsToDraw[0]);Debug.Write(rectsToDraw[j]);)。

于 2012-09-12T03:22:03.507 回答
0

没关系,我设法修复它......代码如下:

private void FNF()
    {
        int XMX = 0;
        int moveX= 0;
        int moveY = 0;
        int MH = RectBin.Height;
        int MW = RectBin.Width;
        int c1 = Items.Count;
        rectsToDraw = new List<Rectangle>();

        for(int i = 0; i < c1; i++)
        {
            int height = Oblici[i].Height;
            int width = Oblici[i].Width;
            if(((moveY + height) <= MV)&&((moveX+width)<=MW))
            {
                rectsToDraw.Add(new Rectangle(moveX, moveY, width, height));
                moveY = moveY + height;
                if (XMX < moveX + width)
                {
                    XMX = moveX + width;
                }
            }
            else if((XMX + width) <= MW)
            {
                moveX = XMX;
                rectsToDraw.Add(new Rectangle(moveX, 0, width, height));
                moveY = height;
                XMX = moveX + width;
            }
            else
            {
                Debug.Write("Doesn't fit.");
                break;
            }
        }
        for (int j = 0; j < rectsToDraw.Count; j++)
        {
            Debug.Write(rectsToDraw[j]);
        }
    }

又写了一遍,改了一些名字(无缘无故)....还是谢谢....

于 2012-09-12T17:34:44.407 回答