Class: Draught::PathCleaner

Inherits:
Object
  • Object
show all
Defined in:
lib/draught/path_cleaner.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_points) ⇒ PathCleaner

Returns a new instance of PathCleaner.



15
16
17
# File 'lib/draught/path_cleaner.rb', line 15

def initialize(input_points)
  @input_points = input_points
end

Instance Attribute Details

#input_pointsObject (readonly)

Returns the value of attribute input_points.



13
14
15
# File 'lib/draught/path_cleaner.rb', line 13

def input_points
  @input_points
end

Class Method Details

.dedupe(path) ⇒ Object



5
6
7
# File 'lib/draught/path_cleaner.rb', line 5

def self.dedupe(path)
  Path.new(new(path.points).dedupe)
end

.simplify(path) ⇒ Object



9
10
11
# File 'lib/draught/path_cleaner.rb', line 9

def self.simplify(path)
  Path.new(new(path.points).simplify)
end

Instance Method Details

#dedupeObject



19
20
21
22
23
24
25
26
# File 'lib/draught/path_cleaner.rb', line 19

def dedupe
  output_points = [input_points.first]
  input_points.inject do |previous_point, point|
    output_points << point if point != previous_point
    point
  end
  output_points
end

#simplifyObject



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/draught/path_cleaner.rb', line 28

def simplify
  points = dedupe
  pos = 0
  while pos < (points.length - 2)
    triple = points[pos, 3]
    if intercepts?(*triple)
      points.delete_at(pos + 1)
    else
      pos += 1
    end
  end
  points
end