Class: Geom2D::PolygonSet

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

Overview

Represents a set of polygons.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(polygons = []) ⇒ PolygonSet

Creates a new PolygonSet with the given polygons.



22
23
24
# File 'lib/geom2d/polygon_set.rb', line 22

def initialize(polygons = [])
  @polygons = polygons
end

Instance Attribute Details

#polygonsObject (readonly)

The array of polygons.



19
20
21
# File 'lib/geom2d/polygon_set.rb', line 19

def polygons
  @polygons
end

Instance Method Details

#add(polygon) ⇒ Object Also known as: <<

Adds a polygon to this set.



27
28
29
30
# File 'lib/geom2d/polygon_set.rb', line 27

def add(polygon)
  @polygons << polygon
  self
end

#bboxObject

Returns the BoundingBox of all polygons in the set, or nil if it contains no polygon.



53
54
55
56
57
58
# File 'lib/geom2d/polygon_set.rb', line 53

def bbox
  return BoundingBox.new if @polygons.empty?
  result = @polygons.first.bbox
  @polygons[1..-1].each {|v| result.add!(v.bbox) }
  result
end

#each_segment(&block) ⇒ Object

Calls the given block once for each segment of each polygon in the set.

If no block is given, an Enumerator is returned.



42
43
44
45
# File 'lib/geom2d/polygon_set.rb', line 42

def each_segment(&block)
  return to_enum(__method__) unless block_given?
  @polygons.each {|polygon| polygon.each_segment(&block) }
end

#inspectObject Also known as: to_s

:nodoc:



60
61
62
# File 'lib/geom2d/polygon_set.rb', line 60

def inspect # :nodoc:
  "PolygonSet#{@polygons}"
end

#join(other) ⇒ Object Also known as: +

Creates a new polygon set by combining the polygons from this set and the other one.



34
35
36
# File 'lib/geom2d/polygon_set.rb', line 34

def join(other)
  PolygonSet.new(@polygons + other.polygons)
end

#nr_of_contoursObject

Returns the number of polygons in this set.



48
49
50
# File 'lib/geom2d/polygon_set.rb', line 48

def nr_of_contours
  @polygons.size
end