Class: Tuile::Rect
- Inherits:
-
Object
- Object
- Tuile::Rect
- Defined in:
- lib/tuile/rect.rb
Overview
A rectangle, with integer ‘left`, `top`, `width` and `height`, all 0-based.
Instance Attribute Summary collapse
-
#height ⇒ Integer
readonly
Height.
-
#left ⇒ Integer
readonly
Left edge, 0-based.
-
#top ⇒ Integer
readonly
Top edge, 0-based.
-
#width ⇒ Integer
readonly
Width.
Instance Method Summary collapse
-
#at(point) ⇒ Rect
Positioned at the new ‘left`/`top`.
- #centered(screen_size) ⇒ Rect
-
#clamp(max_size) ⇒ Rect
Clamp both width and height and return a rectangle.
- #contains?(point) ⇒ Boolean
- #empty? ⇒ Boolean
- #size ⇒ Size
- #to_s ⇒ String
- #top_left ⇒ Point
Instance Attribute Details
#height ⇒ Integer (readonly)
Returns height.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/tuile/rect.rb', line 14 class Rect < Data.define(:left, :top, :width, :height) # @return [String] def to_s = "#{top_left} #{size}" # @return [Boolean] true if either {#width} or {#height} is zero or negative. def empty? width <= 0 || height <= 0 end # @param point [Point] new top-left corner. # @return [Rect] positioned at the new `left`/`top`. def at(point) Rect.new(point.x, point.y, width, height) end # Centers the rectangle — keeps {#width} and {#height} but modifies # {#top} and {#left} so that the rectangle is centered on a screen. # @param screen_size [Size] screen size # @return [Rect] moved rectangle. def centered(screen_size) at(Point.new((screen_size.width - width) / 2, (screen_size.height - height) / 2)) end # Clamp both width and height and return a rectangle. # @param max_size [Size] the max size # @return [Rect] def clamp(max_size) new_width = width.clamp(nil, max_size.width) new_height = height.clamp(nil, max_size.height) new_width == width && new_height == height ? self : Rect.new(left, top, new_width, new_height) end # @param point [Point] # @return [Boolean] def contains?(point) point.x >= left && point.x < left + width && point.y >= top && point.y < top + height end # @return [Size] def size = Size.new(width, height) # @return [Point] def top_left = Point.new(left, top) end |
#left ⇒ Integer (readonly)
Returns left edge, 0-based.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/tuile/rect.rb', line 14 class Rect < Data.define(:left, :top, :width, :height) # @return [String] def to_s = "#{top_left} #{size}" # @return [Boolean] true if either {#width} or {#height} is zero or negative. def empty? width <= 0 || height <= 0 end # @param point [Point] new top-left corner. # @return [Rect] positioned at the new `left`/`top`. def at(point) Rect.new(point.x, point.y, width, height) end # Centers the rectangle — keeps {#width} and {#height} but modifies # {#top} and {#left} so that the rectangle is centered on a screen. # @param screen_size [Size] screen size # @return [Rect] moved rectangle. def centered(screen_size) at(Point.new((screen_size.width - width) / 2, (screen_size.height - height) / 2)) end # Clamp both width and height and return a rectangle. # @param max_size [Size] the max size # @return [Rect] def clamp(max_size) new_width = width.clamp(nil, max_size.width) new_height = height.clamp(nil, max_size.height) new_width == width && new_height == height ? self : Rect.new(left, top, new_width, new_height) end # @param point [Point] # @return [Boolean] def contains?(point) point.x >= left && point.x < left + width && point.y >= top && point.y < top + height end # @return [Size] def size = Size.new(width, height) # @return [Point] def top_left = Point.new(left, top) end |
#top ⇒ Integer (readonly)
Returns top edge, 0-based.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/tuile/rect.rb', line 14 class Rect < Data.define(:left, :top, :width, :height) # @return [String] def to_s = "#{top_left} #{size}" # @return [Boolean] true if either {#width} or {#height} is zero or negative. def empty? width <= 0 || height <= 0 end # @param point [Point] new top-left corner. # @return [Rect] positioned at the new `left`/`top`. def at(point) Rect.new(point.x, point.y, width, height) end # Centers the rectangle — keeps {#width} and {#height} but modifies # {#top} and {#left} so that the rectangle is centered on a screen. # @param screen_size [Size] screen size # @return [Rect] moved rectangle. def centered(screen_size) at(Point.new((screen_size.width - width) / 2, (screen_size.height - height) / 2)) end # Clamp both width and height and return a rectangle. # @param max_size [Size] the max size # @return [Rect] def clamp(max_size) new_width = width.clamp(nil, max_size.width) new_height = height.clamp(nil, max_size.height) new_width == width && new_height == height ? self : Rect.new(left, top, new_width, new_height) end # @param point [Point] # @return [Boolean] def contains?(point) point.x >= left && point.x < left + width && point.y >= top && point.y < top + height end # @return [Size] def size = Size.new(width, height) # @return [Point] def top_left = Point.new(left, top) end |
#width ⇒ Integer (readonly)
Returns width.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/tuile/rect.rb', line 14 class Rect < Data.define(:left, :top, :width, :height) # @return [String] def to_s = "#{top_left} #{size}" # @return [Boolean] true if either {#width} or {#height} is zero or negative. def empty? width <= 0 || height <= 0 end # @param point [Point] new top-left corner. # @return [Rect] positioned at the new `left`/`top`. def at(point) Rect.new(point.x, point.y, width, height) end # Centers the rectangle — keeps {#width} and {#height} but modifies # {#top} and {#left} so that the rectangle is centered on a screen. # @param screen_size [Size] screen size # @return [Rect] moved rectangle. def centered(screen_size) at(Point.new((screen_size.width - width) / 2, (screen_size.height - height) / 2)) end # Clamp both width and height and return a rectangle. # @param max_size [Size] the max size # @return [Rect] def clamp(max_size) new_width = width.clamp(nil, max_size.width) new_height = height.clamp(nil, max_size.height) new_width == width && new_height == height ? self : Rect.new(left, top, new_width, new_height) end # @param point [Point] # @return [Boolean] def contains?(point) point.x >= left && point.x < left + width && point.y >= top && point.y < top + height end # @return [Size] def size = Size.new(width, height) # @return [Point] def top_left = Point.new(left, top) end |
Instance Method Details
#at(point) ⇒ Rect
Returns positioned at the new ‘left`/`top`.
25 26 27 |
# File 'lib/tuile/rect.rb', line 25 def at(point) Rect.new(point.x, point.y, width, height) end |
#centered(screen_size) ⇒ Rect
33 34 35 |
# File 'lib/tuile/rect.rb', line 33 def centered(screen_size) at(Point.new((screen_size.width - width) / 2, (screen_size.height - height) / 2)) end |
#clamp(max_size) ⇒ Rect
Clamp both width and height and return a rectangle.
40 41 42 43 44 |
# File 'lib/tuile/rect.rb', line 40 def clamp(max_size) new_width = width.clamp(nil, max_size.width) new_height = height.clamp(nil, max_size.height) new_width == width && new_height == height ? self : Rect.new(left, top, new_width, new_height) end |
#contains?(point) ⇒ Boolean
48 49 50 |
# File 'lib/tuile/rect.rb', line 48 def contains?(point) point.x >= left && point.x < left + width && point.y >= top && point.y < top + height end |
#empty? ⇒ Boolean
19 20 21 |
# File 'lib/tuile/rect.rb', line 19 def empty? width <= 0 || height <= 0 end |
#to_s ⇒ String
16 |
# File 'lib/tuile/rect.rb', line 16 def to_s = "#{top_left} #{size}" |