• Status: Solved
• Priority: Medium
• Security: Public
• Views: 295

# Non uniform 2d grid subdivision based on gradient map

Given a black and white gradient map where pure black is 0.0 and pure white is 1.0. I'd like to generate coordinates for a 2d grid that has squares of varying sizes such that areas on the map that are whiter generate smaller squares and areas that are blacker generate squares of larger size. These squares will vary between min_size and max_size depending on the color of the map.

Perhaps this diagram could help explain what I'm trying to achieve:

nonuniform-subdivision.jpg
0
hbiz
• 2
1 Solution

Commented:
Instead of a max_size and a min_size, use a max_size and a num_sizes. This will avoid potential pitfalls if max_size is not min_size times a power of 2. Each successive size will be exactly half the previous one. (So max_size should still be a power of two)

Draw a grid of max_size over the whole image. Then iterate through each grid square. Take the average (or max, your choice) color of the square. If it is above the threshold (current_size_index/num_sizes) then draw the horizontal and vertical lines through the middle, then recursively do the same process on each subsquare (incrementing the current_size_index).
The whole thing will just be one simple recursive function.
0

Commented:
So the functions would look something like this:
``````DrawFunnyGrid()
{
size_index = 1
For i = 0 while i < IMAGE_HEIGHT step max_size
{
For j = 0 while j < IMAGE_WIDTH step max_size
{
DrawLine(i, j, i, j+max_size)
DrawLine(i, j, i + max_size, j)
ProcessSquare(i, j, max_size, size_index)
}
}
}

ProcessSquare(top, left, size, size_index)
{
if( MaxColor(Rectangle(top, left, size, size)) >= size_index/num_sizes)
{
DrawLine( (i+size)/2, j, (i+size)/2, j + size)
DrawLine( i, (j+size)/2, i+size, (j + size)/2)
ProcessSquare(i, j, size/2, size_index + 1)
ProcessSquare(i, (j+size)/2, size/2, size_index + 1)
ProcessSquare((i+size)/2, j, size/2, size_index + 1)
ProcessSquare((i+size)/2, (j+size)/2, size/2, size_index + 1)
}
}
``````
0

Author Commented:
Thanks! this is brilliant!
0

## Featured Post

• 2
Tackle projects and never again get stuck behind a technical roadblock.