Class: Charming::Layout::Rect

Inherits:
Data
  • Object
show all
Defined in:
lib/charming/presentation/layout/rect.rb

Overview

Rect is an immutable rectangle with a top-left position (x, y) and dimensions (width, height). Layout operations produce new Rect instances rather than mutating existing ones.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#heightObject (readonly)

Returns the value of attribute height

Returns:

  • (Object)

    the current value of height



8
9
10
# File 'lib/charming/presentation/layout/rect.rb', line 8

def height
  @height
end

#widthObject (readonly)

Returns the value of attribute width

Returns:

  • (Object)

    the current value of width



8
9
10
# File 'lib/charming/presentation/layout/rect.rb', line 8

def width
  @width
end

#xObject (readonly)

Returns the value of attribute x

Returns:

  • (Object)

    the current value of x



8
9
10
# File 'lib/charming/presentation/layout/rect.rb', line 8

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y

Returns:

  • (Object)

    the current value of y



8
9
10
# File 'lib/charming/presentation/layout/rect.rb', line 8

def y
  @y
end

Instance Method Details

#cover?(point_x, point_y) ⇒ Boolean

Returns true when the zero-based cell coordinate falls within this rectangle.

Returns:

  • (Boolean)


10
11
12
# File 'lib/charming/presentation/layout/rect.rb', line 10

def cover?(point_x, point_y)
  point_x >= x && point_x < x + width && point_y >= y && point_y < y + height
end

#inset(top: 0, right: 0, bottom: 0, left: 0) ⇒ Object

Returns a new Rect inset by top/right/bottom/left cells. The result is clamped to a minimum width/height of 0.



16
17
18
19
20
21
22
23
# File 'lib/charming/presentation/layout/rect.rb', line 16

def inset(top: 0, right: 0, bottom: 0, left: 0)
  Rect.new(
    x: x + left,
    y: y + top,
    width: [width - left - right, 0].max,
    height: [height - top - bottom, 0].max
  )
end