Class: Geom2D::Polygon

Inherits:
Object
  • Object
show all
Defined in:
lib/geom2d/polygon.rb

Overview

Represents a polygon.

Instance Method Summary collapse

Constructor Details

#initialize(vertices = []) ⇒ Polygon

Creates a new Polygon object. The vertices argument has to be an array of point-like objects.



20
21
22
23
# File 'lib/geom2d/polygon.rb', line 20

def initialize(vertices = [])
  @vertices = []
  vertices.each {|value| @vertices << Geom2D::Point(value) }
end

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

#bboxObject

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.

Returns:

  • (Boolean)


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.

Yields:



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

#inspectObject Also known as: to_s

:nodoc:



99
100
101
# File 'lib/geom2d/polygon.rb', line 99

def inspect # :nodoc:
  "Polygon#{@vertices}"
end

#nr_of_contoursObject

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_verticesObject

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

#popObject

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_aryObject 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