Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 16th, 2012  |  syntax: None  |  size: 1.93 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. class SVG
  2.  
  3.   def initialize
  4.     @text = <<-EOD
  5. <?xml version="1.0" standalone="no"?>
  6.  
  7. <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
  8. "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
  9.  
  10. <svg width="100%" height="100%" version="1.1"
  11. xmlns="http://www.w3.org/2000/svg">
  12.  
  13. <g transform="translate(600, 320)">
  14.     EOD
  15.     @circles = ""
  16.   end
  17.  
  18.   def circle(cx, cy, r, stroke, fill)
  19.     @circles << %{<circle cx="#{cx}" cy="#{cy}" r="#{r}" stroke="#{stroke}" fill="#{fill}" stroke-width="0.5" />\n}
  20.   end
  21.  
  22.   def rect(x, y, w, h, transform = nil)
  23.     @text << %{<rect x="#{x}" y="#{y}" width="#{w}" height="#{h}"#{transform ? " transform=\"#{transform}\"" : ''} fill="none" stroke="black" />\n}
  24.   end
  25.  
  26.   def triangle(cx, cy, a, dir)
  27.     case dir
  28.     when :up:
  29.       transform = "rotate(0 #{cx} #{cy})"
  30.     when :down:
  31.       transform = "rotate(180 #{cx} #{cy})"
  32.     when :left:
  33.       transform = "rotate(270 #{cx} #{cy})"
  34.     when :right:
  35.       transform = "rotate(90 #{cx} #{cy})"
  36.     end
  37.    
  38.     a = a.to_f
  39.     v = a * Math.sqrt(3.0) / 2.0
  40.    
  41.     # facing up
  42.     p1 = [cx - a/2.0, cy + v/3.0] # left
  43.     p2 = [cx + a/2.0, cy + v/3.0] # right
  44.     p3 = [cx, cy - 2.0*v/3.0] # top
  45.    
  46.     @text << %{<polygon points="#{p1.join(',')} #{p2.join(',')} #{p3.join(',')}" fill="#000000" transform="#{transform}"/>}
  47.   end
  48.  
  49.   def text(x, y, text)
  50.     @text << %{<text x="#{x}" y="#{y}" text-anchor="middle">#{text}</text>}
  51.   end
  52.  
  53.   def line(x1, y1, x2, y2, style = nil)
  54.     @text << %{<line x1="#{x1}" y1="#{y1}" x2="#{x2}" y2="#{y2}" style="#{style || "stroke:black; stroke-width:0.7"}" />\n}
  55.   end
  56.  
  57.   def lines(r1, r2, angles) #inner, #outer, #angles
  58.     angle = angles.avg
  59.     p1 = PolarPoint.new(r1, angle)
  60.        
  61.     angles.each do |a|
  62.       p2 = PolarPoint.new(r2, a)
  63.  
  64.       line(p1.x, p1.y, p2.x, p2.y)
  65.     end
  66.    
  67.     angle
  68.   end
  69.  
  70.   def to_s
  71.     @text + @circles + "</g></svg>"
  72.   end
  73. end