Class: EightCorner::SvgPrinter

Inherits:
Object
  • Object
show all
Defined in:
lib/eight_corner/svg_printer.rb

Overview

print a figure or collection of figures as an svg document

Instance Method Summary collapse

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(options={})
  options[:incremental_colors] ||= false

  @options = options
end

Instance Method Details

#draw(figure, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


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, 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 = {}) ⇒ 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, 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) ⇒ 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


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