From: Jacob Fugal Date: 2005-10-28T04:53:50+09:00 Subject: Re: ASCII Isometric Graphics... On 10/27/05, CBlair1986 wrote: > Hello, all. > > I've just got a bit of a question, here. I suppose my mind's a bit too > tired to think of a solution at the moment, but it's been on my mind > for a while. What I'd like to do is take an array and turn it into an > isometric view of the data, eg: > > [[[1,1], # Bottom Layer > [1,1]], > [[0,1], # Top Layer > [0,0]]] > > to > +-----+ > |\ \ > | +-----+ > +-----+ | | > |\ \| | > | +-----+-----+ > + |\ \ \ > \| +-----+-----+ > + | | | > \| | | > +-----+-----+ > > The array would hold only binary data, either 1/0 or true/false. Rather than accomadating 1/0 or true/false, I just did 1/0. Handling both is harder than you think, given that 0 is true. # -------------------- class Canvas def draw( data, x=0, y=0 ) layers = data.size rows = data.map{ |layer| layer.size }.max columns = data.map{ |layer| layer.map{ |row| row.size }.max }.max @canvas = [] (layers * 3 + rows * 2 + 1).times do |i| @canvas[i] = ([' '] * (columns * 6 * rows * 2 + 1)).join end @x, @y = x, y @y += data.size * 3 data.each do |layer| @y -= 3 draw_layer( layer ) end puts @canvas.join("\n") end def draw_layer( layer ) layer.each do |row| draw_row( row ) @x += 2 @y += 2 end @x -= layer.size * 2 @y -= layer.size * 2 end def draw_row( row ) @x += row.size * 6 row.reverse.each do |datum| @x -= 6 draw_box unless datum.zero? end end def draw_box @canvas[@y + 0][(@x + 0)..(@x + 6)] = '+-----+' @canvas[@y + 1][(@x + 0)..(@x + 7)] = '|\\ \\' @canvas[@y + 2][(@x + 0)..(@x + 8)] = '| +-----+' @canvas[@y + 3][(@x + 0)..(@x + 8)] = '+ | |' @canvas[@y + 4][(@x + 1)..(@x + 8)] = '\\| |' @canvas[@y + 5][(@x + 2)..(@x + 8)] = '+-----+' end end data = [[[ 1, 1 ], [ 1, 1 ]], [[ 0, 1 ], [ 0, 0 ]]] Canvas.new.draw( data )