Class: Rubydraw::Rectangle

Inherits:
Object show all
Defined in:
lib/rubydraw/rectangle.rb

Overview

A Rectangle is defined by the position, width, and height, and knows if a point falls inside it. One good use for this class would be a button (will be implemented in a future version, probably soon)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(position, dimensions) ⇒ Rectangle

Create a new rectangle with the given dimensions and position.



14
15
16
# File 'lib/rubydraw/rectangle.rb', line 14

def initialize(position, dimensions)
  @position, @dimensions = position, dimensions
end

Instance Attribute Details

#dimensionsObject (readonly)

Returns the value of attribute dimensions.



6
7
8
# File 'lib/rubydraw/rectangle.rb', line 6

def dimensions
  @dimensions
end

#positionObject (readonly) Also known as: top_left

Returns the value of attribute position.



6
7
8
# File 'lib/rubydraw/rectangle.rb', line 6

def position
  @position
end

Class Method Details

.[](position, dimensions) ⇒ Object

Shorthand new method



9
10
11
# File 'lib/rubydraw/rectangle.rb', line 9

def self.[](position, dimensions)
  self.new(position, dimensions)
end

Instance Method Details

#bottom_leftObject

Returns a point at the bottom left corner



32
33
34
# File 'lib/rubydraw/rectangle.rb', line 32

def bottom_left
  Point[x, y + height]
end

#bottom_rightObject

Returns the position at the bottom right



42
43
44
# File 'lib/rubydraw/rectangle.rb', line 42

def bottom_right
  @position + @dimensions
end

#contains?(point) ⇒ Boolean

Returns if the given point is inside this rectangle. See Rubydraw::Point#inside?

Returns:

  • (Boolean)


47
48
49
# File 'lib/rubydraw/rectangle.rb', line 47

def contains?(point)
  point.inside?(self)
end

#heightObject

Returns this rectangle’s height.



62
63
64
# File 'lib/rubydraw/rectangle.rb', line 62

def height
  @dimensions.y
end

#pointsObject

Returns all four corners in this Rectangle.



67
68
69
# File 'lib/rubydraw/rectangle.rb', line 67

def points
  [top_left, top_right, bottom_left, bottom_right]
end

#to_sObject

Returns a human-readable string, specifying this rectangle.



72
73
74
75
# File 'lib/rubydraw/rectangle.rb', line 72

def to_s
  d = @dimensions
  ":Rectangle: (position: #{@position.to_s}) (dimensions: #{d.x}x#{d.y}):"
end

#to_sdlObject

Returns an SDL::Rectangle equal to this one.



52
53
54
# File 'lib/rubydraw/rectangle.rb', line 52

def to_sdl
  SDL::Rect.new([x, y, width, height])
end

#top_rightObject

Returns the point at the top right corner



37
38
39
# File 'lib/rubydraw/rectangle.rb', line 37

def top_right
  Point[x + width, y]
end

#widthObject

Returns this rectangle’s width.



57
58
59
# File 'lib/rubydraw/rectangle.rb', line 57

def width
  @dimensions.x
end

#xObject

Returns the x position for the rectangle



19
20
21
# File 'lib/rubydraw/rectangle.rb', line 19

def x
  @position.x
end

#yObject

Returns this rectangle’s y position



24
25
26
# File 'lib/rubydraw/rectangle.rb', line 24

def y
  @position.y
end