Non uniform 2d grid subdivision based on gradient map

Posted on 2011-10-11
Last Modified: 2012-05-12
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:

Question by:hbiz
    LVL 37

    Expert Comment

    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.
    LVL 37

    Accepted Solution

    So the functions would look something like this:
      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)

    Open in new window


    Author Closing Comment

    Thanks! this is brilliant!

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now