Sha256: 11859a39bb094fe756620927794d9ad16576758a0684cabf5d5c16f06012a2be

Contents?: true

Size: 1.03 KB

Versions: 3

Compression:

Stored size: 1.03 KB

Contents

# Ruby2D::Rectangle

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
      self.color = opts[:color] || 'white'
      update_coords(@x, @y, @width, @height)
      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.7.0 lib/ruby2d/rectangle.rb
ruby2d-0.6.1 lib/ruby2d/rectangle.rb
ruby2d-0.6.0 lib/ruby2d/rectangle.rb