Class: GridGenerator::SquareOne::Face
- Inherits:
-
Object
- Object
- GridGenerator::SquareOne::Face
- Defined in:
- lib/grid_generator/square_one/face.rb
Constant Summary collapse
- COLOURS =
{ fill: "#d0d0d0", stroke: "#404040" }
Instance Attribute Summary collapse
-
#axis_direction ⇒ Object
readonly
Returns the value of attribute axis_direction.
-
#elements ⇒ Object
readonly
Returns the value of attribute elements.
-
#units ⇒ Object
readonly
Returns the value of attribute units.
-
#x ⇒ Object
readonly
Returns the value of attribute x.
-
#y ⇒ Object
readonly
Returns the value of attribute y.
Instance Method Summary collapse
- #as_json ⇒ Object
- #axis ⇒ Object
- #back_axis ⇒ Object
- #element_shapes ⇒ Object
- #face_size ⇒ Object
- #forward_axis ⇒ Object
- #half_edge_width ⇒ Object
- #half_face_size ⇒ Object
-
#initialize(x:, y:, units:, elements:, axis_direction: :forward) ⇒ Face
constructor
A new instance of Face.
- #to_svg ⇒ Object
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_direction ⇒ Object (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 |
#elements ⇒ Object (readonly)
Returns the value of attribute elements.
27 28 29 |
# File 'lib/grid_generator/square_one/face.rb', line 27 def elements @elements end |
#units ⇒ Object (readonly)
Returns the value of attribute units.
27 28 29 |
# File 'lib/grid_generator/square_one/face.rb', line 27 def units @units end |
#x ⇒ Object (readonly)
Returns the value of attribute x.
27 28 29 |
# File 'lib/grid_generator/square_one/face.rb', line 27 def x @x end |
#y ⇒ Object (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_json ⇒ Object
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 |
#axis ⇒ Object
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_axis ⇒ Object
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_shapes ⇒ Object
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_size ⇒ Object
37 38 39 |
# File 'lib/grid_generator/square_one/face.rb', line 37 def face_size @face_size ||= 3 * units end |
#forward_axis ⇒ Object
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_width ⇒ Object
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_size ⇒ Object
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_svg ⇒ Object
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 |