Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sorting 将2D框排序为更大的框,以最有效、最完整的方式填充_Sorting_Lua_2d_Love2d - Fatal编程技术网

Sorting 将2D框排序为更大的框,以最有效、最完整的方式填充

Sorting 将2D框排序为更大的框,以最有效、最完整的方式填充,sorting,lua,2d,love2d,Sorting,Lua,2d,Love2d,背景: 您好,我已经在这个函数上工作了一段时间了,我有点结巴。最终,我试图实现的是一个应用程序,其中用户输入较大的框,同时也输入较小框的列表。(顺便说一句,都是二维的)。然后程序会处理这个过程,并尝试用较小的盒子尽可能完整地填充较大的盒子。对于那些好奇的人来说,这是一个剧院平台应用程序 代码素材: 我在这里有一个函数,它接受“box”,这是一个由用户创建的较大盒子组成的表,以及“platforms”,这是一个由较小盒子组成的表。正如在评论中所看到的,我已经写了一部分,如果一个平台完全适合一个盒子

背景:
您好,我已经在这个函数上工作了一段时间了,我有点结巴。最终,我试图实现的是一个应用程序,其中用户输入较大的框,同时也输入较小框的列表。(顺便说一句,都是二维的)。然后程序会处理这个过程,并尝试用较小的盒子尽可能完整地填充较大的盒子。对于那些好奇的人来说,这是一个剧院平台应用程序

代码素材:
我在这里有一个函数,它接受“box”,这是一个由用户创建的较大盒子组成的表,以及“platforms”,这是一个由较小盒子组成的表。正如在评论中所看到的,我已经写了一部分,如果一个平台完全适合一个盒子,那么它就会这样做。然后,它会减少该平台给定大小的数量,并将该框标记为已填充

问题:
我无法解决的问题是如何以最有效的方式通过编程将两个平台安装到一个更大的盒子中。我考虑过使用方框的x面,并用给定平台集的x值填充,然后用该集中相同的x值交互不同的平台,但y值不同,但有几个问题。
有没有关于我该去哪里的建议

userInterfaceCall()
    squares[#squares+1] = {x1=x, y1=y, x2 = nil, y2 = nil, f=false}
    --x1,y1 and x2,y2 are coordinates for two opposite points in a square
    --f is the boolean that is marked true when a square (box) is completely filled
end

platforms[1] = {x=4, y=4, q=2} --example user data
platforms[2] = {x=4, y=6, q=2} --x and y are platform dimensions
platforms[3] = {x=6, y=2, q=1} --q is quantity
platforms[4] = {x=8, y=4, q=1} 

process(squares,platforms) --this is called by a UI element

function process(box,platforms)
for i,box in ipairs(box) do                     --for every square do
    if box.f == false then                      --if the box already has a given platform, don't do shit
        for i,platform in ipairs(platforms) do  --for each platform for each box
            boxX = math.abs(box.x1-box.x2)/scale  --Ignore this, this is working with scaling from-
            boxY = math.abs(box.y1-box.y2)/scale  --pixel size to feet to compare to list of platforms
            if boxX == platform.x and boxY == platform.y and platform.q > 0 then        --Test if any platform fits directly in the box
                placements[#placements+1] = {x1 = box.x1, y1 = box.y1, x2 = box.x2, y2 = box.y2, s = ''} --Creates a new placement of a given platform, which is then drawn
                platform.q = platform.q - 1 --we reduce the quantity, cause we used one
                box.f = true --yes, we just filled the box completely
            elseif boxY == platform.x and boxX == platform.y and platform.q > 0 then    --Test for switched x and y
                placements[#placements+1] = {x1 = box.x1, y1 = box.y1, x2 = box.x2, y2 = box.y2, s = ''}
                platform.q = platform.q - 1
                box.f = true
            elseif
                --put multiple platforms in one box, Help
            else
                setPrompt('Could not find for box: '..boxX..','..boxY)
            end
        end
    end
end

结束

您发现了的一个变体,已知为NP完全。因此,不存在简单的“最佳”解决方案,然而,已经发现许多策略可以在可接受的时间内产生可接受的结果。请参阅链接文章以获得进一步解释。

其他人如需了解此内容,请阅读此处:


这是一个有效的javascript/CSS解决方案。

谢谢!我想我只是在尝试查找时没有使用正确的搜索词。