Class: GridGenerator::Megaminx::FaceElementFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/grid_generator/megaminx/face_element_factory.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x:, y:, index:, face_points:, face_lines:, face:, rotator:) ⇒ FaceElementFactory

Returns a new instance of FaceElementFactory.



6
7
8
9
10
11
12
13
14
15
# File 'lib/grid_generator/megaminx/face_element_factory.rb', line 6

def initialize(x:, y:, index:, face_points: , face_lines:, face:, rotator: )
  @x, @y = x, y
  @index = index
  @face_points = face_points
  @face_lines = face_lines
  face_attr = FaceParser.new(face).parse
  @colour = face_attr && face_attr[:colour]
  @opacity = face_attr && face_attr[:opacity]
  @rotator = rotator 
end

Instance Attribute Details

#colourObject (readonly)

Returns the value of attribute colour.



17
18
19
# File 'lib/grid_generator/megaminx/face_element_factory.rb', line 17

def colour
  @colour
end

#face_linesObject (readonly)

Returns the value of attribute face_lines.



17
18
19
# File 'lib/grid_generator/megaminx/face_element_factory.rb', line 17

def face_lines
  @face_lines
end

#face_pointsObject (readonly)

Returns the value of attribute face_points.



17
18
19
# File 'lib/grid_generator/megaminx/face_element_factory.rb', line 17

def face_points
  @face_points
end

#indexObject (readonly)

Returns the value of attribute index.



17
18
19
# File 'lib/grid_generator/megaminx/face_element_factory.rb', line 17

def index
  @index
end

#opacityObject (readonly)

Returns the value of attribute opacity.



17
18
19
# File 'lib/grid_generator/megaminx/face_element_factory.rb', line 17

def opacity
  @opacity
end

#rotatorObject (readonly)

Returns the value of attribute rotator.



17
18
19
# File 'lib/grid_generator/megaminx/face_element_factory.rb', line 17

def rotator
  @rotator
end

#xObject (readonly)

Returns the value of attribute x.



17
18
19
# File 'lib/grid_generator/megaminx/face_element_factory.rb', line 17

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



17
18
19
# File 'lib/grid_generator/megaminx/face_element_factory.rb', line 17

def y
  @y
end

Instance Method Details

#buildObject



88
89
90
# File 'lib/grid_generator/megaminx/face_element_factory.rb', line 88

def build
  GridGenerator::BaseElement.new(points: points, colour: colour, opacity: opacity)
end

#center_pointsObject

front face lines - parallel 0 - top right 1 - right 2 - down 3 - left 4 - top left right



37
38
39
40
41
42
43
# File 'lib/grid_generator/megaminx/face_element_factory.rb', line 37

def center_points
  face_lines.each_with_index.map do |line, i|
    a = line
    b = face_lines[(i+1)%5]
    GridGenerator::Helper.intersection(a, b) 
  end
end

#corner_points(i) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/grid_generator/megaminx/face_element_factory.rb', line 61

def corner_points(i)
  line_a = face_lines[(i-1) % 5]
  line_b = face_lines[(i) % 5]
  intersection = GridGenerator::Helper.intersection(line_a, line_b)

  [
    face_points[i], 
    line_a.b,
    intersection,
    line_b.a
  ]
end

#edge_points(i) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/grid_generator/megaminx/face_element_factory.rb', line 45

def edge_points(i)
  previous_line = face_lines[(i-1)%5]
  this_line = face_lines[i%5]
  next_line = face_lines[(i+1)%5]

  previous_this_point = GridGenerator::Helper.intersection(previous_line, this_line)
  this_next_point = GridGenerator::Helper.intersection(this_line, next_line)

  [
    previous_line.b,
    next_line.a,
    this_next_point,
    previous_this_point 
  ]
end

#offsetObject



19
20
21
# File 'lib/grid_generator/megaminx/face_element_factory.rb', line 19

def offset
  Matrix.column_vector([x, y])
end

#pointsObject



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/grid_generator/megaminx/face_element_factory.rb', line 74

def points
  all_points = if index == 0
    center_points
  else
    if index % 2 == 0
      edge_points((index / 2) - 1)
    else
      corner_points((index - 1) / 2)
    end
  end

  all_points.map { |p| rotator.rotate(p) + offset }
end