-
Notifications
You must be signed in to change notification settings - Fork 1
/
path.rb
93 lines (81 loc) · 1.98 KB
/
path.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
class Path
class Node
attr_reader :x, :y, :op
attr_accessor :prev, :next
def initialize(x, y, op)
@x = x.to_f
@y = y.to_f
@op = op.to_sym
end
def connect(next_node)
self.next = next_node
next_node.prev = self
self
end
def reconnect(opp_node)
# assume that self.x == opp_node.x && self.y == opp_node.y
if (self.next.x == opp_node.prev.x && self.next.y == opp_node.prev.y)
opp_node.prev.connect(self.next)
self.connect(opp_node)
self
else
nil
end
end
def output(op = @op)
@op = nil
"%g %g %s " % [@x, @y, op]
end
end
def initialize(nodes = [])
@nodes = nodes
end
def add(buf_node)
(buf_node << buf_node[0]).each_cons(2) { |a,b| @nodes << a.connect(b) } if (buf_node.length >= 2)
end
def optimize
return self unless @nodes
# reconnect nodes (total number of nodes is not changed)
@nodes.group_by(&:y).each do |y,nodes|
nodes.group_by(&:x).each do |x,buf|
while (n1 = buf.pop)
buf.each { |n2| break if n1.reconnect(n2) }
end
end
end
# remove needless nodes
@nodes.reject! do |node|
d1x = node.prev.x - node.x
d1y = node.prev.y - node.y
d2x = node.next.x - node.x
d2y = node.next.y - node.y
if (d1x * d2y == d2x * d1y)
node.prev.connect(node.next)
true
else
false
end
end
self
end
def output_fill(buf = '')
return buf if (@nodes.length < 2)
buf << "\n" unless (buf[-1] == "\n")
nodes = @nodes.dup
while (node0 = node = nodes.shift)
next unless node.op
buf << node.output(:m)
until ((node = node.next) == node0)
buf << node.output(:l)
end
buf << "h\n"
end
buf << "B\n"
end
def output_stroke(buf = '', close = false)
return buf if (@nodes.length < 1)
@nodes.each { |node| buf << node.output }
buf << "h " if close
buf << "S\n"
end
end