Class: RailroadDiagrams::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/railroad_diagrams/path.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y) ⇒ Path

Returns a new instance of Path.



7
8
9
10
11
# File 'lib/railroad_diagrams/path.rb', line 7

def initialize(x, y)
  @x = x
  @y = y
  @attrs = { 'd' => "M#{x} #{y}" }
end

Instance Attribute Details

#attrsObject (readonly)

Returns the value of attribute attrs.



5
6
7
# File 'lib/railroad_diagrams/path.rb', line 5

def attrs
  @attrs
end

#xObject (readonly)

Returns the value of attribute x.



5
6
7
# File 'lib/railroad_diagrams/path.rb', line 5

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



5
6
7
# File 'lib/railroad_diagrams/path.rb', line 5

def y
  @y
end

Instance Method Details

#add(parent) ⇒ Object



91
92
93
94
# File 'lib/railroad_diagrams/path.rb', line 91

def add(parent)
  parent.children << self
  self
end

#arc(sweep) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/railroad_diagrams/path.rb', line 81

def arc(sweep)
  x = AR
  y = AR
  x *= -1 if sweep[0] == 'e' || sweep[1] == 'w'
  y *= -1 if sweep[0] == 's' || sweep[1] == 'n'
  cw = %w[ne es sw wn].include?(sweep) ? 1 : 0
  @attrs['d'] += "a#{AR} #{AR} 0 0 #{cw} #{x} #{y}"
  self
end

#arc_8(start, dir) ⇒ Object



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
# File 'lib/railroad_diagrams/path.rb', line 49

def arc_8(start, dir)
  arc = AR
  s2 = 1 / Math.sqrt(2) * arc
  s2inv = arc - s2
  sweep = dir == 'cw' ? '1' : '0'
  path = "a #{arc} #{arc} 0 0 #{sweep} "

  sd = start + dir
  offset = case sd
           when 'ncw' then [s2, s2inv]
           when 'necw' then [s2inv, s2]
           when 'ecw' then [-s2inv, s2]
           when 'secw' then [-s2, s2inv]
           when 'scw' then [-s2, -s2inv]
           when 'swcw' then [-s2inv, -s2]
           when 'wcw' then [s2inv, -s2]
           when 'nwcw' then [s2, -s2inv]
           when 'nccw' then [-s2, s2inv]
           when 'nwccw' then [-s2inv, s2]
           when 'wccw' then [s2inv, s2]
           when 'swccw' then [s2, s2inv]
           when 'sccw' then [s2, -s2inv]
           when 'seccw' then [s2inv, -s2]
           when 'eccw' then [-s2inv, -s2]
           when 'neccw' then [-s2, -s2inv]
           end

  path += offset.map(&:to_s).join(' ')
  @attrs['d'] += path
  self
end

#down(val) ⇒ Object



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

def down(val)
  v([0, val].max)
end

#formatObject



104
105
106
107
# File 'lib/railroad_diagrams/path.rb', line 104

def format
  @attrs['d'] += 'h.5'
  self
end

#h(val) ⇒ Object



23
24
25
26
# File 'lib/railroad_diagrams/path.rb', line 23

def h(val)
  @attrs['d'] += "h#{val}"
  self
end

#l(x, y) ⇒ Object



18
19
20
21
# File 'lib/railroad_diagrams/path.rb', line 18

def l(x, y)
  @attrs['d'] += "l#{x} #{y}"
  self
end

#left(val) ⇒ Object



32
33
34
# File 'lib/railroad_diagrams/path.rb', line 32

def left(val)
  h(-[0, val].max)
end

#m(x, y) ⇒ Object



13
14
15
16
# File 'lib/railroad_diagrams/path.rb', line 13

def m(x, y)
  @attrs['d'] += "m#{x} #{y}"
  self
end

#right(val) ⇒ Object



28
29
30
# File 'lib/railroad_diagrams/path.rb', line 28

def right(val)
  h([0, val].max)
end

#text_diagramObject



109
110
111
# File 'lib/railroad_diagrams/path.rb', line 109

def text_diagram
  TextDiagram.new(0, 0, [])
end

#to_sObject



113
114
115
# File 'lib/railroad_diagrams/path.rb', line 113

def to_s
  "Path(#{@x.inspect}, #{@y.inspect})"
end

#up(val) ⇒ Object



45
46
47
# File 'lib/railroad_diagrams/path.rb', line 45

def up(val)
  v(-[0, val].max)
end

#v(val) ⇒ Object



36
37
38
39
# File 'lib/railroad_diagrams/path.rb', line 36

def v(val)
  @attrs['d'] += "v#{val}"
  self
end

#write_svg(write) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/railroad_diagrams/path.rb', line 96

def write_svg(write)
  write.call('<path')
  @attrs.sort.each do |name, value|
    write.call(" #{name}=\"#{RailroadDiagrams.escape_attr(value)}\"")
  end
  write.call(' />')
end