Convert tiles to rectangles
Posted: Fri Jan 06, 2023 1:41 pm
Hi all!
The problem comes from another topic: viewtopic.php?f=4&t=94150
We have a map of tiles, for example:
We are need to convert it to the smallest amount of rectangles, for example:
How to do that?
I have a code to convert the map to the list of horizontal/vertical rectangles, but how to optimize the amount of rectangles?
The problem comes from another topic: viewtopic.php?f=4&t=94150
We have a map of tiles, for example:
Code: Select all
map = {
{1,1,1,1,1,1,1,1,1,1},
{1,0,0,1,0,1,0,0,0,1},
{1,1,0,1,0,0,0,1,0,1},
{1,0,0,0,0,1,1,1,0,1},
{1,0,1,1,0,1,0,1,0,1},
{1,0,0,1,0,1,0,1,0,1},
{1,0,1,1,0,1,0,1,0,1},
{1,0,0,1,0,1,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1},
}
I have a code to convert the map to the list of horizontal/vertical rectangles, but how to optimize the amount of rectangles?
Code: Select all
map = {
{1,1,1,0,1,0,1,0,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,0,1,0,1,0,1,1,1,1,1,1,1,1,1,1,1,1, },
{1,0,0,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0, 1,0,0,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0, },
{1,0,1,1,1,1,1,1,1,1,1,0,1,0,1,0,1,1,1,0, 1,0,1,1,1,1,1,1,1,1,1,0,1,0,1,0,1,1,1,0, },
{0,0,1,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,1,0, 0,0,1,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,1,0, },
{1,1,1,0,1,0,1,1,1,1,1,1,1,0,1,1,1,0,1,1, 1,1,1,0,1,0,1,1,1,1,1,1,1,0,1,1,1,0,1,1, },
{0,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0,0,0, 0,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0,0,0, },
{1,1,1,1,1,0,1,0,1,0,1,0,1,1,1,1,1,1,1,1, 1,1,1,1,1,0,1,0,1,0,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,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0, 0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0, },
{1,0,1,0,1,0,1,0,1,0,1,1,1,0,1,0,1,1,1,1, 1,0,1,0,1,0,1,0,1,0,1,1,1,0,1,0,1,1,1,1, },
{1,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0, 1,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0, },
{1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1, },
{0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0, },
{1,1,1,0,1,1,1,0,1,0,1,1,1,0,1,1,1,0,1,1, 1,1,1,0,1,1,1,0,1,0,1,1,1,0,1,1,1,0,1,1, },
{0,0,1,0,1,0,1,0,1,0,1,0,0,0,1,0,1,0,1,0, 0,0,1,0,1,0,1,0,1,0,1,0,0,0,1,0,1,0,1,0, },
{1,1,1,0,1,0,1,0,1,0,1,1,1,0,1,0,1,0,1,0, 1,1,1,0,1,0,1,0,1,0,1,1,1,0,1,0,1,0,1,0, },
{1,0,0,0,1,0,1,0,1,0,0,0,1,0,0,0,1,0,1,0, 1,0,0,0,1,0,1,0,1,0,0,0,1,0,0,0,1,0,1,0, },
}
Code: Select all
rectangles = {}
local firstX, lastX = 1, #map[1]
local firstY, lastY = 1, #map
for y = firstY, lastY do
-- local r = {x=firstX,y=y,w=1,h=1}
local r
for x = firstX, lastX do
if map[y][x] == 1 and (x == lastX) then -- lastwall
if r then
r.w=r.w+1
else
r = {x=x,y=y,w=1,h=1}
end
table.insert (rectangles, r)
elseif map[y][x] == 1 then -- wall
if r then
r.w=r.w+1
else
r = {x=x,y=y,w=1,h=1}
end
elseif r then -- not wall, save existing rectangle
table.insert (rectangles, r)
r = nil
end
end
end
print('#rectangles', #rectangles)
rectangles2 = {}
for x = firstX, lastX do
local r
for y = firstY, lastY do
if map[y][x] == 1 and (y == lastY) then -- lastwall
if r then
r.h=r.h+1
else
r = {x=x,y=y,w=1,h=1}
end
table.insert (rectangles2, r)
elseif map[y][x] == 1 then -- wall
if r then
r.h=r.h+1
else
r = {x=x,y=y,w=1,h=1}
end
elseif r then -- not wall, save existing rectangle
table.insert (rectangles2, r)
r = nil
end
end
end
print('#rectangles2', #rectangles2)
function love.draw()
love.graphics.scale(16)
love.graphics.setLineWidth(1/8)
for i, r in ipairs (rectangles2) do
love.graphics.rectangle('line', r.x-1, r.y-1, r.w, r.h)
end
end