Sha256: dbfc31e9be57356bd973b22d4f949f17c976afd2a76c2f8266484379538e7635

Contents?: true

Size: 835 Bytes

Versions: 3

Compression:

Stored size: 835 Bytes

Contents

module Geometer
  class Dimensions < Struct.new(:width, :height)
    include Geometer::PointHelpers

    def inspect
      "#{width}x#{height}"
    end

    def contains?(position)
      x,y=*position
      x_range.include?(x) && y_range.include?(y)
    end

    def sample
      x,y = x_range.to_a.sample, y_range.to_a.sample
      coord(x,y)
    end

    def area
      x_range.size * y_range.size
    end

    def x_range
      0..(width-1)
    end

    def y_range
      0..(height-1)
    end

    def all_locations
      @all_locations ||= assemble_all_locations
    end

    def assemble_all_locations
      locs = []
      x_range.each do |x|
        y_range.each do |y|
          locs.push coord(x,y)
        end
      end
      locs
    end
  end

  module DimensionHelpers
    def dim(w,h); Dimensions.new(w,h) end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
geometer-0.1.3 lib/geometer/dimensions.rb
geometer-0.1.2 lib/geometer/dimensions.rb
geometer-0.1.1 lib/geometer/dimensions.rb