static void positionSprite( List<ArrangedSprite> sprites, int index, int outputWidth )
{
int x = 0;
int y = 0;
while( true )
{
// Is this position free for us to use?
var intersects = findIntersectingSprite( sprites, index, x, y );
if( intersects < 0 )
{
sprites[index].x = x;
sprites[index].y = y;
return;
}
// Skip past the existing sprite that we collided with.
x = sprites[intersects].x + sprites[intersects].width;
// If we ran out of room to move to the right,
// try the next line down instead.
if( x + sprites[index].width > outputWidth )
{
x = 0;
y++;
}
}
}