Class: Engine::Path

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(points) ⇒ Path

Returns a new instance of Path.



9
10
11
12
13
14
15
16
17
# File 'lib/engine/path.rb', line 9

def initialize(points)
  optimised_points = remove_colinear(points)
  if optimised_points.length <= 3
    @points = points
  else
    @points = optimised_points
  end
  #@points = points
end

Instance Attribute Details

#pointsObject (readonly)

Returns the value of attribute points.



7
8
9
# File 'lib/engine/path.rb', line 7

def points
  @points
end

Instance Method Details

#find_earObject

Raises:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/engine/path.rb', line 27

def find_ear
  triangles = points.map.with_index do |point, i|
    a = points[i - 1]
    b = point
    c = points[(i + 1) % points.length]
    [a, b, c]
  end

  triangles.each do |triangle|
    if is_ear?(triangle)
      new_points = points.reject { |point| point == triangle[1] }
      return triangle, Path.new(new_points)
    end
  end
  raise NoEarsException
end

#is_ear?(triangle) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
# File 'lib/engine/path.rb', line 44

def is_ear?(triangle)
  return false unless clockwise?(triangle)
  points.each do |point|
    next if triangle.include?(point)
    return false if point_in_triangle?(point, triangle)
  end
  true
end

#lengthObject



23
24
25
# File 'lib/engine/path.rb', line 23

def length
  points.length
end

#point_string(p) ⇒ Object



19
20
21
# File 'lib/engine/path.rb', line 19

def point_string(p)
  "#{20 * (p[0] + 3)}, #{50 * (p[1] + 2)}"
end