Class: Tuile::Rect
- Inherits:
-
Object
- Object
- Tuile::Rect
- Defined in:
- lib/tuile/rect.rb,
sig/tuile.rbs
Overview
A rectangle, with integer left, top, width and height, all 0-based.
Instance Attribute Summary collapse
-
#height ⇒ Integer
readonly
@return — height.
-
#left ⇒ Integer
readonly
@return — left edge, 0-based.
-
#top ⇒ Integer
readonly
@return — top edge, 0-based.
-
#width ⇒ Integer
readonly
@return — width.
Instance Method Summary collapse
-
#at(point) ⇒ Rect
@param
point— new top-left corner. - #centered(screen_size) ⇒ Rect
-
#clamp(max_size) ⇒ Rect
Clamp both width and height and return a rectangle.
-
#contains?(point) ⇒ Boolean
@param
point. -
#contains_rect?(other) ⇒ Boolean
@param
other— another rectangle. - #empty? ⇒ Boolean
- #size ⇒ Size
- #to_s ⇒ String
- #top_left ⇒ Point
Instance Attribute Details
#height ⇒ Integer (readonly)
@return — 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 58 59 60 61 62 63 64 65 66 67 68 69 |
# 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 # @param other [Rect] another rectangle. # @return [Boolean] true if `other` lies entirely within this rectangle. # Uses the same half-open edges as {#contains?} (right/bottom exclusive). # An {#empty? empty} `other` covers no cells, so it is trivially contained. def contains_rect?(other) return true if other.empty? other.left >= left && other.top >= top && other.left + other.width <= left + width && other.top + other.height <= top + height end # @return [Size] def size = Size.new(width, height) # @return [Point] def top_left = Point.new(left, top) end |
#left ⇒ Integer (readonly)
@return — 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 58 59 60 61 62 63 64 65 66 67 68 69 |
# 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 # @param other [Rect] another rectangle. # @return [Boolean] true if `other` lies entirely within this rectangle. # Uses the same half-open edges as {#contains?} (right/bottom exclusive). # An {#empty? empty} `other` covers no cells, so it is trivially contained. def contains_rect?(other) return true if other.empty? other.left >= left && other.top >= top && other.left + other.width <= left + width && other.top + other.height <= top + height end # @return [Size] def size = Size.new(width, height) # @return [Point] def top_left = Point.new(left, top) end |
#top ⇒ Integer (readonly)
@return — 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 58 59 60 61 62 63 64 65 66 67 68 69 |
# 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 # @param other [Rect] another rectangle. # @return [Boolean] true if `other` lies entirely within this rectangle. # Uses the same half-open edges as {#contains?} (right/bottom exclusive). # An {#empty? empty} `other` covers no cells, so it is trivially contained. def contains_rect?(other) return true if other.empty? other.left >= left && other.top >= top && other.left + other.width <= left + width && other.top + other.height <= top + height end # @return [Size] def size = Size.new(width, height) # @return [Point] def top_left = Point.new(left, top) end |
#width ⇒ Integer (readonly)
@return — 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 58 59 60 61 62 63 64 65 66 67 68 69 |
# 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 # @param other [Rect] another rectangle. # @return [Boolean] true if `other` lies entirely within this rectangle. # Uses the same half-open edges as {#contains?} (right/bottom exclusive). # An {#empty? empty} `other` covers no cells, so it is trivially contained. def contains_rect?(other) return true if other.empty? other.left >= left && other.top >= top && other.left + other.width <= left + width && other.top + other.height <= 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
@param point — new top-left corner.
@return — 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.
@param max_size — the max size
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
@param point
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 |
#contains_rect?(other) ⇒ Boolean
@param other — another rectangle.
@return — true if other lies entirely within this rectangle.
Uses the same half-open edges as #contains? (right/bottom exclusive).
An empty other covers no cells, so it is trivially contained.
56 57 58 59 60 61 62 |
# File 'lib/tuile/rect.rb', line 56 def contains_rect?(other) return true if other.empty? other.left >= left && other.top >= top && other.left + other.width <= left + width && other.top + other.height <= 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}" |