#!/usr/bin/ruby # WA5ZNU $FONTSIZE=12 $axesTextColor = "stroke:blue;" $textStyle="text-anchor: center; Font-Family: Fixed; font-size: %spt; " % ($FONTSIZE) $myStyle = "fill:none; stroke:red; stroke-width: 0.5;" $dotStyles = ["fill:blue;", "fill:green;", "fill:red;"] $strokeStyles = ["stroke:blue;", "stroke:green;", "stroke:red;"] $polyLineStyles = ["fill:none; stroke:blue;", "fill:none; stroke:green;", "fill:none; stroke:red;"] $viewBox = 'viewBox="0 0 800 800"' # svg offers scaling as well, but at least until 1.2 it seems not possible to scale views without # scaling fonts. def rect(rho, theta) theta = theta - 90 # convert to north=0=up theta = Math::PI * theta / 180.0 return [rho * Math::cos(theta), rho * Math::sin(theta)] end class Text def initialize(data, x, y,scale) @SCALE=scale @style = nil @x = x*@SCALE @y = y*@SCALE @data = data end def set_style(style) @style = style end def getXML() buf = "" buf += @data buf += "" return buf end end class Circle def initialize(cx, cy, r, scale) @SCALE=scale @style = nil @cx = cx*@SCALE @cy = cy*@SCALE @r = r*@SCALE end def set_style(style) @style = style end def getXML() buf = "" return buf end end class Line def initialize(x1, y1, x2, y2,scale) @SCALE=scale @style = nil @x1 = x1*@SCALE @x2 = x2*@SCALE @y1 = y1*@SCALE @y2 = y2*@SCALE end def set_style(style) @style = style end def getXML() buf = "" return buf end end class Polyline def initialize(points) @style = nil @points = points end def set_style(style) @style = style end def getXML() buf = "" return buf end end class LIT def initialize(text) @buf = text end def getXML() return @buf end end class TRANSFORM_BEGIN < LIT def initialize(x, y) super("") end end class TRANSFORM_END < LIT def initialize() super('') end end class SVG def initialize(x,y,scale) @SCALE=scale @buf = [LIT.new(''), LIT.new(''), TRANSFORM_BEGIN.new(x*@SCALE, y*@SCALE)] end def addElement(element) @buf << element end def getXML() @buf << TRANSFORM_END.new() buf2 = @buf.collect{ |x| x.getXML() } buf2 << '' return buf2.join("\n") end end class Patterns def initialize(max_db, min_db, db_graph_spacing,scale) @POLAR_LABEL_STEP=15 @MAX_DB=max_db @MIN_DB=min_db @DB_GRAPH_SPACING=db_graph_spacing @GRAPH_DB_RANGE=(@MAX_DB-@MIN_DB) @X=(@GRAPH_DB_RANGE+2*@DB_GRAPH_SPACING) @Y=(@GRAPH_DB_RANGE+2*@DB_GRAPH_SPACING) @SCALE=scale end def polarLinesGraph(filenames) s = SVG.new(@X,@Y,@SCALE) plotGraphPaper(s) i = 0 for filename in filenames do plotLines(i, filename, s, $polyLineStyles[i]) i += 1 end return s.getXML() end def polarDotsGraph(filenames) s = SVG.new(@X,@Y,@SCALE) plotGraphPaper(s) i = 0 for filename in filenames do plotDots(i, filename, s, $dotStyles[i]) i += 1 end return s.getXML() end def mytext(str,x,y,textColorStyle) t2 = Text.new(str,x,y,@SCALE) t2.set_style($textStyle + textColorStyle) return t2 end def plotGraphPaper(s) drawCircles(s) drawLines(s) plotPolarLabels(s) end def plotPolarLabels(s) (0.step(359,@POLAR_LABEL_STEP)).each() do |theta| (x,y) = rect(@GRAPH_DB_RANGE+@DB_GRAPH_SPACING, theta) s.addElement(mytext(theta.to_s, x, y, $axesTextColor)) (x,y) = rect(@GRAPH_DB_RANGE, theta) s.addElement(plin(x, y)) end end def drawCircles(s) (0.upto(@GRAPH_DB_RANGE/@DB_GRAPH_SPACING)).each() do |x| v=x*@DB_GRAPH_SPACING label = (@MIN_DB+v).to_s s.addElement(circ(v)) s.addElement(mytext(label, (v), 0, $axesTextColor)) s.addElement(mytext(label, (-v), 0, $axesTextColor)) # s.addElement(mytext(l, 0, (v))) # s.addElement(mytext(l, 0, (-v))) end end def drawLines(s) s.addElement(hlin(-@GRAPH_DB_RANGE, @GRAPH_DB_RANGE, 0)) s.addElement(vlin(0, -@GRAPH_DB_RANGE, @GRAPH_DB_RANGE)) end def circ(w) t2 = Circle.new(0, 0, w,@SCALE) t2.set_style($myStyle) return t2 end def dot(x,y,dotStyle) t2 = Circle.new(x, y, 0.2,@SCALE) t2.set_style(dotStyle) return t2 end def plin(x, y) t2 = Line.new(0, 0, x, y, @SCALE) t2.set_style($myStyle) return t2 end def hlin(x1, x2, y) t2 = Line.new(x1, y, x2, y,@SCALE) t2.set_style($myStyle) return t2 end def vlin(x, y1, y2) t2 = Line.new(x, y1, x, y2,@SCALE) t2.set_style($myStyle) return t2 end def plotLines(i, filename, s, polyLineStyle) s.addElement(mytext(filename, 0, @Y + i * $FONTSIZE*2/@SCALE, $textStyle + $strokeStyles[i])) points = "" for line in open(filename) (theta,rho) = line.split() nrho = Float(rho)-@MIN_DB ntheta = Float(theta) (x,y) = rect(nrho, ntheta) points = points + ((x*@SCALE)).to_s + "," + ((y*@SCALE)).to_s+"," #s.addElement(mytext(rho, x, y)) end t2 = Polyline.new(points) t2.set_style(polyLineStyle) s.addElement(t2) end def plotDots(i, filename, s, dotStyle) s.addElement(mytext(filename, 0, @Y + i * $FONTSIZE*2/@SCALE, $textStyle + $strokeStyles[i])) for line in open(filename) (theta,rho) = line.split() nrho = Float(rho)-@MIN_DB ntheta = Float(theta) (x,y) = rect(nrho, ntheta) s.addElement(dot(x,y,dotStyle)) #s.addElement(mytext(rho, x, y)) end end end