Class: Ruby2D::Rectangle

Inherits:
Quad
  • Object
show all
Defined in:
lib/ruby2d/rectangle.rb

Overview

A rectangle

Direct Known Subclasses

Square

Instance Attribute Summary

Attributes inherited from Quad

#x1, #x2, #x3, #x4, #y1, #y2, #y3, #y4

Attributes included from Renderable

#color, #height, #width, #x, #y, #z

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Quad

#color=

Methods included from Renderable

#add, #remove

Constructor Details

#initialize(x: 0, y: 0, width: 200, height: 100, z: 0, color: nil, colour: nil, opacity: nil) ⇒ Rectangle

Create an rectangle

Parameters:

  • x (Numeric) (defaults to: 0)
  • y (Numeric) (defaults to: 0)
  • width (Numeric) (defaults to: 200)
  • height (Numeric) (defaults to: 100)
  • z (Numeric) (defaults to: 0)
  • color (String, Array) (defaults to: nil)

    A single colour or an array of exactly 4 colours

  • opacity (Numeric) (defaults to: nil)

    Opacity of the image when rendering

Raises:

  • (ArgumentError)

    if an array of colours does not have 4 entries



17
18
19
20
21
22
23
24
# File 'lib/ruby2d/rectangle.rb', line 17

def initialize(x: 0, y: 0, width: 200, height: 100, z: 0, color: nil, colour: nil, opacity: nil)
  @width = width
  @height = height
  super(x1: @x = x, y1: @y = y,
        x2: x + width, y2: y,
        x3: x + width, y3: y + height,
        x4: x, y4: y + height, z: z, color: color, colour: colour, opacity: opacity)
end

Class Method Details

.draw(x:, y:, width:, height:, color:) ⇒ Object



52
53
54
55
56
57
# File 'lib/ruby2d/rectangle.rb', line 52

def self.draw(x:, y:, width:, height:, color:)
  super(x1: x, y1: y,
        x2: x + width, y2: y,
        x3: x + width, y3: y + height,
        x4: x, y4: y + height, color: color)
end

Instance Method Details

#contains?(x, y) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
# File 'lib/ruby2d/rectangle.rb', line 59

def contains?(x, y)
  # Override the check from Quad since this is faster
  x >= @x && x <= (@x + @width) && y >= @y && y <= (@y + @height)
end

#height=(height) ⇒ Object



46
47
48
49
50
# File 'lib/ruby2d/rectangle.rb', line 46

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

#width=(width) ⇒ Object



40
41
42
43
44
# File 'lib/ruby2d/rectangle.rb', line 40

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

#x=(x) ⇒ Object



26
27
28
29
30
31
# File 'lib/ruby2d/rectangle.rb', line 26

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

#y=(y) ⇒ Object



33
34
35
36
37
38
# File 'lib/ruby2d/rectangle.rb', line 33

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