Class: RailroadDiagrams::AlternatingSequence

Inherits:
DiagramMultiContainer show all
Defined in:
lib/railroad_diagrams/alternating_sequence.rb

Instance Attribute Summary

Attributes inherited from DiagramItem

#attrs, #children, #down, #height, #needs_space, #up, #width

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from DiagramMultiContainer

#to_str, #walk

Methods inherited from DiagramItem

#add, #to_str, #walk, #write_svg

Constructor Details

#initialize(*items) ⇒ AlternatingSequence

Returns a new instance of AlternatingSequence.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/railroad_diagrams/alternating_sequence.rb', line 11

def initialize(*items)
  super('g', items)
  @needs_space = false

  arc = AR
  vert = VS
  first, second = @items

  arc_x = 1 / Math.sqrt(2) * arc * 2
  arc_y = (1 - (1 / Math.sqrt(2))) * arc * 2
  cross_y = [arc, vert].max
  cross_x = (cross_y - arc_y) + arc_x

  first_out = [
    arc + arc, (cross_y / 2) + arc + arc, (cross_y / 2) + vert + first.down
  ].max
  @up = first_out + first.height + first.up

  second_in = [
    arc + arc, (cross_y / 2) + arc + arc, (cross_y / 2) + vert + second.up
  ].max
  @down = second_in + second.height + second.down

  @height = 0

  first_width = (first.needs_space ? 20 : 0) + first.width
  second_width = (second.needs_space ? 20 : 0) + second.width
  @width = (2 * arc) + [first_width, cross_x, second_width].max + (2 * arc)
end

Class Method Details

.new(*items) ⇒ Object



5
6
7
8
9
# File 'lib/railroad_diagrams/alternating_sequence.rb', line 5

def self.new(*items)
  raise "AlternatingSequence takes exactly two arguments, but got #{items.size} arguments." unless items.size == 2

  super
end

Instance Method Details

#format(x, y, width) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/railroad_diagrams/alternating_sequence.rb', line 46

def format(x, y, width)
  arc = AR
  gaps = determine_gaps(width, @width)
  Path.new(x, y).right(gaps[0]).add(self)
  x += gaps[0]
  Path.new(x + @width, y + @height).right(gaps[1]).add(self)
  # bounding box
  # Path(x+gaps[0], y).up(@up).right(@width).down(@up+@down).left(@width).up(@up).add(self)
  first, second = @items

  # top
  first_in = @up - first.up
  first_out = @up - first.up - first.height
  Path.new(x, y).arc('se').up(first_in - (2 * arc)).arc('wn').add(self)
  first.format(x + (2 * arc), y - first_in, @width - (4 * arc)).add(self)
  Path.new(x + @width - (2 * arc), y - first_out)
      .arc('ne').down(first_out - (2 * arc)).arc('ws').add(self)

  # bottom
  second_in = @down - second.down - second.height
  second_out = @down - second.down
  Path.new(x, y)
      .arc('ne')
      .down(second_in - (2 * arc))
      .arc('ws')
      .add(self)
  second.format(x + (2 * arc), y + second_in, @width - (4 * arc)).add(self)
  Path.new(x + @width - (2 * arc), y + second_out)
      .arc('se').up(second_out - (2 * arc)).arc('wn').add(self)

  # crossover
  arc_x = 1 / Math.sqrt(2) * arc * 2
  arc_y = (1 - (1 / Math.sqrt(2))) * arc * 2
  cross_y = [arc, VS].max
  cross_x = (cross_y - arc_y) + arc_x
  cross_bar = (@width - (4 * arc) - cross_x) / 2

  Path.new(x + arc, y - (cross_y / 2) - arc)
      .arc('ws')
      .right(cross_bar)
      .arc_8('n', 'cw')
      .l(cross_x - arc_x, cross_y - arc_y)
      .arc_8('sw', 'ccw')
      .right(cross_bar)
      .arc('ne')
      .add(self)

  Path.new(x + arc, y + (cross_y / 2) + arc)
      .arc('wn')
      .right(cross_bar)
      .arc_8('s', 'ccw')
      .l(cross_x - arc_x, -(cross_y - arc_y))
      .arc_8('nw', 'cw')
      .right(cross_bar)
      .arc('se')
      .add(self)

  self
end

#text_diagramObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/railroad_diagrams/alternating_sequence.rb', line 106

def text_diagram
  cross_diag, corner_bot_left, corner_bot_right, corner_top_left, corner_top_right,
  line, line_vertical, tee_left, tee_right = TextDiagram.get_parts(
    %w[
      cross_diag roundcorner_bot_left roundcorner_bot_right
      roundcorner_top_left roundcorner_top_right line
      line_vertical tee_left tee_right
    ]
  )

  first_td = @items[0].text_diagram
  second_td = @items[1].text_diagram
  max_width = TextDiagram.max_width(first_td, second_td)
  left_width, right_width = TextDiagram.gaps(max_width, 0)

  left_lines = []
  right_lines = []
  separator = []

  left_size, right_size = TextDiagram.gaps(first_td.width, 0)
  diagram_td = first_td.expand(left_width - left_size, right_width - right_size, 0, 0)

  left_lines += [' ' * 2] * diagram_td.entry
  left_lines << (corner_top_left + line)
  left_lines += ["#{line_vertical} "] * (diagram_td.height - diagram_td.entry - 1)
  left_lines << (corner_bot_left + line)

  right_lines += [' ' * 2] * diagram_td.entry
  right_lines << (line + corner_top_right)
  right_lines += [" #{line_vertical}"] * (diagram_td.height - diagram_td.entry - 1)
  right_lines << (line + corner_bot_right)

  separator << ("#{line * (left_width - 1)}#{corner_top_right} #{corner_top_left}#{line * (right_width - 2)}")
  separator << ("#{' ' * (left_width - 1)} #{cross_diag} #{' ' * (right_width - 2)}")
  separator << ("#{line * (left_width - 1)}#{corner_bot_right} #{corner_bot_left}#{line * (right_width - 2)}")

  left_lines << (' ' * 2)
  right_lines << (' ' * 2)

  left_size, right_size = TextDiagram.gaps(second_td.width, 0)
  second_td = second_td.expand(left_width - left_size, right_width - right_size, 0, 0)
  diagram_td = diagram_td.append_below(second_td, separator, move_entry: true, move_exit: true)

  left_lines << (corner_top_left + line)
  left_lines += ["#{line_vertical} "] * second_td.entry
  left_lines << (corner_bot_left + line)

  right_lines << (line + corner_top_right)
  right_lines += [" #{line_vertical}"] * second_td.entry
  right_lines << (line + corner_bot_right)

  mid_point = first_td.height + (separator.size / 2)
  diagram_td = diagram_td.alter(new_entry: mid_point, new_exit: mid_point)

  left_td = TextDiagram.new(mid_point, mid_point, left_lines)
  right_td = TextDiagram.new(mid_point, mid_point, right_lines)

  diagram_td = left_td.append_right(diagram_td, '').append_right(right_td, '')
  TextDiagram.new(1, 1, [corner_top_left, tee_left, corner_bot_left])
             .append_right(diagram_td, '')
             .append_right(TextDiagram.new(1, 1, [corner_top_right, tee_right, corner_bot_right]), '')
end

#to_sObject



41
42
43
44
# File 'lib/railroad_diagrams/alternating_sequence.rb', line 41

def to_s
  items = @items.map(&:to_s).join(', ')
  "AlternatingSequence(#{items})"
end