Class: Geom2D::Polygon
- Inherits:
-
Object
- Object
- Geom2D::Polygon
- Defined in:
- lib/geom2d/polygon.rb
Overview
Represents a polygon.
Instance Method Summary collapse
-
#[](i) ⇒ Object
Returns the i-th vertex of the polygon.
-
#add(x, y = nil) ⇒ Object
(also: #<<)
Adds a new vertex to the end of the polygon.
-
#bbox ⇒ Object
Returns the BoundingBox of this polygon, or an empty BoundingBox if the polygon has no vertices.
-
#ccw? ⇒ Boolean
Returns
true
if the vertices of the polygon are ordered in a counterclockwise fashion. -
#each_segment {|Geom2D::Segment.new(@vertices[-1], @vertices[0])| ... } ⇒ Object
Calls the given block once for each segment in the polygon.
-
#each_vertex(&block) ⇒ Object
Calls the given block once for each vertex of the polygon.
-
#initialize(vertices = []) ⇒ Polygon
constructor
Creates a new Polygon object.
-
#inspect ⇒ Object
(also: #to_s)
:nodoc:.
-
#nr_of_contours ⇒ Object
Returns one since a polygon object represents a single polygon.
-
#nr_of_vertices ⇒ Object
Returns the number of vertices in the polygon.
-
#pop ⇒ Object
Removes the last vertex of the polygon.
-
#reverse! ⇒ Object
Reverses the direction of the vertices (and therefore the segments).
-
#to_ary ⇒ Object
(also: #to_a)
Returns an array with the vertices of the polygon.
Constructor Details
Instance Method Details
#[](i) ⇒ Object
Returns the i-th vertex of the polygon.
36 37 38 |
# File 'lib/geom2d/polygon.rb', line 36 def [](i) @vertices[i] end |
#add(x, y = nil) ⇒ Object Also known as: <<
Adds a new vertex to the end of the polygon.
41 42 43 44 |
# File 'lib/geom2d/polygon.rb', line 41 def add(x, y = nil) @vertices << Geom2D::Point(x, y) self end |
#bbox ⇒ Object
Returns the BoundingBox of this polygon, or an empty BoundingBox if the polygon has no vertices.
73 74 75 76 77 78 |
# File 'lib/geom2d/polygon.rb', line 73 def bbox return BoundingBox.new if @vertices.empty? result = @vertices.first.bbox @vertices[1..-1].each {|v| result.add!(v) } result end |
#ccw? ⇒ Boolean
Returns true
if the vertices of the polygon are ordered in a counterclockwise fashion.
81 82 83 84 85 86 |
# File 'lib/geom2d/polygon.rb', line 81 def ccw? return true if @vertices.empty? area = @vertices[-1].wedge(@vertices[0]) 0.upto(@vertices.size - 2) {|i| area += @vertices[i].wedge(@vertices[i + 1]) } area >= 0 end |
#each_segment {|Geom2D::Segment.new(@vertices[-1], @vertices[0])| ... } ⇒ Object
Calls the given block once for each segment in the polygon.
If no block is given, an Enumerator is returned.
63 64 65 66 67 68 69 |
# File 'lib/geom2d/polygon.rb', line 63 def each_segment return to_enum(__method__) unless block_given? return unless @vertices.size > 1 @vertices.each_cons(2) {|v1, v2| yield(Geom2D::Segment.new(v1, v2)) } yield(Geom2D::Segment.new(@vertices[-1], @vertices[0])) end |
#each_vertex(&block) ⇒ Object
Calls the given block once for each vertex of the polygon.
If no block is given, an Enumerator is returned.
55 56 57 58 |
# File 'lib/geom2d/polygon.rb', line 55 def each_vertex(&block) return to_enum(__method__) unless block_given? @vertices.each(&block) end |
#inspect ⇒ Object Also known as: to_s
:nodoc:
99 100 101 |
# File 'lib/geom2d/polygon.rb', line 99 def inspect # :nodoc: "Polygon#{@vertices}" end |
#nr_of_contours ⇒ Object
Returns one since a polygon object represents a single polygon.
26 27 28 |
# File 'lib/geom2d/polygon.rb', line 26 def nr_of_contours 1 end |
#nr_of_vertices ⇒ Object
Returns the number of vertices in the polygon.
31 32 33 |
# File 'lib/geom2d/polygon.rb', line 31 def nr_of_vertices @vertices.size end |
#pop ⇒ Object
Removes the last vertex of the polygon.
48 49 50 |
# File 'lib/geom2d/polygon.rb', line 48 def pop @vertices.pop end |
#reverse! ⇒ Object
Reverses the direction of the vertices (and therefore the segments).
89 90 91 |
# File 'lib/geom2d/polygon.rb', line 89 def reverse! @vertices.reverse! end |
#to_ary ⇒ Object Also known as: to_a
Returns an array with the vertices of the polygon.
94 95 96 |
# File 'lib/geom2d/polygon.rb', line 94 def to_ary @vertices.dup end |