class EightCorner::SvgPrinter
print a figure or collection of figures as an svg document
Public Class Methods
new(options={})
click to toggle source
# File lib/eight_corner/svg_printer.rb, line 6 def initialize(options={}) options[:incremental_colors] ||= false @options = options end
Public Instance Methods
draw(figure, options={})
click to toggle source
# File lib/eight_corner/svg_printer.rb, line 26 def draw(figure, options={}) defaults = { x_offset: 0, y_offset: 0, width: 200, height: 200, show_border: false, mark_initial_point: false, label: nil, method: :solid } Base.validate_options!(options, defaults) options = defaults.merge(options) raise ArgumentError, "invalid :method" if ! respond_to?(options[:method]) points = figure.points out = "<g transform='translate(#{options[:x_offset]}, #{options[:y_offset]})'>" if options[:show_border] out += "<rect width='#{options[:width]}' height='#{options[:height]}' style='stroke:black; stroke-width:1; fill:none'></rect>" end out += send(options[:method], points) if options[:mark_initial_point] out += point(points[0].x, points[0].y, 5, '#ff0000') end if options[:label] out += "<text x='5' y='#{options[:height]-5}' style='font-family: sans-serif'>#{options[:label]}</text>" end out += "</g>\n" out end
incremental_colors(points, options={})
click to toggle source
# File lib/eight_corner/svg_printer.rb, line 66 def incremental_colors(points, options={}) out = '' interp = Interpolate::Points.new(1 => 0, (points.size-1) => 12) 1.upto(points.size-1) do |i| prev = points[i-1] curr = points[i] hex_str = interp.at(i).to_i.to_s(16) * 6 out += line(prev, curr, hex_str) end out += line(points.last, points.first, interp.at(points.size-1).to_i.to_s(16) * 6) out end
line(from, to, color)
click to toggle source
# File lib/eight_corner/svg_printer.rb, line 80 def line(from, to, color) "<line x1='#{from.x}' y1='#{from.y}' x2='#{to.x}' y2='#{to.y}' style='stroke:##{color}; stroke-width:4'/>\n" end
point(x, y, r, color)
click to toggle source
# File lib/eight_corner/svg_printer.rb, line 84 def point(x, y, r, color) "<circle cx='#{x}' cy='#{y}' r='#{r}' fill='#{color}' stroke='none' />" end
print(points)
click to toggle source
# File lib/eight_corner/svg_printer.rb, line 20 def print(points) svg do @options[:incremental_colors] ? incremental_colors(points) : solid(points) end end
solid(points)
click to toggle source
# File lib/eight_corner/svg_printer.rb, line 59 def solid(points) out = '<polygon points="' out += points.map{|p| "#{p.x},#{p.y}"}.join(' ') out += '" style="fill:none; stroke:black; stroke-width:4"/>' out end
svg(width, height) { |self| ... }
click to toggle source
# File lib/eight_corner/svg_printer.rb, line 12 def svg(width, height) out = "<svg xmlns='http://www.w3.org/2000/svg' version='1.1' width='#{width}' height='#{height}'>\n" out += yield(self) out += '</svg>' out end