class Geom2D::Polygon
Represents a polygon.
Public Class Methods
Creates a new Polygon
object. The vertices
argument has to be an array of point-like objects.
# File lib/geom2d/polygon.rb, line 19 def initialize(vertices = []) @vertices = [] vertices.each {|value| @vertices << Geom2D::Point(value) } end
Public Instance Methods
Returns the i-th vertex of the polygon.
# File lib/geom2d/polygon.rb, line 35 def [](i) @vertices[i] end
Adds a new vertex to the end of the polygon.
# File lib/geom2d/polygon.rb, line 40 def add(x, y = nil) @vertices << Geom2D::Point(x, y) self end
Returns the BoundingBox
of this polygon, or an empty BoundingBox
if the polygon has no vertices.
# File lib/geom2d/polygon.rb, line 74 def bbox return BoundingBox.new if @vertices.empty? result = @vertices.first.bbox @vertices[1..-1].each {|v| result.add!(v) } result end
Returns true
if the vertices of the polygon are ordered in a counterclockwise fashion.
# File lib/geom2d/polygon.rb, line 82 def ccw? return true if @vertices.empty? area = @vertices[-1].wedge(@vertices[0]) 1.upto(@vertices.size - 2) {|i| area += @vertices[i].wedge(@vertices[i + 1]) } area >= 0 end
Calls the given block once for each segment in the polygon.
If no block is given, an Enumerator is returned.
# File lib/geom2d/polygon.rb, line 62 def each_segment return to_enum(__method__) unless block_given? return unless @vertices.size > 1 0.upto(@vertices.size - 2) do |i| yield(Geom2D::Segment(@vertices[i], @vertices[i + 1])) end yield(Geom2D::Segment(@vertices[-1], @vertices[0])) end
Calls the given block once for each vertex of the polygon.
If no block is given, an Enumerator is returned.
# File lib/geom2d/polygon.rb, line 54 def each_vertex(&block) return to_enum(__method__) unless block_given? @vertices.each(&block) end
Returns one since a polygon object represents a single polygon.
# File lib/geom2d/polygon.rb, line 25 def nr_of_contours 1 end
Returns the number of vertices in the polygon.
# File lib/geom2d/polygon.rb, line 30 def nr_of_vertices @vertices.size end
Removes the last vertex of the polygon.
# File lib/geom2d/polygon.rb, line 47 def pop @vertices.pop end
Reverses the direction of the vertices (and therefore the segments).
# File lib/geom2d/polygon.rb, line 90 def reverse! @vertices.reverse! end
Returns an array with the vertices of the polygon.
# File lib/geom2d/polygon.rb, line 95 def to_ary @vertices.dup end