Class: SVGComponents

Inherits:
Object
  • Object
show all
Defined in:
lib/teaching_printables/svg/svg_components.rb

Constant Summary collapse

SVG_VERSION =
"1.1"
DEFAULT_LINE =

“<line x1="#SVGComponents.x1x1.to_s" x2="#SVGComponents.x2x2.to_s" y1="#SVGComponents.y1y1.to_s" y2="#SVGComponents.y2y2.to_s" stroke="#stroke" stroke-width="#SVGComponents.stroke_widthstroke_width.to_s"/>”

end
{
  x1: 0,
  x2: 100,
  y1: 0,
  y2: 100,
  stroke: "black",
  stroke_width: 1,
  stroke_dasharray: "none"
}
DEFAULT_ARROW =
{
  xpos: 0,
  ypos: 0,
  base: 10,
  height: 10,
  fill: "black",
  stroke: "black",
  stroke_width: 1,
}
DEFAULT_CIRCLE =
{
  xpos: 0,
  ypos: 0,
  radius: 2,
  fill: "black",
  stroke: "black",
  stroke_width: 1,
}
DEFAULT_LABEL =
{
  xpos: 0,
  ypos: 0,
  text: "Label Text",
  font_size: 12,
  text_anchor: "middle",
  fill: "black"
}
DEFAULT_RECTANGLE =
{
  xpos: 0,
  ypos: 0,
  width: 100,
  height: 100,
  stroke: "black",
  fill: "transparent",
  stroke_width: 2
}

Class Method Summary collapse

Class Method Details

.circle(options = {}) ⇒ Object



60
61
62
63
# File 'lib/teaching_printables/svg/svg_components.rb', line 60

def circle(options={})
  options = DEFAULT_CIRCLE.merge(options)
  "<circle cx=\"#{options[:xpos].to_s}\" cy=\"#{options[:ypos].to_s}\" r=\"#{options[:radius].to_s}\" stroke=\"#{options[:stroke]}\" stroke-width=\"#{options[:stroke_width].to_s}\" fill=\"#{options[:fill]}\" />"
end


90
91
92
# File 'lib/teaching_printables/svg/svg_components.rb', line 90

def footer
  "</svg>"
end

.header(width, height) ⇒ Object



6
7
8
9
10
11
# File 'lib/teaching_printables/svg/svg_components.rb', line 6

def header(width,height)
  "<svg version=\"#{SVG_VERSION}\"
  baseProfile=\"full\"
  width=\"#{width.to_s}\" height=\"#{height.to_s}\"
  xmlns=\"http://www.w3.org/2000/svg\">".gsub("\n",'')
end

.label(options = {}) ⇒ Object



73
74
75
# File 'lib/teaching_printables/svg/svg_components.rb', line 73

def label(options={})
  "<text x=\"#{options[:xpos].to_s}\" y=\"#{options[:ypos].to_s}\" font-size=\"#{options[:font_size].to_s}\" text-anchor=\"#{options[:text_anchor]}\" fill=\"#{options[:fill]}\">#{options[:text]}</text>"
end

.left_arrowhead(options = {}) ⇒ Object

Create left arrowhead with apex at xpos,ypos and specified base and height



42
43
44
45
# File 'lib/teaching_printables/svg/svg_components.rb', line 42

def left_arrowhead(options={})
  options = DEFAULT_ARROW.merge(options)
  "<polygon points=\"#{options[:xpos]+options[:height]},#{options[:ypos]+options[:height]/2} #{options[:xpos]+options[:height]},#{options[:ypos]-options[:height]/2} #{options[:xpos]},#{options[:ypos]}\" style=\"fill:#{options[:fill]};stroke:#{options[:stroke]};stroke-width:#{options[:stroke_width].to_s}\"/>"
end

.line(options = {}) ⇒ Object



26
27
28
29
# File 'lib/teaching_printables/svg/svg_components.rb', line 26

def line(options={})
  options = DEFAULT_LINE.merge(options)
  "<line x1=\"#{options[:x1].to_s}\" x2=\"#{options[:x2].to_s}\" y1=\"#{options[:y1].to_s}\" y2=\"#{options[:y2].to_s}\" stroke=\"#{options[:stroke]}\" stroke-width=\"#{options[:stroke_width].to_s}\" stroke-dasharray=\"#{options[:stroke_dasharray]}\"/>"
end

.quad_ruled_grid(m_rows, n_columns) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
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
# File 'lib/teaching_printables/svg/svg_components.rb', line 94

def quad_ruled_grid(m_rows,n_columns)

  points_per_box = 40
  
  width = n_columns * points_per_box;
  height = m_rows * points_per_box;
  
  str = header(n_columns*points_per_box,m_rows*points_per_box)
  
  
  # make vertical lines
  for n in (0..n_columns)
    verticle_line_opts = {
      x1: n*points_per_box,
      x2: n*points_per_box,
      y1: 0,
      y2: height,
      stroke: "gray",
      stroke_width: 0.5,
      stroke_dasharray: "none"
    }
    str << line(verticle_line_opts)
  end
  
  # make horizontal lines
  for m in (0..m_rows)
    horizontal_line_opts = {
      x1: 0,
      x2: width,
      y1: m*points_per_box,
      y2: m*points_per_box,
      stroke: "gray",
      stroke_width: 0.5,
      stroke_dasharray: "none"
    }
    str << line(horizontal_line_opts)
  end
  
  str<<footer

  
end

.rectangle(options = {}) ⇒ Object



86
87
88
# File 'lib/teaching_printables/svg/svg_components.rb', line 86

def rectangle(options={})
  "<rect x=\"#{options[:xpos].to_s}\" y=\"#{options[:ypos].to_s}\" width=\"#{options[:width].to_s}\" height=\"#{options[:height].to_s}\" stroke=\"#{options[:stroke]}\" fill=\"#{options[:fill]}\" stroke-width=\"#{options[:stroke_width].to_s}\"/>"
end

.right_arrowhead(options = {}) ⇒ Object



47
48
49
50
# File 'lib/teaching_printables/svg/svg_components.rb', line 47

def right_arrowhead(options={})
  options = DEFAULT_ARROW.merge(options)
  "<polygon points=\"#{options[:xpos]-options[:height]},#{options[:ypos]+options[:height]/2} #{options[:xpos]-options[:height]},#{options[:ypos]-options[:height]/2} #{options[:xpos]},#{options[:ypos]}\" style=\"fill:#{options[:fill]};stroke:#{options[:stroke]};stroke-width:#{options[:stroke_width].to_s}\"/>"
end