Class: GridGenerator::SquareOne::Face

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

Constant Summary collapse

COLOURS =
{
  fill: "#d0d0d0",
  stroke: "#404040"
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x:, y:, units:, elements:, axis_direction: :forward) ⇒ Face

Returns a new instance of Face.



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/grid_generator/square_one/face.rb', line 13

def initialize(x:, y: , units: , elements:, axis_direction: :forward)
  @x, @y = x, y
  @units = units
  @elements = case elements 
  when String
    SquareOneFaceParser.new(elements).to_a
  when Array
    elements 
  else
    raise ArgumentError, "squares must be array or string" 
  end
  @axis_direction = axis_direction
end

Instance Attribute Details

#axis_directionObject (readonly)

Returns the value of attribute axis_direction.



27
28
29
# File 'lib/grid_generator/square_one/face.rb', line 27

def axis_direction
  @axis_direction
end

#elementsObject (readonly)

Returns the value of attribute elements.



27
28
29
# File 'lib/grid_generator/square_one/face.rb', line 27

def elements
  @elements
end

#unitsObject (readonly)

Returns the value of attribute units.



27
28
29
# File 'lib/grid_generator/square_one/face.rb', line 27

def units
  @units
end

#xObject (readonly)

Returns the value of attribute x.



27
28
29
# File 'lib/grid_generator/square_one/face.rb', line 27

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



27
28
29
# File 'lib/grid_generator/square_one/face.rb', line 27

def y
  @y
end

Instance Method Details

#as_jsonObject



91
92
93
94
95
# File 'lib/grid_generator/square_one/face.rb', line 91

def as_json
  {
    "element_shapes" => element_shapes.map(&:as_json)
  }
end

#axisObject



41
42
43
44
45
46
47
# File 'lib/grid_generator/square_one/face.rb', line 41

def axis
  if axis_direction == :back
    back_axis
  else
    forward_axis
  end
end

#back_axisObject



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/grid_generator/square_one/face.rb', line 63

def back_axis
  a = Matrix.column_vector([
    x+half_face_size-half_edge_width,
    y,
  ])

  b = Matrix.column_vector([
    x+half_face_size+half_edge_width,
    y+face_size
  ])

  GridGenerator::BaseLine.new(a: a, b: b) 
end

#element_shapesObject



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/grid_generator/square_one/face.rb', line 77

def element_shapes
  elements.map do |element|
    GridGenerator::SquareOne::ElementFactory.new(
      x: x,
      y: y,
      units: units,
      shape: element[:shape],
      offset: element[:offset],
      colour: element[:colour],
      opacity: element[:opacity]
    ).build
  end
end

#face_sizeObject



37
38
39
# File 'lib/grid_generator/square_one/face.rb', line 37

def face_size
  @face_size ||= 3 * units
end

#forward_axisObject



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/grid_generator/square_one/face.rb', line 49

def forward_axis
  a = Matrix.column_vector([
    x+half_face_size+half_edge_width,
    y,
  ])

  b = Matrix.column_vector([
    x+half_face_size-half_edge_width,
    y+face_size
  ])

  GridGenerator::BaseLine.new(a: a, b: b) 
end

#half_edge_widthObject



29
30
31
# File 'lib/grid_generator/square_one/face.rb', line 29

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

#half_face_sizeObject



33
34
35
# File 'lib/grid_generator/square_one/face.rb', line 33

def half_face_size 
  @half_face_size ||= face_size / 2 
end

#to_svgObject



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/grid_generator/square_one/face.rb', line 97

def to_svg
  output = ""
  element_shapes.each do |element|
    if element.opacity == 0.4
      output += "<polygon points=\"#{ element.points_string }\" style=\"fill:#{ COLOURS[:fill] };stroke:#{ COLOURS[:stroke] };stroke-width:1;opacity:1;\" />"
    end
    output += "<polygon points=\"#{ element.points_string }\" style=\"fill:#{ element.colour };stroke:#{ COLOURS[:stroke] };stroke-width:1;opacity:#{ element.opacity };\" />"
   end

  output += "<line x1=\"#{ axis.x1 }\" y1=\"#{ axis.y1 }\" x2=\"#{ axis.x2 }\" y2=\"#{ axis.y2 }\" style=\"stroke:#{ COLOURS[:stroke] };stroke-width:5\" />"

  output
end