Method: Geometry::Polygon#union

Defined in:
lib/geometry/polygon.rb

#union(other) ⇒ Polygon Also known as: +

Create a new Geometry::Polygon that’s the union of the receiver and a passed Geometry::Polygon

This is a simplified implementation of the alogrithm outlined in the
paper {http://gvu.gatech.edu/people/official/jarek/graphics/papers/04PolygonBooleansMargalit.pdf An algorithm for computing the union, intersection or difference of two polygons}.
In particular, this method assumes the receiver and passed {Polygon}s are "island" type and that the desired output is "regular", as those terms are described in the paper.

Parameters:

Returns:



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/geometry/polygon.rb', line 99

def union(other)
    # Table 1: Both polygons are islands and the operation is union, so both must have the same orientation
    # Reverse the other polygon if the orientations are different
    other = other.reverse if self.clockwise? != other.clockwise?

    # Receiver's vertex ring
    ringA = VertexRing.new
    self.vertices.each {|v| ringA.push v, (other <=> v)}

    # The other vertex ring
    ringB = VertexRing.new
    other.vertices.each {|v| ringB.push v, (self <=> v)}

    # Find intersections
    offsetA = 0
    edgesB = other.edges.dup
    self.edges.each_with_index do |a, indexA|
  offsetB = 0
  ringB.edges_with_index do |b, indexB|
      intersection = a.intersection(b)
      if intersection === true
    if (a.first == b.first) and (a.last == b.last)      # Equal edges
    elsif (a.first == b.last) and (a.last == b.first)   # Ignore equal but opposite edges
    else
        if a.vector.normalize == b.vector.normalize # Same direction?
      offsetA += 1 if ringA.insert_boundary(indexA + 1 + offsetA, b.first)
      offsetB += 1 if ringB.insert_boundary(indexB + 1 + offsetB, a.last)
        else    # Opposite direction
      offsetA += 1 if ringA.insert_boundary(indexA + 1 + offsetA, b.last)
      offsetB += 1 if ringB.insert_boundary(indexB + 1 + offsetB, a.first)
        end
    end
      elsif intersection.is_a?(Point)
    offsetA += 1 if ringA.insert_boundary(indexA + 1 + offsetA, intersection)
    offsetB += 1 if ringB.insert_boundary(indexB + 1 + offsetB, intersection)
      end
  end
    end

    # Table 2: Both polygons are islands and the operation is union, so select outside from both polygons
    edgeFragments = []
    [[ringA, other], [ringB, self]].each do |ring, other_polygon|
  ring.edges do |v1,v2|
      if (v1[:type] == -1) or (v2[:type] == -1)
    edgeFragments.push :first => v1[:vertex], :last => v2[:vertex]
      elsif (v1[:type] == 0) and (v2[:type] == 0)
    if (other_polygon <=> Point[(v1[:vertex] + v2[:vertex])/2]) <= 0
        edgeFragments.push :first => v1[:vertex], :last => v2[:vertex]
    end
      end
  end
    end

    # Delete any duplicated edges. Array#uniq doesn't do the right thing, so using inject instead.
    edgeFragments = edgeFragments.inject([]) {|result,h| result << h unless result.include?(h); result}

    # Delete any equal-and-opposite edges
    edgeFragments = edgeFragments.reject {|f| edgeFragments.find {|f2| (f[:first] == f2[:last]) and (f[:last] == f2[:first])} }

    # Construct the output polygons
    output = edgeFragments.reduce([Array.new]) do |output, fragment|
  next output if fragment.empty?
  polygon = output.last
  polygon.push fragment[:first], fragment[:last] if polygon.empty?
  while 1 do
      adjacent_fragment = edgeFragments.find {|f| fragment[:last] == f[:first]}
      break unless adjacent_fragment

      polygon.push adjacent_fragment[:first], adjacent_fragment[:last]
      fragment = adjacent_fragment.dup
      adjacent_fragment.clear

      break if polygon.first == polygon.last # closed?
  end
  output << Array.new
    end

    # If everything worked properly there should be only one output Polygon
    output.reject! {|a| a.empty?}
    output = Polygon.new *(output[0])

    # Table 4: Both input polygons are "island" type and the operation
    #  is union, so the output polygon's orientation should be the same
    #  as the input polygon's orientation
    (self.clockwise? != output.clockwise?) ? output.reverse : output
end