Class: Sevgi::Geometry::Rect

Inherits:
RectBase
  • Object
show all
Defined in:
lib/sevgi/geometry/elements/rect.rb

Overview

Closed four-sided rectangle aligned to the screen axes.

Direct Known Subclasses

Square

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](width, height, position: Origin) ⇒ Sevgi::Geometry::Rect

Builds a rectangle from size and top-left position.

Parameters:

  • width (Numeric)

    rectangle width

  • height (Numeric)

    rectangle height

  • position (Sevgi::Geometry::Point, Array<Numeric>) (defaults to: Origin)

    top-left position

Returns:

Raises:



19
# File 'lib/sevgi/geometry/elements/rect.rb', line 19

def self.[](...) = from_size(...)

.call(top_left, bottom_right) ⇒ Sevgi::Geometry::Rect

Builds a rectangle from two opposite corners.

Parameters:

Returns:

Raises:



27
# File 'lib/sevgi/geometry/elements/rect.rb', line 27

def self.call(...) = from_corners(...)

.from_corners(top_left, bottom_right) ⇒ Sevgi::Geometry::Rect

Builds a rectangle from two opposite corners.

Parameters:

Returns:

Raises:



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/sevgi/geometry/elements/rect.rb', line 34

def self.from_corners(top_left, bottom_right)
  top_left, bottom_right = Tuples[Point, top_left, bottom_right]
  width = (bottom_right.x - top_left.x).abs

  new_by_points(
    top_left,
    top_left.translate(width, 0.0),
    bottom_right,
    bottom_right.translate(-width, 0.0)
  )
end

.from_size(width, height, position: Origin) ⇒ Sevgi::Geometry::Rect

Builds a rectangle from size and top-left position.

Parameters:

  • width (Numeric)

    rectangle width

  • height (Numeric)

    rectangle height

  • position (Sevgi::Geometry::Point, Array<Numeric>) (defaults to: Origin)

    top-left position

Returns:

Raises:



52
53
54
55
56
57
58
59
60
# File 'lib/sevgi/geometry/elements/rect.rb', line 52

def self.from_size(width, height, position: Origin)
  new_by_segments(
    Segment.rightward(width),
    Segment.downward(height),
    Segment.leftward(width),
    Segment.upward(height),
    position:
  )
end

Instance Method Details

#draw!(node) ⇒ Object

Draws the rectangle into a graphics node.

Parameters:

  • node (Object)

    graphics node receiving the drawing command

Returns:

  • (Object)

    graphics node command result



65
# File 'lib/sevgi/geometry/elements/rect.rb', line 65

def draw!(node, **) = node.rect(x: position.x, y: position.y, width: width, height: height, **)

#heightFloat

Returns rectangle height.

Returns:

  • (Float)


69
# File 'lib/sevgi/geometry/elements/rect.rb', line 69

def height = @height ||= segments[1].length

#widthFloat

Returns rectangle width.

Returns:

  • (Float)


73
# File 'lib/sevgi/geometry/elements/rect.rb', line 73

def width = @width ||= segments[0].length