Sha256: 20b97bd9920d5f489652ca9b8d74ace14533569e01815def0c7ea105a1773a4c

Contents?: true

Size: 1.06 KB

Versions: 3

Compression:

Stored size: 1.06 KB

Contents

# rectangle.rb

module Ruby2D
  class Rectangle < Quad

    attr_reader :x, :y, :width, :height

    def initialize(opts = {})
      @x      = opts[:x]      || 0
      @y      = opts[:y]      || 0
      @z      = opts[:z]      || 0
      @width  = opts[:width]  || 200
      @height = opts[:height] || 100

      update_coords(@x, @y, @width, @height)

      self.color = opts[:color] || 'white'
      add
    end

    def x=(x)
      @x = @x1 = x
      @x2 = x + @width
      @x3 = x + @width
      @x4 = x
    end

    def y=(y)
      @y = @y1 = y
      @y2 = y
      @y3 = y + @height
      @y4 = y + @height
    end

    def width=(w)
      @width = w
      update_coords(@x, @y, w, @height)
    end

    def height=(h)
      @height = h
      update_coords(@x, @y, @width, h)
    end

    def contains?(x, y)
      @x < x and @x + @width > x and @y < y and @y + @height > y
    end

    private

    def update_coords(x, y, w, h)
      @x1 = x
      @y1 = y
      @x2 = x + w
      @y2 = y
      @x4 = x
      @y4 = y + h
      @x3 = x + w
      @y3 = y + h
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ruby2d-0.5.1 lib/ruby2d/rectangle.rb
ruby2d-0.5.0 lib/ruby2d/rectangle.rb
ruby2d-0.4.2 lib/ruby2d/rectangle.rb