Module: CosSinCalc::Triangle::Drawing::Svg

Included in:
CosSinCalc::Triangle::Drawing
Defined in:
lib/cossincalc/triangle/drawing/svg.rb

Constant Summary collapse

VERTEX_LABEL_MARGIN =

The distance between the middle of the vertex label and the vertex itself.

10
VERTEX_VALUE_MARGIN =

The distance between the middle of the vertex value and the vertex itself.

55
FONT_SIZE =

The font size of the labels.

12
ARC_RADIUS =

The radius of the circular arcs at the vertices.

25
NEXT_VARIABLE =

The association between a variable and the next.

{ :a => :b, :b => :c, :c => :a }

Instance Method Summary collapse

Instance Method Details

#save_png(filename) ⇒ Object

Saves a drawing of the triangle as an PNG file. The filename should be provided without the .png extension.



32
33
34
35
36
37
# File 'lib/cossincalc/triangle/drawing/svg.rb', line 32

def save_png(filename)
  save_svg(filename)
  Dir.cd_to(filename) do |basename|
    system("convert \"#{basename}.svg\" \"#{basename}.png\"") || system("rsvg-convert \"#{basename}.svg\" -o \"#{basename}.png\"")
  end
end

#save_svg(filename) ⇒ Object

Saves a drawing of the triangle as an SVG file. The filename should be provided without the .svg extension.



41
42
43
# File 'lib/cossincalc/triangle/drawing/svg.rb', line 41

def save_svg(filename)
  File.open("#{filename}.svg", 'w') { |f| f.write(to_svg) }
end

#to_svgObject

Returns a drawing of the triangle in SVG (Scalable Vector Graphics) format.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/cossincalc/triangle/drawing/svg.rb', line 12

def to_svg
  draw
  invert_coords
  
  polygon = @coords.values.map { |c| c.join(',') }.join(' ')
  labels  = ''
  
  t.each { |v| labels << vertex_label(v) << vertex_arc(v) << vertex_value(v) }
  t.each { |v| labels << edge_label(v) } # Needs to be drawn last in order to make ImageMagick render it correctly.
  
  <<-EOT
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="#{canvas_size}" height="#{canvas_size}">
<polygon fill="#f5eae5" stroke="#993300" stroke-width="1" points="#{polygon}"/>
#{labels}</svg>
EOT
end