class SVGComponents
Constants
- DEFAULT_ARROW
- DEFAULT_CIRCLE
- DEFAULT_LABEL
- DEFAULT_LINE
def line(x1,x2,y1,y2,stroke=“black”,stroke_width=1)
"<line x1=\"#{x1.to_s}\" x2=\"#{x2.to_s}\" y1=\"#{y1.to_s}\" y2=\"#{y2.to_s}\" stroke=\"#{stroke}\" stroke-width=\"#{stroke_width.to_s}\"/>" end
- DEFAULT_RECTANGLE
- SVG_VERSION
Public Class Methods
circle(options={})
click to toggle source
# 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
header(width,height)
click to toggle source
# 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={})
click to toggle source
# 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={})
click to toggle source
Create left arrowhead with apex at xpos,ypos and specified base and height
# 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={})
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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={})
click to toggle source
# 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={})
click to toggle source
# 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")
click to toggle source
# 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