Sha256: 15bd4734acaad35193b289710f14233ef256bbccfa388db3119663885509388f

Contents?: true

Size: 1.34 KB

Versions: 2

Compression:

Stored size: 1.34 KB

Contents

module Vedeu

  # Converts an index into a position for the terminal.
  #
  # When the optional offset `oy` and `ox` params are given, the they are used
  # for the starting position.
  #
  class IndexPosition

    # @param iy [Fixnum]
    # @param ix [Fixnum]
    # @param oy [Fixnum]
    # @param ox [Fixnum]
    # @return [Array]
    def self.[](iy, ix, oy = 1, ox = 1)
      new(iy, ix, oy, ox).[]
    end

    # @param iy [Fixnum]
    # @param ix [Fixnum]
    # @param oy [Fixnum]
    # @param ox [Fixnum]
    # @return [Vedeu::IndexPosition]
    def initialize(iy, ix, oy = 1, ox = 1)
      @iy = iy
      @ix = ix
      @oy = oy
      @ox = ox
    end

    # @return [Array]
    def []
      [y, x]
    end

    # @return [Vedeu::Position]
    def to_position
      Vedeu::Position.new(y, x)
    end

    # @return [Fixnum]
    def y
      (iy <= 0) ? oy : (iy + oy)
    end
    alias_method :first, :y

    # @return [Fixnum]
    def x
      (ix <= 0) ? ox : (ix + ox)
    end
    alias_method :last, :x

    private

    # @return [Fixnum]
    def iy
      @_iy ||= (@iy <= 0) ? 0 : @iy
    end

    # @return [Fixnum]
    def ix
      @_ix ||= (@ix <= 0) ? 0 : @ix
    end

    # @return [Fixnum]
    def oy
      @_oy ||= (@oy <= 1) ? 1 : @oy
    end

    # @return [Fixnum]
    def ox
      @_ox ||= (@ox <= 1) ? 1 : @ox
    end

  end # IndexPosition

end # Vedeu

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
vedeu-0.4.7 lib/vedeu/geometry/index_position.rb
vedeu-0.4.6 lib/vedeu/geometry/index_position.rb