Class: EightCorner::SvgPrinter
- Inherits:
-
Object
- Object
- EightCorner::SvgPrinter
- Defined in:
- lib/eight_corner/svg_printer.rb
Overview
print a figure or collection of figures as an svg document
Instance Method Summary collapse
- #draw(figure, options = {}) ⇒ Object
- #incremental_colors(points, options = {}) ⇒ Object
-
#initialize(options = {}) ⇒ SvgPrinter
constructor
A new instance of SvgPrinter.
- #line(from, to, color) ⇒ Object
- #point(x, y, r, color) ⇒ Object
- #print(points) ⇒ Object
- #solid(points) ⇒ Object
- #svg(width, height) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ SvgPrinter
Returns a new instance of SvgPrinter.
6 7 8 9 10 |
# File 'lib/eight_corner/svg_printer.rb', line 6 def initialize(={}) [:incremental_colors] ||= false @options = end |
Instance Method Details
#draw(figure, options = {}) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/eight_corner/svg_printer.rb', line 26 def draw(figure, ={}) defaults = { x_offset: 0, y_offset: 0, width: 200, height: 200, show_border: false, mark_initial_point: false, label: nil, method: :solid } Base.(, defaults) = defaults.merge() raise ArgumentError, "invalid :method" if ! respond_to?([:method]) points = figure.points out = "<g transform='translate(#{[:x_offset]}, #{[:y_offset]})'>" if [:show_border] out += "<rect width='#{[:width]}' height='#{[:height]}' style='stroke:black; stroke-width:1; fill:none'></rect>" end out += send([:method], points) if [:mark_initial_point] out += point(points[0].x, points[0].y, 5, '#ff0000') end if [:label] out += "<text x='5' y='#{[:height]-5}' style='font-family: sans-serif'>#{[:label]}</text>" end out += "</g>\n" out end |
#incremental_colors(points, options = {}) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/eight_corner/svg_printer.rb', line 66 def incremental_colors(points, ={}) 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) ⇒ Object
80 81 82 |
# 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) ⇒ Object
84 85 86 |
# 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) ⇒ Object
20 21 22 23 24 |
# 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) ⇒ Object
59 60 61 62 63 64 |
# 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) ⇒ Object
12 13 14 15 16 17 18 |
# 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 |