class Draught::PathCleaner

Attributes

input_points[R]

Public Class Methods

dedupe(path) click to toggle source
# File lib/draught/path_cleaner.rb, line 5
def self.dedupe(path)
  Path.new(new(path.points).dedupe)
end
new(input_points) click to toggle source
# File lib/draught/path_cleaner.rb, line 15
def initialize(input_points)
  @input_points = input_points
end
simplify(path) click to toggle source
# File lib/draught/path_cleaner.rb, line 9
def self.simplify(path)
  Path.new(new(path.points).simplify)
end

Public Instance Methods

dedupe() click to toggle source
# 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
simplify() click to toggle source
# 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

Private Instance Methods

axis_aligned?(points, axis) click to toggle source
# File lib/draught/path_cleaner.rb, line 67
def axis_aligned?(points, axis)
  points.map(&axis).uniq.length == 1
end
intercepts?(previous_point, point, next_point) click to toggle source
# File lib/draught/path_cleaner.rb, line 44
def intercepts?(previous_point, point, next_point)
  intercepts_horizontal?(previous_point, point, next_point) ||
    intercepts_vertical?(previous_point, point, next_point)
end
intercepts_horizontal?(previous_point, point, next_point) click to toggle source
# File lib/draught/path_cleaner.rb, line 49
def intercepts_horizontal?(previous_point, point, next_point)
  points = [previous_point, point, next_point]
  intercepts_line?(points, :x)
end
intercepts_line?(points, axis) click to toggle source
# File lib/draught/path_cleaner.rb, line 59
def intercepts_line?(points, axis)
  axis_aligned?(points, perpendicular_axis(axis)) && obviously_intermediate?(points, axis)
end
intercepts_vertical?(previous_point, point, next_point) click to toggle source
# File lib/draught/path_cleaner.rb, line 54
def intercepts_vertical?(previous_point, point, next_point)
  points = [previous_point, point, next_point]
  intercepts_line?(points, :y)
end
obviously_intermediate?(points, axis) click to toggle source
# File lib/draught/path_cleaner.rb, line 71
def obviously_intermediate?(points, axis)
  p1, p2, p3 = points.map(&axis)
  operator = p1 < p3 ? :< : :>
  p1.send(operator, p2) && p2.send(operator, p3)
end
perpendicular_axis(axis) click to toggle source
# File lib/draught/path_cleaner.rb, line 63
def perpendicular_axis(axis)
  {:x => :y, :y => :x}.fetch(axis)
end