Class: Laser::Cutter::Aggregator

Inherits:
Object
  • Object
show all
Defined in:
lib/laser-cutter/aggregator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(array_of_lines = []) ⇒ Aggregator

Returns a new instance of Aggregator.



6
7
8
# File 'lib/laser-cutter/aggregator.rb', line 6

def initialize(array_of_lines = [])
  self.lines = array_of_lines.sort
end

Instance Attribute Details

#linesObject

Returns the value of attribute lines.



4
5
6
# File 'lib/laser-cutter/aggregator.rb', line 4

def lines
  @lines
end

Instance Method Details

#dedup!Object

This method finds lines that are identical (same p1/p2)



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/laser-cutter/aggregator.rb', line 11

def dedup!
  lines_to_delete = []
  count = lines.size
  for i in 0..(count - 1) do
    for j in (i + 1)..(count - 1) do
      l1 = lines[i]
      l2 = lines[j]
      if l1.eql?(l2)
        lines_to_delete << l1
        lines_to_delete << l2
      end
    end
  end
  self.lines = self.lines - lines_to_delete
  self
end

#deoverlap!Object

Find overlapping (intersecting) sections of lines and remove them.



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
# File 'lib/laser-cutter/aggregator.rb', line 30

def deoverlap!
  lines_to_delete = []
  lines_to_add = []
  count = lines.size
  for i in 0..(count - 1) do
    for j in (i + 1)..(count - 1) do
      l1 = lines[i]
      l2 = lines[j]
      if l1.overlaps?(l2)
        lines_to_delete << l1
        lines_to_delete << l2
        lines_to_add << l1.xor(l2)
      end
    end
  end

  lines_to_delete.uniq!
  lines_to_delete.flatten!
  lines_to_add.uniq!
  lines_to_add.flatten!

  self.lines = (self.lines - lines_to_delete + lines_to_add).flatten
  self.lines.sort!
  self
end