
Untitled
By: a guest on
May 16th, 2012 | syntax:
None | size: 1.93 KB | hits: 10 | expires: Never
class SVG
def initialize
@text = <<-EOD
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="100%" height="100%" version="1.1"
xmlns="http://www.w3.org/2000/svg">
<g transform="translate(600, 320)">
EOD
@circles = ""
end
def circle(cx, cy, r, stroke, fill)
@circles << %{<circle cx="#{cx}" cy="#{cy}" r="#{r}" stroke="#{stroke}" fill="#{fill}" stroke-width="0.5" />\n}
end
def rect(x, y, w, h, transform = nil)
@text << %{<rect x="#{x}" y="#{y}" width="#{w}" height="#{h}"#{transform ? " transform=\"#{transform}\"" : ''} fill="none" stroke="black" />\n}
end
def triangle(cx, cy, a, dir)
case dir
when :up:
transform = "rotate(0 #{cx} #{cy})"
when :down:
transform = "rotate(180 #{cx} #{cy})"
when :left:
transform = "rotate(270 #{cx} #{cy})"
when :right:
transform = "rotate(90 #{cx} #{cy})"
end
a = a.to_f
v = a * Math.sqrt(3.0) / 2.0
# facing up
p1 = [cx - a/2.0, cy + v/3.0] # left
p2 = [cx + a/2.0, cy + v/3.0] # right
p3 = [cx, cy - 2.0*v/3.0] # top
@text << %{<polygon points="#{p1.join(',')} #{p2.join(',')} #{p3.join(',')}" fill="#000000" transform="#{transform}"/>}
end
def text(x, y, text)
@text << %{<text x="#{x}" y="#{y}" text-anchor="middle">#{text}</text>}
end
def line(x1, y1, x2, y2, style = nil)
@text << %{<line x1="#{x1}" y1="#{y1}" x2="#{x2}" y2="#{y2}" style="#{style || "stroke:black; stroke-width:0.7"}" />\n}
end
def lines(r1, r2, angles) #inner, #outer, #angles
angle = angles.avg
p1 = PolarPoint.new(r1, angle)
angles.each do |a|
p2 = PolarPoint.new(r2, a)
line(p1.x, p1.y, p2.x, p2.y)
end
angle
end
def to_s
@text + @circles + "</g></svg>"
end
end