Class: Rubydraw::Rectangle
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
-
#dimensions ⇒ Object
readonly
Returns the value of attribute dimensions.
-
#position ⇒ Object
(also: #top_left)
readonly
Returns the value of attribute position.
Class Method Summary collapse
-
.[](position, dimensions) ⇒ Object
Shorthand new method.
Instance Method Summary collapse
-
#bottom_left ⇒ Object
Returns a point at the bottom left corner.
-
#bottom_right ⇒ Object
Returns the position at the bottom right.
-
#contains?(point) ⇒ Boolean
Returns if the given point is inside this rectangle.
-
#height ⇒ Object
Returns this rectangle’s height.
-
#initialize(position, dimensions) ⇒ Rectangle
constructor
Create a new rectangle with the given dimensions and position.
-
#points ⇒ Object
Returns all four corners in this Rectangle.
-
#to_s ⇒ Object
Returns a human-readable string, specifying this rectangle.
-
#to_sdl ⇒ Object
Returns an SDL::Rectangle equal to this one.
-
#top_right ⇒ Object
Returns the point at the top right corner.
-
#width ⇒ Object
Returns this rectangle’s width.
-
#x ⇒ Object
Returns the x position for the rectangle.
-
#y ⇒ Object
Returns this rectangle’s y position.
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
#dimensions ⇒ Object (readonly)
Returns the value of attribute dimensions.
6 7 8 |
# File 'lib/rubydraw/rectangle.rb', line 6 def dimensions @dimensions end |
#position ⇒ Object (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_left ⇒ Object
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_right ⇒ Object
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?
47 48 49 |
# File 'lib/rubydraw/rectangle.rb', line 47 def contains?(point) point.inside?(self) end |
#height ⇒ Object
Returns this rectangle’s height.
62 63 64 |
# File 'lib/rubydraw/rectangle.rb', line 62 def height @dimensions.y end |
#points ⇒ Object
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_s ⇒ Object
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_sdl ⇒ Object
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_right ⇒ Object
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 |
#width ⇒ Object
Returns this rectangle’s width.
57 58 59 |
# File 'lib/rubydraw/rectangle.rb', line 57 def width @dimensions.x end |
#x ⇒ Object
Returns the x position for the rectangle
19 20 21 |
# File 'lib/rubydraw/rectangle.rb', line 19 def x @position.x end |
#y ⇒ Object
Returns this rectangle’s y position
24 25 26 |
# File 'lib/rubydraw/rectangle.rb', line 24 def y @position.y end |