Class: Tuile::Rect

Inherits:
Object
  • Object
show all
Defined in:
lib/tuile/rect.rb

Overview

A rectangle, with integer ‘left`, `top`, `width` and `height`, all 0-based.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#heightInteger (readonly)

Returns height.

Returns:

  • (Integer)

    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

#leftInteger (readonly)

Returns left edge, 0-based.

Returns:

  • (Integer)

    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

#topInteger (readonly)

Returns top edge, 0-based.

Returns:

  • (Integer)

    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

#widthInteger (readonly)

Returns width.

Returns:

  • (Integer)

    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`.

Parameters:

  • point (Point)

    new top-left corner.

Returns:

  • (Rect)

    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

Centers the rectangle — keeps #width and #height but modifies #top and #left so that the rectangle is centered on a screen.

Parameters:

  • screen_size (Size)

    screen size

Returns:

  • (Rect)

    moved rectangle.



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.

Parameters:

  • max_size (Size)

    the max size

Returns:



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

Parameters:

Returns:

  • (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

Returns true if either #width or #height is zero or negative.

Returns:

  • (Boolean)

    true if either #width or #height is zero or negative.



19
20
21
# File 'lib/tuile/rect.rb', line 19

def empty?
  width <= 0 || height <= 0
end

#sizeSize

Returns:



53
# File 'lib/tuile/rect.rb', line 53

def size = Size.new(width, height)

#to_sString

Returns:

  • (String)


16
# File 'lib/tuile/rect.rb', line 16

def to_s = "#{top_left} #{size}"

#top_leftPoint

Returns:



56
# File 'lib/tuile/rect.rb', line 56

def top_left = Point.new(left, top)