winding-polygon 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/winding-polygon/avltree.rb +228 -226
- data/lib/winding-polygon/event_queue.rb +30 -28
- data/lib/winding-polygon/point.rb +39 -37
- data/lib/winding-polygon/polygon.rb +88 -86
- data/lib/winding-polygon/segment.rb +38 -37
- data/lib/winding-polygon/sweep_line.rb +109 -107
- data/lib/winding-polygon/version.rb +1 -1
- data/spec/event_queue_spec.rb +2 -2
- data/spec/point_spec.rb +14 -14
- data/spec/polygon_spec.rb +24 -24
- data/spec/segment_spec.rb +6 -6
- data/spec/sweep_line_spec.rb +3 -3
- data/spec/winding-polygon_spec.rb +3 -3
- data/test/test_avltree.rb +2 -2
- metadata +9 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8f09f3e08429b83b14f827767c614a710c744d00
|
4
|
+
data.tar.gz: 3a2490fe74d30ffccb353b72dad90df6d381950f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: adde2609596a7b83a81a234cb4a1d66fde5cade1663f28aeece7361147490a48a61f9d29d164ac7382a20df5ee54361b4ed0b732043f654607321e193b2d8cf0
|
7
|
+
data.tar.gz: 13babb54197e2582cc71fed07e3aa08a2c13f8ced36c70c51e5bd528e2a1f4e61309cd741533140759c60e453891465bb44b378dc0c1f16a9b039224ce22a5af
|
@@ -1,287 +1,289 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
1
|
+
module WindingPolygon
|
2
|
+
class AVLTree
|
3
|
+
attr_accessor :root
|
4
|
+
def initialize(array = [])
|
5
|
+
#create root
|
6
|
+
@root = nil
|
7
|
+
array.each do |n|
|
8
|
+
insert n
|
9
|
+
end
|
8
10
|
end
|
9
|
-
end
|
10
11
|
|
11
|
-
|
12
|
-
|
12
|
+
def insert(v)
|
13
|
+
i_node = create_node(v)
|
14
|
+
|
15
|
+
prev_node = nil
|
16
|
+
curr_node = @root
|
17
|
+
while !curr_node.nil?
|
18
|
+
prev_node = curr_node
|
19
|
+
if i_node.value < curr_node.value
|
20
|
+
curr_node = curr_node.left
|
21
|
+
else
|
22
|
+
curr_node = curr_node.right
|
23
|
+
end
|
24
|
+
end
|
13
25
|
|
14
|
-
|
15
|
-
|
16
|
-
while !curr_node.nil?
|
17
|
-
prev_node = curr_node
|
18
|
-
if i_node.value < curr_node.value
|
19
|
-
curr_node = curr_node.left
|
26
|
+
if prev_node.nil?
|
27
|
+
@root = i_node
|
20
28
|
else
|
21
|
-
|
29
|
+
i_node.parent = prev_node
|
30
|
+
if i_node.value < prev_node.value
|
31
|
+
prev_node.left = i_node
|
32
|
+
else
|
33
|
+
prev_node.right = i_node
|
34
|
+
end
|
22
35
|
end
|
36
|
+
i_node.update_height
|
37
|
+
@root = i_node.balance
|
38
|
+
i_node
|
39
|
+
end
|
40
|
+
|
41
|
+
def search(v)
|
42
|
+
search_node(@root, v)
|
23
43
|
end
|
24
44
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
if
|
30
|
-
|
45
|
+
def delete(v)
|
46
|
+
d = search(v)
|
47
|
+
return nil if d.nil?
|
48
|
+
|
49
|
+
if d.left == nil or d.right == nil
|
50
|
+
n = d
|
51
|
+
else #both children exist
|
52
|
+
n = d.next
|
53
|
+
end
|
54
|
+
|
55
|
+
if !n.left.nil?
|
56
|
+
b = n.left
|
31
57
|
else
|
32
|
-
|
58
|
+
b = n.right
|
33
59
|
end
|
34
|
-
end
|
35
|
-
i_node.update_height
|
36
|
-
@root = i_node.balance
|
37
|
-
i_node
|
38
|
-
end
|
39
60
|
|
40
|
-
|
41
|
-
|
42
|
-
|
61
|
+
b.parent = n.parent unless b.nil?
|
62
|
+
|
63
|
+
if n.parent.nil?
|
64
|
+
@root = b
|
65
|
+
elsif n == n.parent.left
|
66
|
+
n.parent.left = b
|
67
|
+
else
|
68
|
+
n.parent.right = b
|
69
|
+
end
|
43
70
|
|
44
|
-
|
45
|
-
d = search(v)
|
46
|
-
return nil if d.nil?
|
71
|
+
d.value = n.value unless n == d
|
47
72
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
73
|
+
if b.nil?
|
74
|
+
n.parent.update_height if !n.parent.nil?
|
75
|
+
@root = n.balance
|
76
|
+
else
|
77
|
+
b.update_height
|
78
|
+
@root = b.balance
|
79
|
+
end
|
80
|
+
n
|
52
81
|
end
|
53
82
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
b = n.right
|
83
|
+
def print
|
84
|
+
result = sort
|
85
|
+
puts result.inspect
|
58
86
|
end
|
59
87
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
elsif n == n.parent.left
|
65
|
-
n.parent.left = b
|
66
|
-
else
|
67
|
-
n.parent.right = b
|
88
|
+
def sort
|
89
|
+
result = Array.new
|
90
|
+
sort_node(@root, result )
|
91
|
+
result
|
68
92
|
end
|
69
93
|
|
70
|
-
|
94
|
+
def min
|
95
|
+
@root.min
|
96
|
+
end
|
71
97
|
|
72
|
-
|
73
|
-
|
74
|
-
@root = n.balance
|
75
|
-
else
|
76
|
-
b.update_height
|
77
|
-
@root = b.balance
|
98
|
+
def max
|
99
|
+
@root.max
|
78
100
|
end
|
79
|
-
n
|
80
|
-
end
|
81
101
|
|
82
|
-
def print
|
83
|
-
result = sort
|
84
|
-
puts result.inspect
|
85
|
-
end
|
86
102
|
|
87
|
-
def sort
|
88
|
-
result = Array.new
|
89
|
-
sort_node(@root, result )
|
90
|
-
result
|
91
|
-
end
|
92
103
|
|
93
|
-
|
94
|
-
|
95
|
-
|
104
|
+
private
|
105
|
+
def create_node(v = nil)
|
106
|
+
n = AVLNode.new(:value => v)
|
107
|
+
end
|
96
108
|
|
97
|
-
|
98
|
-
|
99
|
-
|
109
|
+
def sort_node(n, array)
|
110
|
+
if !n.nil?
|
111
|
+
sort_node(n.left, array)
|
112
|
+
array.push(n.value)
|
113
|
+
sort_node(n.right, array)
|
114
|
+
end
|
115
|
+
end
|
100
116
|
|
117
|
+
def search_node(n, v)
|
118
|
+
if !n.nil?
|
119
|
+
if n.value == v
|
120
|
+
return n
|
121
|
+
elsif v < n.value
|
122
|
+
search_node(n.left,v)
|
123
|
+
else
|
124
|
+
search_node(n.right,v)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
101
129
|
|
130
|
+
class AVLNode
|
131
|
+
BAL_H = 1
|
132
|
+
attr_accessor :height, :left, :right, :value, :parent
|
133
|
+
def initialize args
|
134
|
+
@height = 0
|
135
|
+
@left = nil
|
136
|
+
@right = nil
|
137
|
+
@value = nil
|
138
|
+
@parent = nil
|
139
|
+
args.each do |k,v|
|
140
|
+
instance_variable_set("@#{k}", v) unless v.nil?
|
141
|
+
end
|
142
|
+
end
|
102
143
|
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
144
|
+
def balance
|
145
|
+
rotate if difference.abs > BAL_H
|
146
|
+
return self if @parent.nil?
|
147
|
+
@parent.balance
|
148
|
+
end
|
107
149
|
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
150
|
+
def update_height
|
151
|
+
l_height = @left.nil? ? 0 : @left.height
|
152
|
+
r_height = @right.nil? ? 0 : @right.height
|
153
|
+
@height = ((l_height > r_height) ? l_height : r_height) + 1
|
154
|
+
@parent.update_height unless @parent.nil?
|
113
155
|
end
|
114
|
-
end
|
115
156
|
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
157
|
+
def rotate
|
158
|
+
if difference >= BAL_H
|
159
|
+
#check if children should rotate too
|
160
|
+
@right.rotate if @right.difference <= -BAL_H
|
161
|
+
rotate_left
|
162
|
+
elsif difference <= - BAL_H
|
163
|
+
#check if children should rotate too
|
164
|
+
@left.rotate if @left.difference >= BAL_H
|
165
|
+
rotate_right
|
124
166
|
end
|
125
167
|
end
|
126
|
-
end
|
127
|
-
end
|
128
|
-
|
129
|
-
class AVLNode
|
130
|
-
BAL_H = 1
|
131
|
-
attr_accessor :height, :left, :right, :value, :parent
|
132
|
-
def initialize args
|
133
|
-
@height = 0
|
134
|
-
@left = nil
|
135
|
-
@right = nil
|
136
|
-
@value = nil
|
137
|
-
@parent = nil
|
138
|
-
args.each do |k,v|
|
139
|
-
instance_variable_set("@#{k}", v) unless v.nil?
|
140
|
-
end
|
141
|
-
end
|
142
168
|
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
169
|
+
def rotate_left
|
170
|
+
#the old right is now the root
|
171
|
+
root = @right
|
172
|
+
root.parent = @parent
|
173
|
+
#update the parent to point to the new root
|
174
|
+
if not @parent.nil?
|
175
|
+
if @parent.right == self
|
176
|
+
@parent.right = root
|
177
|
+
else
|
178
|
+
@parent.left = root
|
179
|
+
end
|
180
|
+
end
|
148
181
|
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
@height = ((l_height > r_height) ? l_height : r_height) + 1
|
153
|
-
@parent.update_height unless @parent.nil?
|
154
|
-
end
|
182
|
+
#this node's right is now the root's left
|
183
|
+
@right = root.left
|
184
|
+
root.left.parent = self unless root.left.nil?
|
155
185
|
|
156
|
-
|
157
|
-
|
158
|
-
#
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
#check if children should rotate too
|
163
|
-
@left.rotate if @left.difference >= BAL_H
|
164
|
-
rotate_right
|
186
|
+
#the root is now the parent of this node
|
187
|
+
@parent = root
|
188
|
+
#this node is now the root's left
|
189
|
+
root.left = self
|
190
|
+
root.left.update_height
|
191
|
+
root
|
165
192
|
end
|
166
|
-
end
|
167
193
|
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
194
|
+
def rotate_right
|
195
|
+
root = @left
|
196
|
+
root.parent = @parent
|
197
|
+
#update the parent to point to the new root
|
198
|
+
if not @parent.nil?
|
199
|
+
if @parent.right == self
|
200
|
+
@parent.right = root
|
201
|
+
else
|
202
|
+
@parent.left = root
|
203
|
+
end
|
178
204
|
end
|
179
|
-
end
|
180
205
|
|
181
|
-
|
182
|
-
|
183
|
-
root.left.parent = self unless root.left.nil?
|
206
|
+
@left = root.right
|
207
|
+
root.right.parent = self unless root.right.nil?
|
184
208
|
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
209
|
+
@parent = root
|
210
|
+
root.right = self
|
211
|
+
root.right.update_height
|
212
|
+
root
|
213
|
+
end
|
214
|
+
|
215
|
+
def difference
|
216
|
+
r_height = @right.nil? ? 0 : @right.height
|
217
|
+
l_height = @left.nil? ? 0 : @left.height
|
218
|
+
r_height - l_height
|
219
|
+
end
|
192
220
|
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
#update the parent to point to the new root
|
197
|
-
if not @parent.nil?
|
198
|
-
if @parent.right == self
|
199
|
-
@parent.right = root
|
221
|
+
def next
|
222
|
+
if not @right.nil?
|
223
|
+
@right.min
|
200
224
|
else
|
201
|
-
|
225
|
+
curr_node = self
|
226
|
+
p_node = @parent
|
227
|
+
while p_node != nil && curr_node == p_node.right
|
228
|
+
curr_node = p_node
|
229
|
+
p_node = p_node.parent
|
230
|
+
end
|
231
|
+
p_node
|
202
232
|
end
|
203
233
|
end
|
204
234
|
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
235
|
+
def prev
|
236
|
+
if not @left.nil?
|
237
|
+
@left.max
|
238
|
+
else
|
239
|
+
curr_node = self
|
240
|
+
p_node = @parent
|
241
|
+
while p_node != nil && curr_node == p_node.left
|
242
|
+
curr_node = p_node
|
243
|
+
p_node = p_node.parent
|
244
|
+
end
|
245
|
+
p_node
|
246
|
+
end
|
247
|
+
end
|
213
248
|
|
214
|
-
def difference
|
215
|
-
r_height = @right.nil? ? 0 : @right.height
|
216
|
-
l_height = @left.nil? ? 0 : @left.height
|
217
|
-
r_height - l_height
|
218
|
-
end
|
219
249
|
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
curr_node = self
|
225
|
-
p_node = @parent
|
226
|
-
while p_node != nil && curr_node == p_node.right
|
227
|
-
curr_node = p_node
|
228
|
-
p_node = p_node.parent
|
250
|
+
def min
|
251
|
+
node = self
|
252
|
+
while !node.left.nil?
|
253
|
+
node = node.left
|
229
254
|
end
|
230
|
-
|
255
|
+
node
|
231
256
|
end
|
232
|
-
end
|
233
257
|
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
curr_node = self
|
239
|
-
p_node = @parent
|
240
|
-
while p_node != nil && curr_node == p_node.left
|
241
|
-
curr_node = p_node
|
242
|
-
p_node = p_node.parent
|
258
|
+
def max
|
259
|
+
node = self
|
260
|
+
while !node.right.nil?
|
261
|
+
node = node.right
|
243
262
|
end
|
244
|
-
|
263
|
+
node
|
245
264
|
end
|
246
|
-
end
|
247
|
-
|
248
265
|
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
266
|
+
def print_node
|
267
|
+
out_s = "\t\t\t\t\t\n"
|
268
|
+
out_s += "\t\t#{@parent}\t\t\n"
|
269
|
+
out_s += "#{@height}\t\t|#{@value}|\t\t\n"
|
270
|
+
out_s += "\t#{@left}\t\t#{@right}\t\n"
|
271
|
+
out_s += "\t\t\t\t\t\n"
|
272
|
+
puts out_s
|
253
273
|
end
|
254
|
-
node
|
255
|
-
end
|
256
274
|
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
275
|
+
def print
|
276
|
+
out_s = "#{@value} - "
|
277
|
+
instance_variables.each do |i|
|
278
|
+
out_s += "(#{i}: #{instance_variable_get(i.to_sym)})"
|
279
|
+
end
|
280
|
+
puts out_s
|
261
281
|
end
|
262
|
-
node
|
263
|
-
end
|
264
282
|
|
265
|
-
|
266
|
-
|
267
|
-
out_s += "\t\t#{@parent}\t\t\n"
|
268
|
-
out_s += "#{@height}\t\t|#{@value}|\t\t\n"
|
269
|
-
out_s += "\t#{@left}\t\t#{@right}\t\n"
|
270
|
-
out_s += "\t\t\t\t\t\n"
|
271
|
-
puts out_s
|
272
|
-
end
|
273
|
-
|
274
|
-
def print
|
275
|
-
out_s = "#{@value} - "
|
276
|
-
instance_variables.each do |i|
|
277
|
-
out_s += "(#{i}: #{instance_variable_get(i.to_sym)})"
|
283
|
+
def to_s
|
284
|
+
@value.to_s
|
278
285
|
end
|
279
|
-
puts out_s
|
280
|
-
end
|
281
286
|
|
282
|
-
def to_s
|
283
|
-
@value.to_s
|
284
287
|
end
|
285
288
|
|
286
|
-
end
|
287
|
-
|
289
|
+
end
|