I am pretty sure there might be something not so 100% correct with the data. I am assuming the adjacent tiles fit correctly this means colors are the same on adjacent borders.
Now in my previous algo, it guarantees uniqueness, let's say I am reading all the pixels and constructing the border columns/rows for each image [or tile] in a concatinated string:
for(var i:int = 0; i < 52; ++i)
{
for(var j:int = 0; j < 52; ++j)
{
var r:uint = ba.readUnsignedByte();
var g:uint = ba.readUnsignedByte();
var b:uint = ba.readUnsignedByte();
var color:uint = (r<<16 | g<<8 | b);
bmpData.setPixel(j,i,color);
if(i == 0)
topRow += color;
if(i==1)
topRowMinusOne += color;
if(j==0)
leftColumn += color;
if(j==1)
leftColumnMinusOne += color;
if(i==51)
bottomRow += color;
if(j==51)
rightColumn += color;
}
}
I can just push a tile to an array, and save the index of the array in 4 hashmaps.
allBitmaps.push({
bitmap:bmp,
topRow:topRow,
bottomRow:bottomRow,
leftColumn:leftColumn,
rightColumn:rightColumn
});
leftMap[leftColumn] =allBitmaps.length-1;
rightMap[rightColumn] = allBitmaps.length-1;
topMap[topRow] = allBitmaps.length-1;
topMap[topRow] = allBitmaps.length-1;
bottomMap[bottomRow] = allBitmaps.length-1;
Now by
definition of the problem, every value in one of the above hashmaps is unique, you cannot have the same line of colors for any two images on the same side, else it would means that there is a possibility that 1 image can have 2 adjacent images to the same side.
From my debugging it is actually showing that there are some images that have the same borders [on any side] that is actually shared!
But for the purpose of the demo I am continuing.
We can know the 1st top left tile by actually comparing:
if(topRow == topRowMinusOn && leftColumn == leftColumnMinusOne)
{
startingIndex = allBitmaps.length-1;
bmp.x = -1;
bmp.y = -1;
}
At the end, when all the bittmaps are loaded we can just run the algorithm
if(allBitmaps.length == array.length && startingIndex != -1)
runAlgorithm(startingIndex);
and the actual small recursive algorithm will just check from the starting top/left tile if it has a border, from the hashmaps, it will recurse ie:
function runAlgorithm(index:int):void
{
var currentBitmap:Object = allBitmaps[index];
if(currentBitmap.visited) return;
currentBitmap.visited = true;
var left:int = rightMap[currentBitmap.leftColumn];
var right:int = leftMap[currentBitmap.rightColumn];
var top:int = bottomMap[currentBitmap.topRow];
var bottom:int = topMap[currentBitmap.bottomRow];
if(left){
allBitmaps[left].bitmap.x = currentBitmap.bitmap.x - 50;
runAlgorithm(left);
}
if(right){
allBitmaps[right].bitmap.x = currentBitmap.bitmap.x + 50;
runAlgorithm(right);
}
if(top){
allBitmaps[top].bitmap.y = currentBitmap.bitmap.y - 50;
runAlgorithm(top);
}
if(bottom){
allBitmaps[bottom].bitmap.y = currentBitmap.bitmap.y + 50;
runAlgorithm(bottom);
}
}