Class: DPTM6::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/dptm6/path.rb

Defined Under Namespace

Classes: Node

Instance Method Summary collapse

Constructor Details

#initialize(nodes = []) ⇒ Path

Returns a new instance of Path.



35
36
37
# File 'lib/dptm6/path.rb', line 35

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

Instance Method Details

#add(buf_node) ⇒ Object



39
40
41
# File 'lib/dptm6/path.rb', line 39

def add(buf_node)
  (buf_node << buf_node[0]).each_cons(2) { |a,b| @nodes << a.connect(b) } if (buf_node.length >= 2)
end

#optimizeObject



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
# File 'lib/dptm6/path.rb', line 43

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

#output_fill(buf = '') ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/dptm6/path.rb', line 72

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

#output_stroke(buf = '', close = false) ⇒ Object



87
88
89
90
91
92
# File 'lib/dptm6/path.rb', line 87

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