class Geom2D::Polygon

Represents a polygon.

Public Class Methods

new(vertices = []) click to toggle source

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

<<(x, y = nil)
Alias for: add
[](i) click to toggle source

Returns the i-th vertex of the polygon.

# File lib/geom2d/polygon.rb, line 35
def [](i)
  @vertices[i]
end
add(x, y = nil) click to toggle source

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
Also aliased as: <<
bbox() click to toggle source

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
ccw?() click to toggle source

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
each_segment() { |Segment(vertices, vertices)| ... } click to toggle source

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
each_vertex(&block) click to toggle source

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
nr_of_contours() click to toggle source

Returns one since a polygon object represents a single polygon.

# File lib/geom2d/polygon.rb, line 25
def nr_of_contours
  1
end
nr_of_vertices() click to toggle source

Returns the number of vertices in the polygon.

# File lib/geom2d/polygon.rb, line 30
def nr_of_vertices
  @vertices.size
end
pop() click to toggle source

Removes the last vertex of the polygon.

# File lib/geom2d/polygon.rb, line 47
def pop
  @vertices.pop
end
reverse!() click to toggle source

Reverses the direction of the vertices (and therefore the segments).

# File lib/geom2d/polygon.rb, line 90
def reverse!
  @vertices.reverse!
end
to_a()
Alias for: to_ary
to_ary() click to toggle source

Returns an array with the vertices of the polygon.

# File lib/geom2d/polygon.rb, line 95
def to_ary
  @vertices.dup
end
Also aliased as: to_a