Class: Geom2D::Rectangle
- Inherits:
-
Object
- Object
- Geom2D::Rectangle
- Defined in:
- lib/geom2d/rectangle.rb
Overview
Represents an axis aligned rectangle.
Instance Attribute Summary collapse
-
#height ⇒ Object
readonly
The height of the rectangle.
-
#width ⇒ Object
readonly
The width of the rectangle.
-
#x ⇒ Object
readonly
The x-coordinate of the bottom-left corner of the rectangle.
-
#y ⇒ Object
readonly
The y-coordinate of the bottom-left corner of the rectangle.
Instance Method Summary collapse
-
#bbox ⇒ Object
Returns the BoundingBox of this rectangle.
-
#ccw? ⇒ Boolean
Returns
true
since the vertices of the rectangle are always ordered in a counterclockwise fashion. -
#each_segment {|Geom2D::Segment.new(v[-1], v[0])| ... } ⇒ Object
Calls the given block once for each segment of the rectangle.
-
#each_vertex(&block) ⇒ Object
Calls the given block once for each corner of the rectangle.
-
#initialize(x, y, width, height) ⇒ Rectangle
constructor
Creates a new Rectangle object, with (x, y) specifying the bottom-left corner of the rectangle.
-
#inspect ⇒ Object
(also: #to_s)
:nodoc:.
-
#nr_of_contours ⇒ Object
Returns one since a rectangle object is a single polygon.
-
#nr_of_vertices ⇒ Object
Returns four since a rectangle has four vertices.
-
#to_ary ⇒ Object
(also: #to_a)
Returns an array with the vertices of the rectangle.
Constructor Details
#initialize(x, y, width, height) ⇒ Rectangle
Creates a new Rectangle object, with (x, y) specifying the bottom-left corner of the rectangle.
38 39 40 41 42 43 |
# File 'lib/geom2d/rectangle.rb', line 38 def initialize(x, y, width, height) @x = x @y = y @width = width @height = height end |
Instance Attribute Details
#height ⇒ Object (readonly)
The height of the rectangle.
35 36 37 |
# File 'lib/geom2d/rectangle.rb', line 35 def height @height end |
#width ⇒ Object (readonly)
The width of the rectangle.
32 33 34 |
# File 'lib/geom2d/rectangle.rb', line 32 def width @width end |
#x ⇒ Object (readonly)
The x-coordinate of the bottom-left corner of the rectangle.
26 27 28 |
# File 'lib/geom2d/rectangle.rb', line 26 def x @x end |
#y ⇒ Object (readonly)
The y-coordinate of the bottom-left corner of the rectangle.
29 30 31 |
# File 'lib/geom2d/rectangle.rb', line 29 def y @y end |
Instance Method Details
#bbox ⇒ Object
Returns the BoundingBox of this rectangle.
75 76 77 |
# File 'lib/geom2d/rectangle.rb', line 75 def bbox BoundingBox.new(x, y, x + width, y + height) end |
#ccw? ⇒ Boolean
Returns true
since the vertices of the rectangle are always ordered in a counterclockwise fashion.
81 82 83 |
# File 'lib/geom2d/rectangle.rb', line 81 def ccw? true end |
#each_segment {|Geom2D::Segment.new(v[-1], v[0])| ... } ⇒ Object
Calls the given block once for each segment of the rectangle.
If no block is given, an Enumerator is returned.
66 67 68 69 70 71 72 |
# File 'lib/geom2d/rectangle.rb', line 66 def each_segment return to_enum(__method__) unless block_given? v = vertices v.each_cons(2) {|v1, v2| yield(Geom2D::Segment.new(v1, v2)) } yield(Geom2D::Segment.new(v[-1], v[0])) end |
#each_vertex(&block) ⇒ Object
Calls the given block once for each corner of the rectangle.
If no block is given, an Enumerator is returned.
58 59 60 61 |
# File 'lib/geom2d/rectangle.rb', line 58 def each_vertex(&block) return to_enum(__method__) unless block_given? vertices.each(&block) end |
#inspect ⇒ Object Also known as: to_s
:nodoc:
85 86 87 |
# File 'lib/geom2d/rectangle.rb', line 85 def inspect # :nodoc: "Rectangle[(#{@x},#{@y}),width=#{@width},height=#{@height}]" end |
#nr_of_contours ⇒ Object
Returns one since a rectangle object is a single polygon.
46 47 48 |
# File 'lib/geom2d/rectangle.rb', line 46 def nr_of_contours 1 end |
#nr_of_vertices ⇒ Object
Returns four since a rectangle has four vertices.
51 52 53 |
# File 'lib/geom2d/rectangle.rb', line 51 def nr_of_vertices 4 end |
#to_ary ⇒ Object Also known as: to_a
Returns an array with the vertices of the rectangle.
91 92 93 |
# File 'lib/geom2d/rectangle.rb', line 91 def to_ary vertices end |