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


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

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
76
# File 'lib/teaching_printables/svg/svg_components.rb', line 73

def label(options={})
  options = DEFAULT_LABEL.merge(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

.number_line(startpt, endpt) ⇒ Object



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
# File 'lib/teaching_printables/svg/svg_components.rb', line 95

def number_line(startpt,endpt)
  points_per_unit = 40
  width = (startpt-endpt+1)*points_per_unit
  height = 2*points_per_unit
  str = header(width,height)
  horizontal_line_opts = {
    x1: 0,
    x2: width,
    y1: points_per_unit,
    y2: points_per_unit,
    stroke: "gray",
    stroke_width: 0.5,
    stroke_dasharray: "none"
  }
  str << line(horizontal_line_opts)
  
  # make vertical lines
  for n in (startpt..endpt)
    verticle_line_opts = {
      x1: n*points_per_unit,
      x2: n*points_per_unit,
      y1: points_per_unit,
      y2: height,
      stroke: "gray",
      stroke_width: 0.5,
      stroke_dasharray: "none"
    }
    str << line(verticle_line_opts)
    str << label(xpos: n*points_per_unit, ypos: 0, text: "#{n}")
  end
  str<<footer
  
end

.quad_ruled_grid(m_rows, n_columns) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/teaching_printables/svg/svg_components.rb', line 150

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



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

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

.tens_and_ones_shape_svg(tens, ones, fill_color = "red") ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/teaching_printables/svg/svg_components.rb', line 129

def tens_and_ones_shape_svg(tens,ones,fill_color="red")
  points_per_box = 40
  
  ncols = 10
  nrows = tens
  
  width = ncols*points_per_box
  height = (nrows+1)*points_per_box
  str = header(width,height)
  
  polygon_points =  "\"0 0, #{width} 0, " +
                    "#{width} #{height-points_per_box}, " +
                    "#{ones*points_per_box} #{height-points_per_box}, " +
                    "#{ones*points_per_box} #{height}, " +
                    "0 #{height}, " +
                    "0 0\""
  str<<"<polygon points=#{polygon_points} fill=\"#{fill_color}\" stroke=\"#000\"/>"
  
  str<<footer
end