I haven't fully read your code other than to get a general sense of
what it is you're trying to do.  And since you double or halve the
spacing until it meets certain constraints, it looks to me like you're
ultimately trying to solve a problem like this:

    2 ** x == y

Where you know y but do not know x.  Such a problem can be solved with
logarthims.

    x * log(2) == log(y)

    x == log(y) / log(2)

Once you derive x, you can then do a ceiling, floor, round, etc.
depending on what your preference is.

So the puzzle for you is can you turn the problem you're trying to
solve into the form:

    2 ** x == y ?

For example, maybe y would be some formula involving the base grid and
40, which seems to be a key number to you?

Good luck,

Eric

----------------
Are you interested in on-site Ruby training that uses well-designed,
real-world, hands-on exercises?  http://LearnRuby.com