Class: GridGenerator::SquareOne::ElementFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/grid_generator/square_one/element_factory.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x:, y:, units:, shape:, offset:, colour:, opacity:) ⇒ ElementFactory

Returns a new instance of ElementFactory.



7
8
9
10
11
12
13
14
# File 'lib/grid_generator/square_one/element_factory.rb', line 7

def initialize(x: , y:, units:, shape:, offset:, colour:, opacity:)
  @x, @y = x, y
  @units = units
  @shape = shape
  @offset = offset
  @colour = colour
  @opacity = opacity
end

Instance Attribute Details

#colourObject (readonly)

Returns the value of attribute colour.



16
17
18
# File 'lib/grid_generator/square_one/element_factory.rb', line 16

def colour
  @colour
end

#offsetObject (readonly)

Returns the value of attribute offset.



16
17
18
# File 'lib/grid_generator/square_one/element_factory.rb', line 16

def offset
  @offset
end

#opacityObject (readonly)

Returns the value of attribute opacity.



16
17
18
# File 'lib/grid_generator/square_one/element_factory.rb', line 16

def opacity
  @opacity
end

#shapeObject (readonly)

Returns the value of attribute shape.



16
17
18
# File 'lib/grid_generator/square_one/element_factory.rb', line 16

def shape
  @shape
end

#unitsObject (readonly)

Returns the value of attribute units.



16
17
18
# File 'lib/grid_generator/square_one/element_factory.rb', line 16

def units
  @units
end

#xObject (readonly)

Returns the value of attribute x.



16
17
18
# File 'lib/grid_generator/square_one/element_factory.rb', line 16

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



16
17
18
# File 'lib/grid_generator/square_one/element_factory.rb', line 16

def y
  @y
end

Instance Method Details

#base_pointsObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/grid_generator/square_one/element_factory.rb', line 18

def base_points
  case shape
  when :edge
    [
      Matrix.column_vector([x+half_face_size-half_edge_width, y]),
      Matrix.column_vector([x+half_face_size+half_edge_width, y]),
      Matrix.column_vector([x+half_face_size, y+half_face_size])
    ]
  when :corner
    [
      Matrix.column_vector([x+half_face_size+half_edge_width, y]),
      Matrix.column_vector([x+face_size, y]),
      Matrix.column_vector([x+face_size, y+half_face_size-half_edge_width]),
      Matrix.column_vector([x+half_face_size, y+half_face_size])
    ] 
  when :middle
    [
      Matrix.column_vector([x+half_face_size+half_edge_width, y]),
      Matrix.column_vector([x+face_size, y]),
      Matrix.column_vector([x+face_size, y+face_size]),
      Matrix.column_vector([x+half_face_size-half_edge_width, y+face_size])
    ] 
  when :middle_flipped
    @base_points ||= [
      Matrix.column_vector([x+half_face_size-half_edge_width, y]),
      Matrix.column_vector([x+face_size, y]),
      Matrix.column_vector([x+face_size, y+face_size]),
      Matrix.column_vector([x+half_face_size+half_edge_width, y+face_size])
    ] 
  else
    raise ArgumentError, "unknown face shape: #{edge}"
  end
end

#buildObject



84
85
86
# File 'lib/grid_generator/square_one/element_factory.rb', line 84

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

#face_sizeObject



64
65
66
# File 'lib/grid_generator/square_one/element_factory.rb', line 64

def face_size
  @face_size ||= 3 * units
end

#half_edge_widthObject



56
57
58
# File 'lib/grid_generator/square_one/element_factory.rb', line 56

def half_edge_width 
  @half_edge_width ||= half_face_size * Math.tan(Math::PI/12) 
end

#half_face_sizeObject



60
61
62
# File 'lib/grid_generator/square_one/element_factory.rb', line 60

def half_face_size 
  @half_face_size ||= face_size / 2 
end

#offset_radiansObject



52
53
54
# File 'lib/grid_generator/square_one/element_factory.rb', line 52

def offset_radians
  offset*Math::PI/6
end

#pointsObject



80
81
82
# File 'lib/grid_generator/square_one/element_factory.rb', line 80

def points
  base_points.map { |p| (rotation_matrix * (p - rotation_point)) + rotation_point }
end

#rotation_matrixObject



72
73
74
75
76
77
# File 'lib/grid_generator/square_one/element_factory.rb', line 72

def rotation_matrix
  @rotation_matrix ||= Matrix[
    [Math.cos(offset_radians), -1*Math.sin(offset_radians)], 
    [Math.sin(offset_radians), Math.cos(offset_radians)]
  ]
end

#rotation_pointObject



68
69
70
# File 'lib/grid_generator/square_one/element_factory.rb', line 68

def rotation_point
  Matrix.column_vector([x+half_face_size, y+half_face_size])
end