Class: Sevgi::Geometry::Rect
- Inherits:
-
RectBase
- Object
- Sevgi::Geometry::Rect
- Defined in:
- lib/sevgi/geometry/elements/rect.rb
Overview
Closed four-sided rectangle aligned to the screen axes.
Direct Known Subclasses
Class Method Summary collapse
-
.[](width, height, position: Origin) ⇒ Sevgi::Geometry::Rect
Builds a rectangle from size and top-left position.
-
.call(top_left, bottom_right) ⇒ Sevgi::Geometry::Rect
Builds a rectangle from two opposite corners.
-
.from_corners(top_left, bottom_right) ⇒ Sevgi::Geometry::Rect
Builds a rectangle from two opposite corners.
-
.from_size(width, height, position: Origin) ⇒ Sevgi::Geometry::Rect
Builds a rectangle from size and top-left position.
Instance Method Summary collapse
-
#bottom ⇒ Sevgi::Geometry::Line
Returns the bottom side line.
-
#bottom_left ⇒ Sevgi::Geometry::Point
Returns the bottom-left corner.
-
#bottom_right ⇒ Sevgi::Geometry::Point
Returns the bottom-right corner.
-
#draw!(node) ⇒ Object
Draws the rectangle into a graphics node.
-
#height ⇒ Float
Returns rectangle height.
-
#left ⇒ Sevgi::Geometry::Line
Returns the left side line.
-
#right ⇒ Sevgi::Geometry::Line
Returns the right side line.
-
#top ⇒ Sevgi::Geometry::Line
Returns the top side line.
-
#top_left ⇒ Sevgi::Geometry::Point
Returns the top-left corner.
-
#top_right ⇒ Sevgi::Geometry::Point
Returns the top-right corner.
-
#width ⇒ Float
Returns rectangle width.
Class Method Details
.[](width, height, position: Origin) ⇒ Sevgi::Geometry::Rect
19 |
# File 'lib/sevgi/geometry/elements/rect.rb', line 19 def self.[](...) = from_size(...) |
.call(top_left, bottom_right) ⇒ Sevgi::Geometry::Rect
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.
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.
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
#bottom ⇒ Sevgi::Geometry::Line
Returns the bottom side line.
111 112 113 |
# File 'lib/sevgi/geometry/elements/rect.rb', line 111 %i[top right bottom left].each_with_index do |side, i| define_method(side) { lines[i] } end |
#bottom_left ⇒ Sevgi::Geometry::Point
Returns the bottom-left corner.
91 92 93 |
# File 'lib/sevgi/geometry/elements/rect.rb', line 91 %i[top_left top_right bottom_right bottom_left].each_with_index do |corner, i| define_method(corner) { points[i] } end |
#bottom_right ⇒ Sevgi::Geometry::Point
Returns the bottom-right corner.
91 92 93 |
# File 'lib/sevgi/geometry/elements/rect.rb', line 91 %i[top_left top_right bottom_right bottom_left].each_with_index do |corner, i| define_method(corner) { points[i] } end |
#draw!(node) ⇒ Object
Draws the rectangle into a graphics node.
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, **) |
#height ⇒ Float
Returns rectangle height.
69 |
# File 'lib/sevgi/geometry/elements/rect.rb', line 69 def height = @height ||= segments[1].length |
#left ⇒ Sevgi::Geometry::Line
Returns the left side line.
111 112 113 |
# File 'lib/sevgi/geometry/elements/rect.rb', line 111 %i[top right bottom left].each_with_index do |side, i| define_method(side) { lines[i] } end |
#right ⇒ Sevgi::Geometry::Line
Returns the right side line.
111 112 113 |
# File 'lib/sevgi/geometry/elements/rect.rb', line 111 %i[top right bottom left].each_with_index do |side, i| define_method(side) { lines[i] } end |
#top ⇒ Sevgi::Geometry::Line
Returns the top side line.
111 112 113 |
# File 'lib/sevgi/geometry/elements/rect.rb', line 111 %i[top right bottom left].each_with_index do |side, i| define_method(side) { lines[i] } end |
#top_left ⇒ Sevgi::Geometry::Point
Returns the top-left corner.
91 92 93 |
# File 'lib/sevgi/geometry/elements/rect.rb', line 91 %i[top_left top_right bottom_right bottom_left].each_with_index do |corner, i| define_method(corner) { points[i] } end |
#top_right ⇒ Sevgi::Geometry::Point
Returns the top-right corner.
91 92 93 |
# File 'lib/sevgi/geometry/elements/rect.rb', line 91 %i[top_left top_right bottom_right bottom_left].each_with_index do |corner, i| define_method(corner) { points[i] } end |
#width ⇒ Float
Returns rectangle width.
73 |
# File 'lib/sevgi/geometry/elements/rect.rb', line 73 def width = @width ||= segments[0].length |