Sha256: a6346fa67bb09b9d34e04d36f21a84b4083184e417796eb69c7af7d727d4d37a

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 KB

Contents

module Straightedge
  module Figures
    class Grid < Figure
      include Enumerable
      def_delegators :to_a, :sample, :map!
      def_delegator :dimensions, :x, :width
      def_delegator :dimensions, :y, :height

      attr_reader :dimensions, :scale, :figure

      def initialize(dimensions=[1,1], opts={})
	@dimensions = dimensions
	@scale      = opts.delete(:scale)  { 1.0 }
	super(to_a, opts)
      end

      def at(xy)
	[xy.x, xy.y]
      end

      def each
	Grid.each_coordinate([width, height]) do |x, y|
	  yield(at([x,y]))
	end
      end

      def cell_at(xy)
	@cells     ||= {}
	@cells[xy] ||= Figures::Quadrilateral.new(dimensions: [@scale, @scale], location: to_pixels(xy)) #[xy.x+1,xy.y+1])) #to_pixels(xy))
      end

      def each_cell
	each { |xy| yield cell_at(xy) }
      end

      def paint!
        #each(&:to_point)
	each_cell(&:paint)
      end

      def clip(xys=[])
	xys.reject do |xy|
	  _x,_y = *xy
	  _x < 0 || _y < 0 || x >= _x || y >= _y
	end 
      end

      def self.each_coordinate(dimensions)
	dimensions.x.times do |x|
	  dimensions.y.times do |y|
	    yield [x,y]
	  end
	end
      end

      def to_pixels(xy)
	[xy.x * (@scale/2), xy.y * (@scale/2)]
      end

      #def to_coords(xy)
      #  [xy.x / (@scale/2), xy.y / (@scale/2) ]
      #end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
straightedge-0.1.1 lib/straightedge/figures/grid.rb