placeTile method
Attempts to place a tile of given dimensions in the height map, returning updated heights or null.
Finds the lowest uniform base across the tile's width, validating fit within quilt height. Used in quilt building to minimize gaps, integrating with variance scoring for optimal arrangements.
Implementation
List<int>? placeTile(List<int> heights, int tileW, int tileH, int targetH) {
if (tileW > style.carpetWidth) return null;
int bestI = -1;
int minBase = 999999;
for (int i = 0; i <= style.carpetWidth - tileW; i++) {
int maxInRange = heights[i];
for (int j = i + 1; j < i + tileW; j++) {
maxInRange = max(maxInRange, heights[j]);
}
if (maxInRange < minBase || (maxInRange == minBase && i < bestI)) {
minBase = maxInRange;
bestI = i;
}
}
if (bestI == -1) return null;
if (minBase + tileH > targetH) return null;
int minInRange = heights[bestI];
for (int j = bestI + 1; j < bestI + tileW; j++) {
minInRange = min(minInRange, heights[j]);
}
if (minInRange != minBase) return null;
List<int> newHeights = List.from(heights);
for (int j = bestI; j < bestI + tileW; j++) {
newHeights[j] = minBase + tileH;
}
return newHeights;
}